重复数据中取最大最小值,而且不同类型只显示一条

时间:2023-01-16 15:16:03

---重复数据中取最大最小值,而且不同类型只显示一条

--CREATE TABLE test_aa(
--a INT,
--b VARCHAR(20)    
--)
--INSERT into test_aa(a,b) VALUES(1,'aa')
--INSERT into test_aa(a,b) VALUES(14,'aa')
--INSERT into test_aa(a,b) VALUES(11,'ab')
--INSERT into test_aa(a,b) VALUES(12,'ac')
--INSERT into test_aa(a,b) VALUES(11,'ae')
--INSERT into test_aa(a,b) VALUES(1,'aa')
--INSERT into test_aa(a,b) VALUES(1,'aa')
--INSERT into test_aa(a,b) VALUES(1,'aa')
--INSERT into test_aa(a,b) VALUES(1,'aa')


SELECT a,b  FROM test_aa ta
--max
SELECT a FROM test_aa ta WHERE ta.a>(SELECT min(ta2.a) FROM test_aa ta2 WHERE ta2.b=ta.b)
--min
SELECT a FROM test_aa ta WHERE ta.a<(SELECT max(ta2.a) FROM test_aa ta2 WHERE ta2.b=ta.b)



SELECT a,b  FROM test_aa ta WHERE ta.a=(SELECT min(ta2.a) FROM test_aa ta2 WHERE ta2.b=ta.b) AND
ta.a NOT IN (SELECT a FROM test_aa ta WHERE ta.a>(SELECT min(ta2.a) FROM test_aa ta2 WHERE ta2.b=ta.b))


SELECT a,b  FROM test_aa ta WHERE ta.a=(SELECT max(ta2.a) FROM test_aa ta2 WHERE ta2.b=ta.b) AND
ta.a NOT IN (SELECT a FROM test_aa ta WHERE ta.a<(SELECT max(ta2.a) FROM test_aa ta2 WHERE ta2.b=ta.b))