SQL Server:Stuff函数按行号汇总多行

时间:2019-07-10 15:22:25

标签: sql sql-server

我正在使用这两个表cl_line_remRemark_code。它们在Remark_code_id列上加入。

select Claim_id, Linenum, Remark_code_ID
from claim_line_remark 
where claim_id = 13639035

输出:

Claim_id      Linenum   Remark_code_ID
---------------------------------------
13639035    1   697
13639035    1   739
13639035    1   1322
13639035    2   697
13639035    2   739
13639035    2   1322
13639035    3   697
13639035    3   739
13639035    3   1322
13639035    4   697
13639035    4   739
13639035    4   1322
13639035    5   697
13639035    5   739
13639035    5   1322
13639035    6   697
13639035    6   739
13639035    6   1322

select clr.claim_id, clr.linenum, clr.remark_code_id, rc.name
from claim_line_remark clr
inner join Remark_Code rc on clr.Remark_Code_ID = rc.Remark_Code_ID
where claim_id = 13639035

输出:

claim_id    linenum   Remark_code_id    Name
------------------------------------------------
13639035    1   697       HAWAII FFS SCHEDULE
13639035    1   739       DO NOT BALANCE BILL.  
13639035    1   1322          SUPPRESS ALL EOB
13639035    2   697       HAWAII FFS SCHEDULE
13639035    2   739       DO NOT BALANCE BILL.  
13639035    2   1322          SUPPRESS ALL EOB
13639035    3   697       HAWAII FFS SCHEDULE
13639035    3   739       DO NOT BALANCE BILL. 
13639035    3   1322          SUPPRESS ALL EOB
13639035    4   697       HAWAII FFS SCHEDULE
13639035    4   739       DO NOT BALANCE BILL.  
13639035    4   1322          SUPPRESS ALL EOB
13639035    5   697       HAWAII FFS SCHEDULE
13639035    5   739       DO NOT BALANCE BILL. 
13639035    5   1322          SUPPRESS ALL EOB
13639035    6   697       HAWAII FFS SCHEDULE
13639035    6   739       DO NOT BALANCE BILL. 
13639035    6   1322          SUPPRESS ALL EOB

我尝试将这段代码与Stuff函数一起使用,但似乎无法正常工作:

  select 
      clr.Claim_ID, clr.Linenum,
      stuff((select '; ' + rc.Name
             from Remark_Code rc
             where rc.Remark_Code_ID = clr.Remark_Code_ID
             for xml path('')), 1, 1, '')  [Remarks]
  from 
      Claim_Line_Remark clr  
  where 
      claim_id in (13639035)

我要找的是这个

Claim_id     Linenum   Name
------------------------------------------------------------------------------
13639035    1       HAWAII FFS SCHEDULE,DO NOT BALANCE BILL,SUPPRESS ALL..
13639035    2       HAWAII FFS SCHEDULE,DO NOT BALANCE BILL,SUPPRESS ALL..
13639035    3       HAWAII FFS SCHEDULE,DO NOT BALANCE BILL,SUPPRESS ALL..
13639035    4       HAWAII FFS SCHEDULE,DO NOT BALANCE BILL,SUPPRESS ALL..
13639035    5       HAWAII FFS SCHEDULE,DO NOT BALANCE BILL,SUPPRESS ALL..
13639035    6       HAWAII FFS SCHEDULE,DO NOT BALANCE BILL,SUPPRESS ALL..

我在当前代码中得到的是这个

13639035    1    HAWAII FFS SCHEDULE
13639035    1    DO NOT BALANCE BILL.  
13639035    1    SUPPRESS ALL EOB
13639035    2    HAWAII FFS SCHEDULE
13639035    2    DO NOT BALANCE BILL.  
13639035    2    SUPPRESS ALL EOB
13639035    3    HAWAII FFS SCHEDULE
13639035    3    DO NOT BALANCE BILL.  
13639035    3    SUPPRESS ALL EOB
13639035    4    HAWAII FFS SCHEDULE
13639035    4    DO NOT BALANCE BILL. 
13639035    4    SUPPRESS ALL EOB
13639035    5    HAWAII FFS SCHEDULE
13639035    5    DO NOT BALANCE BILL. 
13639035    5    SUPPRESS ALL EOB
13639035    6    HAWAII FFS SCHEDULE
13639035    6    DO NOT BALANCE BILL. 
13639035    6    SUPPRESS ALL EOB

1 个答案:

答案 0 :(得分:0)

请尝试以下代码-

WITH CTE
AS
(
    SELECT clr.claim_id, 
           clr.linenum, 
           clr.remark_code_id, 
           rc.name
    FROM claim_line_remark clr
         INNER JOIN Remark_Code rc ON clr.Remark_Code_ID = rc.Remark_Code_ID
    WHERE claim_id = 13639035
)

SELECT B.claim_id,B.linenum,
STUFF((
    SELECT DISTINCT  ',' + A.Name 
    FROM CTE A 
    WHERE A.claim_id = B.claim_id AND A.linenum = B.linenum  
    FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 1, ''
) Name
FROM CTE B
group by B.claim_id,B.linenum

输出为-

claim_id    linenum Name
13639035    1       DO NOT BALANCE BILL.,HAWAII FFS SCHEDULE,SUPPRESS ALL EOB
13639035    2       DO NOT BALANCE BILL.,HAWAII FFS SCHEDULE,SUPPRESS ALL EOB
13639035    3       DO NOT BALANCE BILL.,HAWAII FFS SCHEDULE,SUPPRESS ALL EOB
13639035    4       DO NOT BALANCE BILL.,HAWAII FFS SCHEDULE,SUPPRESS ALL EOB
13639035    5       DO NOT BALANCE BILL.,HAWAII FFS SCHEDULE,SUPPRESS ALL EOB
13639035    6       DO NOT BALANCE BILL.,HAWAII FFS SCHEDULE,SUPPRESS ALL EOB