获取最后日期/时间的记录

时间:2017-02-13 21:07:07

标签: sql greatest-n-per-group

我有这个表布局。我希望获得带有最新日期/时间戳的“USER1”记录。

comments_sk case_sk comments    create_user_id  create_date
1           68200   test 1        USER1           2/11/17 11:00
1           68200   test 2        USER1           2/4/17 14:54
1           68200   test 3        USER2           12/30/16 12:37
1           8200    test 4        USER2           12/14/16 14:23
1           68200   test 5        USER3           12/13/16 9:16
1           68200   test 6        USER2           12/8/16 10:38
1           68200   test 7        USER3           12/2/16 10:58
1           68200   test 8        USER2           12/1/16 11:21
1           68200   test 9        USER2           11/8/16 13:36

我尝试了这些查询,但显示了USER1的所有记录。

SELECT DISTINCT comments_sk, case_sk, CAST(comments AS varchar) AS comments, create_user_id, create_date
FROM         comments AS s
WHERE     (s.case_sk = 68200) AND (s.create_user_id = 'USER1') AND (s.create_date =
                          (SELECT     MAX(s1.create_date) AS cdate
                            FROM          comments AS s1
                            WHERE      (s1.comments_sk = s.comments_sk)))
GROUP BY comments_sk, case_sk, CAST(comments AS varchar), create_user_id, create_date
ORDER BY create_date DESC
SELECT     s.comments_sk, s.case_sk, max(CAST(s.comments AS varchar)) AS comments, s.create_user_id, s.create_date
FROM         comments AS s INNER JOIN
                          (SELECT    comments_sk, case_sk, max(CAST(comments AS varchar)) AS comments, create_user_id, MAX(create_date) AS cdate
                            FROM          comments
                            GROUP BY comments_sk, case_sk, CAST(comments AS varchar), create_user_id, maint_user_id, maint_date, comment_type_sk) AS s1 ON 
                      s.case_sk = s1.case_sk AND s.create_date = s1.cdate
WHERE     (s.case_sk = 68200)
GROUP BY s.comments_sk, s.case_sk, CAST(s.comments AS varchar), s.create_user_id, s.create_date, s.comment_type_sk
HAVING      (s.create_user_id = 'USER1')
ORDER BY s.create_date DESC
SELECT     comments_sk, case_sk, CAST(comments AS varchar) AS comments, create_user_id, max(create_date)
FROM         comments AS s
WHERE     EXISTS
                          (SELECT     comments_sk, case_sk, CAST(comments AS varchar) AS comments, create_user_id, MAX(create_date) AS cdate
                            FROM          comments AS s1
                            WHERE      (s1.case_sk = s.case_sk) AND (s1.comments_sk = s.comments_sk)
                            GROUP BY comments_sk, case_sk, CAST(comments AS varchar), create_user_id, maint_user_id, maint_date, comment_type_sk
                            HAVING      (s.create_date = MAX(s1.create_date))) AND (s.create_user_id = 'USER1') AND (s.case_sk = 68200)
GROUP BY comments_sk, case_sk, CAST(comments AS varchar), create_user_id, create_date, comment_type_sk
ORDER BY create_date DESC

1 个答案:

答案 0 :(得分:2)

使用'前1'和降序,你应该能够得到你想要的。试试这个:

SELECT top 1 comments_sk, case_sk, CAST(comments AS varchar) AS comments, create_user_id, create_date
FROM         comments AS s
WHERE     (s.case_sk = 68200) AND (s.create_user_id = 'USER1') 
GROUP BY comments_sk, case_sk, CAST(comments AS varchar), create_user_id, create_date
ORDER BY create_date DESC

<强>更新

根据您的评论,我建议您使用 CTE 获取用户的最新评论,然后将其链接到评论表以获取实际评论。这是如何编写查询的示例。很可能你想修改WHERE子句并使用参数而不是硬编码值:

; with _cte as (
    SELECT DISTINCT comments_sk, case_sk, create_user_id, Max(create_date) as lastCommentDate
    FROM         comments AS s
    WHERE     s.create_user_id = 'USER1'
    GROUP BY comments_sk, case_sk, create_user_id
)

SELECT  _cte.comments_sk, _cte.case_sk, CAST(comments AS varchar) AS comments, _cte.create_user_id, _cte.lastCommentDate
FROM    comments AS s
        inner join _cte on s.create_date = _cte.lastCommentDate and s.create_user_id = _cte.create_user_id and s.case_sk = _cte.case_sk
WHERE     (s.case_sk = 68200) AND (s.create_user_id = 'USER1')