选择ID最大且条件

时间:2015-08-12 08:51:16

标签: sql sql-server select greatest-n-per-group

我有以下结构的表评论:

CommentId (int), 
CommentText (nvarchar(max))
EditedBy (nvarchar(max))
ParentCommentId (int)
CaseId (int)

针对特定caseId我想选择最新评论文本以及EditedBy列。

例如,如果有人添加评论表将是

CommentId   CommentText          EditedBy     ParentCommentId   CaseId
   1         ParentComment1         ABC          NULL               1 
   2         ParentComment2         ABC          NULL               1 

现在,如果有人编辑了该评论,该表格看起来像

CommentId   CommentText          EditedBy     ParentCommentId   CaseId
   1         ParentComment1       ABC          NULL               1 
   2         ParentComment2       ABC          NULL               2
   3         Comment2             DEF          1                  1 

此编辑可以进行多次 我想选择最新的评论以及历史。 在这种情况下,我的数据集应该是这样的:

CaseId   CommentId   CommentText       Predicate
  1       3           Comment2         Created By ABC , Updated by DEF
  1       2           ParentComment2   Created By ABC

这是问题的简化版本。 TIA

3 个答案:

答案 0 :(得分:3)

您可以使用FOR XML PATH创建谓词列。这样的事情。

<强> SQL Fiddle

示例数据

CREATE TABLE Comment
    ([CommentId] int, [CommentText] varchar(8), [EditedBy] varchar(3), [ParentCommentId] varchar(4), [CaseId] int);

INSERT INTO Comment
    ([CommentId], [CommentText], [EditedBy], [ParentCommentId], [CaseId])
VALUES
    (1, 'Comment1', 'ABC', NULL, 1),
    (2, 'Comment2', 'DEF', '1', 1);

<强>查询

;WITH CTE AS 
(
SELECT CommentID, CommentText,EditedBy,ParentCommentID,CaseID,ROW_NUMBER()OVER(PARTITION BY Caseid ORDER BY CommentID DESC) RN
FROM Comment
), CTE2 as
(
SELECT CommentID, CommentText,EditedBy,ParentCommentID,CaseID,
  (
    SELECT
    CASE WHEN ParentCommentID IS NULL THEN 'Created By ' ELSE ', Updated By ' END
    + EditedBy
    FROM CTE C1
    WHERE C1.CaseID = C2.CaseID
    ORDER BY CommentID ASC
    FOR XML PATH('')
  ) as Predicate
FROM CTE C2
WHERE RN = 1
)
SELECT CaseID,CommentID, CommentText,Predicate FROM CTE2;

修改

如果您不想为更新Updated By的每位用户重复caseid,请使用以下CASE

CASE WHEN ParentCommentID IS NULL THEN 'Created By ' + EditedBy + ', Updated By' ELSE '' END,
CASE WHEN ParentCommentID IS NOT NULL THEN ', ' + EditedBy ELSE '' END

而不是

    CASE WHEN ParentCommentID IS NULL THEN 'Created By ' ELSE ', Updated By ' END + EditedBy

<强>输出

| CaseID | CommentID | CommentText |                      Predicate |
|--------|-----------|-------------|--------------------------------|
|      1 |         2 |    Comment2 | Created By ABC, Updated By DEF |

编辑2

使用递归CTE来实现预期的输出。这样的事情。

<强> SQL Fiddle

<强>查询

;WITH CTEComment AS 
(
SELECT CommentID as RootCommentID,CommentID, CommentText,EditedBy,ParentCommentID,CaseID
FROM Comment
WHERE ParentCommentID IS NULL
UNION ALL 
SELECT CTEComment.RootCommentID as RootCommentID,Comment.CommentID, Comment.CommentText,Comment.EditedBy,Comment.ParentCommentID,Comment.CaseID
FROM CTEComment
INNER JOIN Comment
    ON CTEComment.CommentID = Comment.ParentcommentID
        AND CTEComment.CaseID = Comment.CaseID
), CTE as  
(
SELECT CommentID,RootCommentID,CommentText,EditedBy,ParentCommentID,CaseID,ROW_NUMBER()OVER(PARTITION BY CaseID,RootCommentID ORDER BY CommentID DESC) RN
FROM CTEComment
), CTE2 as
(
SELECT CommentID, CommentText,EditedBy,ParentCommentID,CaseID,
  (
    SELECT
    CASE WHEN ParentCommentID IS NULL THEN 'Created By ' ELSE ', Updated By ' END
    + EditedBy
    FROM CTE C1
    WHERE C1.CaseID = C2.CaseID
    AND C1.RootCommentID = C2.RootCommentID
    ORDER BY CommentID ASC
    FOR XML PATH('')
  ) as Predicate
FROM CTE C2
WHERE RN = 1
)
SELECT CaseID,CommentID, CommentText,Predicate FROM CTE2;

<强>输出

| CaseID | CommentID |    CommentText |                      Predicate |
|--------|-----------|----------------|--------------------------------|
|      1 |         3 |       Comment2 | Created By ABC, Updated By DEF |
|      2 |         2 | ParentComment2 |                 Created By ABC |

答案 1 :(得分:-1)

You can use this method
DECLARE @CaseId [int];
SET @CaseId=1`enter code here`
DECLARE @GeneratedBy [nvarchar](max);
SET @GeneratedBy=(SELECT  EditBy from Comment  WHERE ParentCommentId is NULL AND CaseId = @CaseId)
PRINT @GeneratedBy;
SELECT CaseId , CommentId , CommentText  , 'Created By '+@GeneratedBy+ ', Updated By ' +EditBy  AS Predicate FROM Comment
WHERE CaseId = @CaseId AND ParentCommentId=(SELECT  max(ParentCommentId) from Comment  WHERE  CaseId = @CaseId)

答案 2 :(得分:-1)

您只需使用自我联接即可获得结果:

找到以下查询:

select a.CaseId,b.CommentId, b.CommentText,'Created By '+a.EditedBy +', update by '+b.EditedBy as Predicate
from Comment a
inner join Comment b
on a.CaseId=b.CaseId
where a.CommentId<b.CommentId