0

在本例中,我能否使用 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] where [C4] = 'MyValue' order by [C3] desc .

4

3 回答 3

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
于 2013-08-12T23:23:01.453 回答
1

这适用于您的特定示例:

SELECT
  C1 = MAX(C1),
  C2,
  C3,
  C4
FROM atable
GROUP BY
  C2,
  C3,
  C4
;

如果从中选择正确的值C1应该遵循比获取一个更复杂的逻辑MAX(),您可能需要使用@bvr 的建议(正确调整ORDER BY子句)。

于 2013-08-13T06:40:09.373 回答
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
于 2013-08-13T04:06:52.097 回答