在此示例中,我是否可以使用T-SQL语法和单个select语句仅检索行BB,DD和FF?
C1 | C2 | C3 | C4
-----------------
AA | KK | 11 | 99
BB | KK | 11 | 99
CC | KK | 22 | 99
DD | KK | 22 | 99
EE | KK | 33 | 99
FF | KK | 33 | 99
好的,所以我最终解决了我的问题:SELECT distinct [C4],[C1],[C2],[C3] FROM [Table]其中[C4] ='MyValue'顺序由[ C3] desc。
答案 0 :(得分:2)
试一试:
SELECT C1, C2, C3, C4 FROM mytable WHERE C1 IN ('BB', 'DD', 'FF')
如果要使C3列唯一,则查询应为:
SELECT MAX(C1), C2, C3, C4 FROM mytable GROUP BY C2, C3, C4
答案 1 :(得分:1)
试试这个
SELECT t.C1, t.C2, t.C3, t.C4 FROM
(
SELECT *,
ROW_NUMBER() OVER (PARTITION BY C3 ORDER BY C1 DESC) AS seqnum
FROM MyTable
) t
WHERE seqnum = 1
答案 2 :(得分:1)
这适用于您的特定示例:
SELECT
C1 = MAX(C1),
C2,
C3,
C4
FROM atable
GROUP BY
C2,
C3,
C4
;
如果从C1
中选择正确的值,则应遵循比获取MAX()
更复杂的逻辑,您可能需要使用@bvr's suggestion(调整ORDER BY
正确地说。)