在 SQL 中,可能会将两行变成一行,其中一些行元素变成列

In SQL potentially turn two rows into one with some of the row elements becoming columns

一般每个付款人有两行,一行代表成功计数,另一行代表失败计数。

我想将两行 return 作为一个包含成功和失败列的行。

但有时只有一行,要么成功,要么失败,但不能同时出现。

我已经尝试连接源 table 本身,左连接和右连接都不会拾取丢失的成功或丢失的失败。完整连接 return 医疗保险行的四行,这真的很麻烦。

==> source data below <==

CorpName           PayerName                 PlanName                 PayerID Status  PlanUid  Count
------------------ ------------------------- ------------------------ ------- ------- -------- -----
Medicare of Texas  Novitas Solution          Medicare - Texas, Part B 04412   FAILURE 660FED8E  19
Medicare of Texas  Novitas Solution          Medicare - Texas, Part B 04412   SUCCESS 660FED8E  29
GHI PPO            GHI PPO                   Group Health Inc. - New  13551   FAILURE BFF5E581   1
United Healthcare  Benefits of Texas, Inc.   United Healthcare        87726   SUCCESS 9C1E2A67   5


==> desired output <==

CorpName           PayerName                 PlanName                 PayerID PlanUid  Success Failure
------------------ ------------------------- ------------------------ ------- -------- ------- -------
Medicare of Texas  Novitas Solution          Medicare - Texas, Part B 04412   660FED8E  29       19
GHI PPO            GHI PPO                   Group Health Inc. - New  13551   BFF5E581   0        1
United Healthcare  Benefits of Texas, Inc.   United Healthcare        87726   9C1E2A67   5        0

你基本上是在枢轴之后,你可以聚合并使用条件案例表达式,未经测试但类似于:

select
    max(CorpName)  CorpName, 
    max(PayerName) PayerName, 
    max(PlanName)  PlanName, 
    max(PayerID)   PayerID, 
    max(PlanUid)   PlanUid, 
    coalesce(case when status='SUCCESS' then "count" end,0) as Success,
    coalesce(case when status='FAILURE' then "count" end,0) as Failure
group by 
    CorpName, PayerName, PlanName, PayerID, PlanUid;

你可以使用自join:

select p.corpname, p.payername, p.planname, p.payerid, max(coalesce(p1.`count`, 0)), max(coalesce(p2.`count`, 0)) from plans p 
left join plans p1 on p.payerid = p1.payerid and p1.status='SUCCESS' 
left join  plans p2 on p.payerid = p2.payerid and p2.status='FAILURE' 
group by p.corpname, p.payername, p.planname, p.payerid

你可以试试这个:

IF (OBJECT_ID('tempdb..#MyTable') IS NOT NULL)
    BEGIN
      DROP TABLE #MyTable
    END;
  IF (OBJECT_ID('tempdb..#product') IS NOT NULL)
    BEGIN
      DROP TABLE #product
    END;
    
CREATE TABLE #MyTable (CorpName varchar(50) NOT NULL, PayerName varchar(50) NOT NULL, PlanName varchar(50)  NOT NULL, PayerID varchar(20)  NOT NULL, [Status] varchar(20) NOT NULL, PlanUid varchar(20), [COUNT] int not null)

INSERT INTO #MyTable (CorpName ,PayerName,PlanName ,PayerID, [Status] , PlanUid, [Count]) 
    Values ('Medicare of Texas','Novitas Solution', 'Medicare - Texas, Part B','04412','FAILURE', '660FED8E',  19),
     ('Medicare of Texas','Novitas Solution', 'Medicare - Texas, Part B','04412','SUCCESS', '660FED8E',  29),
     ('GHI PPO','GHI PPO', 'Group Health Inc. - New' , '13551',   'FAILURE', 'BFF5E581',  1),
     ('United Healthcare',  'Benefits of Texas, Inc.',   'United Healthcare',  '87726','SUCCESS','9C1E2A67', 5)

-- select * from #MyTable
select CorpName ,PayerName,PlanName ,PayerID,PlanUid
, sum(coalesce(case when [status] = 'SUCCESS' then ([count]) end, 0)) as SuccessCnt
, sum(coalesce(case when [status] = 'FAILURE' then ([count]) end, 0)) as FailureCnt
from #MyTable
group by CorpName ,PayerName,PlanName ,PayerID,PlanUid