1

我在 sql 中运行这个命令

select ID, TASK, CreatedBy,CreateDate
from John Results group by ID, TASK, CreatedBy, CreateDate

这是返回这个

20  text    user1   2008-11-07 19:10:47.230
20  text    user2   2008-11-07 17:45:17.993
24  text    user3   2008-11-07 18:58:08.727
25  text    user4   2008-11-07 20:19:01.293
36  text    user5   2008-11-10 21:31:23.610
37  text    user6   2008-11-10 21:34:09.597
38  text    user7   2008-11-10 21:35:19.660

有两个 ID 是 20,我试图得到日期最短的结果,所以在这个例子中它是

20  text    user2   2008-11-07 17:45:17.993

我将如何为所有重复的 ID 执行此
操作非常感谢

4

3 回答 3

3
select ID, TASK, FIRST(CreatedBy),MIN(CreateDate) as 'LowestDate'
from John Results group by ID, TASK, CreatedBy, CreateDate 
having CreateDate = LowestDate 
于 2012-05-11T15:21:52.980 回答
3
    WITH mycte
    (id, createdate, createdby, rownum)
    AS
    (
     SELECT id, createdate,createdby,  ROW_NUMBER() OVER (PARTITION BY id ORDER BY createdate  ASC) AS ROWNUM
     FROM johnresults
    )

    SELECT j.id, j.task, mycte.createdby, mycte.createdate 
    FROM
    johnresults j
    inner join mycte ON j.id = mycte.id and mycte.rownum = 1
    GROUP BY j.id, j.task, mycte.createdby, mycte.createdate
于 2012-05-11T15:43:05.270 回答
0
select ID, TASK, CreatedBy, min(CreateDate) as 'LowestDate'
from John Results 
group by ID, TASK, CreatedBy
于 2012-05-11T15:20:15.623 回答