我有这样的桌子:
ID workType state IterationId date
-------------------------------------------------------------------
49 User Story Active 14 2016-06-28
49 User Story Active 10 2016-06-29
49 User Story Active 23 2016-07-03
49 User Story Active 26 2016-07-24
49 User Story Active 27 2016-07-25
50 bug Active 23 2016-07-15
50 bug Active 26 2016-07-20
51 User Story Active 10 2016-06-25
51 User Story Active 14 2016-06-28
. . . . .
. . . . .
. . . . .
我需要选择每组ID中包含最少日期的行。所以:
ID workType state IterationId date
---------------------------------------------------------------
49 User Story Active 14 2016-06-28
50 bug Active 23 2016-07-15
51 User Story Active 10 2016-06-25
. . . . .
. . . . .
. . . . .
答案 0 :(得分:5)
您可以尝试使用ROW_NUMBER() PARTITION BY ID
进行以下操作。
SELECT *
FROM (SELECT *,
Row_number() OVER (
partition BY id
ORDER BY [date]) rn
FROM your_table) t
WHERE rn = 1
答案 1 :(得分:1)
这只猫的另一种皮肤...
;WITH minRow AS (
SELECT ID,
MIN(Date) as [MinDate]
FROM YourTable
GROUP BY ID)
SELECT t.ID,
t.workType,
t.State,
t.IterationId,
t.Date
FROM YourTable t
JOIN mt on mt.Id = t.Id AND t.Date = mt.MinDate