我有一个表T的列x,y,a,b使得
T
x
y
a
b
SELECT x,y,a,b FROM T ORDER BY x,y,a,b
给我下表
x | y | a | b x1 | y1 | a1 | b1 x1 | y1 | a1 | b2 x1 | y1 | a2 | b1 x1 | y2 | a1 | b1 x1 | y2 | a1 | b2 x1 | y2 | a2 | b1
我将如何获得每个x,y组的第一行?也就是说,我将如何获得下表
x | y | a | b x1 | y1 | a1 | b1 x1 | y2 | a1 | b1
这是第二个示例:对于这样的表T
x | y | a | b x1 | y1 | a1 | b3 x1 | y1 | a1 | b4 x1 | y1 | a2 | b1 x1 | y1 | a2 | b2 x1 | y2 | a1 | b3 x1 | y2 | a1 | b4 x1 | y2 | a2 | b1 x1 | y2 | a2 | b2
我期望得到
x | y | a | b x1 | y1 | a1 | b3 x1 | y2 | a1 | b3
考虑到SQL 2005或更高版本:
SELECT T1.X, T1.Y, T1.A, T1.B FROM (SELECT X, Y, A, B, ROW_NUMBER() OVER (Partition BY X,Y Order By A,B) AS RowNum FROM T ) T1 WHERE T1.RowNum = 1