SQL表将id的多行放入多列中

时间:2017-08-09 19:13:18

标签: sql sql-server database

我正在尝试从2个不同的表中获取数据,并将合并后的数据放入报告中。每个索赔号可以具有三种可能的状态之一:已批准,已拒绝,已上诉。上诉可以是成功的,也可以是被拒绝的。

目前,每个声明ID都有多个条目,但我希望将每个声明分组在一条记录中,并将每种可能的状态作为字段。关于获得输出的最佳方法的任何想法如下?

表1

Claim       AddDate     AddUser    
1234        08/01/2017  Catan    
4567        08/02/2017  Jigsu    
7890        08/07/2017  Panama

表2

Claim      Notes             NoteType
1234       Denied            2
1234       Appeal Success    3
4567       Approved          1
7890       Denied            2
7890       Appeal Denied     3

输出表

Claim     ApprovalNote        DenialNote      AppealNote
1234      NULL                Denied          Appeal Success    
4567      Approved            NULL            NULL    
7890      NULL                Denied          Appeal Denied

我正在使用SQL Server 2008。

5 个答案:

答案 0 :(得分:1)

您可以加入表2的每个子部分,以便在没有聚合或案例陈述的情况下轻松实现这一目标。

SELECT  
    t1.Claim ,
    ApprovalNote.Notes AS ApprovalNote ,
    DenialNote.Notes AS DenialNote ,
    AppealNote.Notes AS AppealNote
FROM    
    Table1 t1
    LEFT OUTER JOIN Table2 ApprovalNote 
        ON t1.Claim = ApprovalNote.Claim
        AND ApprovalNote.NoteType = 1
    LEFT OUTER JOIN Table2 DenialNote 
        ON t1.Claim = DenialNote.Claim
        AND DenialNote.NoteType = 2
    LEFT OUTER JOIN Table2 AppealNote 
        ON t1.Claim = AppealNote.Claim
        AND AppealNote.NoteType = 3;

答案 1 :(得分:0)

考虑聚合查询中包含的条件聚合:

SELECT m.Claim, Max(m.subApprovalNote) As ApprovalNote, 
                Max(m.subDenialNote) As DenialNote, 
                Max(m.subAppealNote) As AppealNote
FROM
   (SELECT t1.Claim, CASE WHEN t2.NoteType = 1
                          THEN t2.Notes
                          ELSE NULL
                     END AS subApprovalNote,
           CASE WHEN t2.NoteType = 2
                THEN t2.Notes
                ELSE NULL
           END AS subDenialNote,
           CASE WHEN t2.NoteType = 3
                THEN t2.Notes
                ELSE NULL
           END AS subAppealNote
    FROM table1 t1
    INNER JOIN table2 t2 ON t1.Claim = t2.Claim
   ) As m
GROUP BY m.Claim

答案 2 :(得分:0)

如果我理解正确,你可以尝试

Select claim ,
Case When exists (Select 1 from table2 where table1.claim = table2.claim 
                                         and notetype = 1
     Then 'Approved' Else Null End ,

Case When exists (Select 1 from table2 where table1.claim = table2.claim 
                                         and notetype = 2
     Then 'Denial' Else Null End ,


Case When exists (Select 1 from table2 where table1.claim = table2.claim 
                                         and notetype = 3 
     Then notes Else Null End 

from table1

答案 3 :(得分:0)

--Sample Data
declare @Table2 table(Claim int ,Notes varchar(500))
insert into @Table2
select 1234,'Denied' union
select 1234,'Appeal Success' union
select 4567,'Approved' union
select 7890,'Denied' union
select 7890,'Appeal Denied' 
--Main Query
SELECT Claim, 
[Approved] AS ApprovedNote,[Denied] AS DeniedNote, COALESCE([Appeal 
Success],[Appeal Denied]) AS AppealNote
FROM
(SELECT Claim,Notes FROM @Table2) AS SourceTable
PIVOT
(
MAX(Notes)
FOR Notes IN ([Denied],[Appeal Success],[Approved],[Appeal Denied])
) AS PivotTable;

答案 4 :(得分:0)

SELECT uc.claim,
       uc.adduser,
       MAX(CASE WHEN nc.notetype = 1 THEN nc.notes END) AS approvalnote,
       MAX(CASE WHEN nc.notetype = 2 THEN nc.notes END) AS denialnote,
       MAX(CASE WHEN nc.notetype = 3 THEN nc.notes END) AS appealnote
  FROM user_claim uc
 INNER JOIN notes_claim nc
    ON uc.claim = nc.claim
 GROUP BY uc.claim,
          uc.adduser

结果

claim   adduser approvalnote    denialnote  appealnote
1234    Catan   NULL            Denied      Appeal Success
4567    Jigsu   Approved        NULL        NULL
7890    Panama  NULL            Denied      Appeal Denied