Sql选择字段唯一值并上后面的字段

by Weijie JIN on

需求: 选择A字段唯一值,并上B字段,如果A字段相同B字段不同,则取第一次出现的B字段的值。


A B Id
————————————–
1 A 1
1 B 2
2 C 3
2 D 4
3 E 5
3 F 6

选择结果:
A B Id
————————————–
1 A 1
2 C 3
3 E 5

SELECT *
FROM TEST
WHERE (ID IN
          (SELECT MIN(id)
         FROM TEST
         GROUP BY A))
SELECT * FROM TEST WHERE (ID IN (SELECT MIN(id) FROM TEST GROUP BY A))

Previous post:

Next post: