您似乎假设按 [Column B] 排序,您可以在自联接中明确说明:
SELECT t1.[Column A], t1.[Column B], t1.[Column C], t1.[Column D]
FROM YourTableName t1
JOIN YourTableName t2
ON t2.[Column A] = t1.[Column A]
AND t2.[Column B] > t1.[Column B]
WHERE t2.[Column C] > t1.[Column C]
AND t2.[Column D] < t1.[Column D]
这假设恰好有两行 [Column A] = 'Ex1' 等。如果在 [Column A] 中有超过两行具有相同值的行,您可能会发现结果出乎意料。
请注意,前两个比较是连接条件的一部分,第三和第四个比较是 WHERE 子句的一部分。
更新:
满足更改的要求:可能有 20 行 [Column A] = 'Ex1'。返回 [Column A] 的不同值,其中 [Column C] 的最小值与 [Column D] 的最小值不在同一行。[B 列] 不相关。
SELECT DISTINCT t1.[Column A]
FROM YourTableName t1
JOIN
(SELECT [Column A], MIN([Column C]) AS MinC, MIN([Column D]) AS MinD
FROM YourTableName
GROUP BY [Column A]) t2
ON t2.[Column A] = t1.[Column A]
WHERE t1.[Column C] = t2.MinC
AND t1.[Column D] <> t2.MinD
这将返回:
Column A
Ex1
对于以下测试表:
CREATE TABLE YourTableName
([Column A] VARCHAR(50),
[Column B] VARCHAR(50),
[Column C] INT,
[Column D] INT)
和测试数据:
INSERT INTO YourTableName
([Column A], [Column B], [Column C], [Column D])
VALUES ('Ex1', 'Title A', 1, 2)
INSERT INTO YourTableName
([Column A], [Column B], [Column C], [Column D])
VALUES ('Ex1', 'Title B', 2, 1)
INSERT INTO YourTableName
([Column A], [Column B], [Column C], [Column D])
VALUES ('Ex1', 'Title C', 3, 1)