我有一张像这样的表Table
:
ColumnA (VARCHAR)|ColumnB (VARCHAR)|ColumnC (VARCHAR)|ColumnD (DATE)
A1 B1 C1 2012-12-06
A1 B1 C1 2012-12-12
A2 B2 C2 2012-12-07
A2 B2 C2 2012-12-09
我想只选择第一个匹配到ColumnA-ColumnB-ColumnC的结果,所以结果将是这样的:
A1 | B1 | C1 | 2012-12-06
A2 | B2 | C2 | 2012-12-07
答案 0 :(得分:2)
您可以在Date列上使用聚合函数,在其余部分使用组:
select ColumnA, ColumnB, ColumnC, Min(ColumnD)
from yourtable
group by ColumnA, ColumnB, ColumnC
或者您可以使用row_number()
:
select ColumnA, ColumnB, ColumnC, ColumnD
from
(
select ColumnA, ColumnB, ColumnC, ColumnD,
row_number() over(partition by ColumnA, ColumnB, ColumnC order by ColumnD) rn
from yourtable
) src
where rn = 1
答案 1 :(得分:1)
看起来像分组是你想要的。
SELECT ColumnA, ColumnB, ColumnC, MIN(ColumnD)
FROM MyTable
GROUP BY ColumnA, ColumnB, ColumnC