【问题标题】:SQL Server 2012 Vertical data to HorizontalSQL Server 2012 垂直数据到水平
【发布时间】:2020-02-25 20:46:01
【问题描述】:

我有一个 SQL Server 2012 表,我需要使用多列进行轮换。我已经检查了其他问题的几个答案,例如(SQL Server 2008 垂直数据到水平),但没有一个适合我的情况。我有临时表“#temp2”,我想同时选择和过滤它。以下是#temp2 表的内容

Outstanding     Members     Loan Officer    DaysDelay
------------------------------------------------------------
205130.62        165        Kevin           Days 0
 61259.68         42        Kevin           Days 1-30
   141.88          2        Kevin           Days 31-60
   562.23          1        Kevin           Days 91-180
  8233.3           4        Kevin           Days over 180
206648.85        153        Alan            Days 0
 39903.47         40        Alan            Days 1-30
  3263.66          2        Alan            Days 91-180
 15628.94          8        Alan            Days over 180
230604.98        155        Kate            Days 0
 58086.31         39        Kate            Days 1-30
 22616.34          2        Kate            Days 31-60
  1310.46          1        Kate            Days 61-90
  3645.12          3        Kate            Days 91-180
  3466.41          3        Kate            Days over 180

我想把这张桌子改成下面这张

Loan Officer    Days 0      Days 1-30   Days 31-60      Days 61-90      Days 91-180 Days over 180
Kevin           205130.62   61259.68    141.88          NULL            562.23      8233.3
Kevin           165         42          2               NULL            1           4
Alan            206648.85   39903.47    NULL            NULL            3263.66     15628.94
Alan            153         40          NULL            NULL            2           8
Kate            230604.98   58086.31    22616.34        1310.46         3645.12     3466.41
Kate            155         39          2               1               3           3

表格将从垂直旋转到水平,对于同一信贷员,将有两行,一为杰出成员。

感谢您的帮助。

【问题讨论】:

  • 如何确定重名的顺序?

标签: sql-server tsql pivot pivot-table


【解决方案1】:

您可以像这样使用条件聚合:

select loanofficer,
       max(case when daysdelay = 'Days 0' then outstanding end) as days_0,
       max(case when daysdelay = 'Days 1-30' then outstanding end) as days_1_30,
       . . .   -- for the rest of the columns
from (select t.*,
             row_number() over (partition by loanofficer order by members desc) as seqnum
      from #temp2 t
     ) t
group by loanofficer, seqnum
order by loanofficer, seqnum;

【讨论】:

    【解决方案2】:

    Gordon Linof 查询接近但未显示表中的成员,但这就是我解决问题的方法:

    select * from 
    (select [Loan Officer],
           max(case when daysdelay = 'Days 0' then outstanding end) as days_0,
           max(case when daysdelay = 'Days 1-30' then outstanding end) as days_1_30,
           max(case when daysdelay = 'Days 31-60' then outstanding end) as days_31_60,
           max(case when daysdelay = 'Days 61-90' then outstanding end) as days_61_90,
           max(case when daysdelay = 'Days 91-180' then outstanding end) as days_91_180,
           max(case when daysdelay = 'Days over 180' then outstanding end) as days_above_190
    from (select t.*,
                 row_number() over (partition by [Loan Officer] order by members desc) as seqnum
          from #temp2 t
         ) t
    group by [Loan Officer]
    union all
    select [Loan Officer],
           max(case when daysdelay = 'Days 0' then members end) as days_0,
           max(case when daysdelay = 'Days 1-30' then members end) as days_1_30,
           max(case when daysdelay = 'Days 31-60' then members end) as days_31_60,
           max(case when daysdelay = 'Days 61-90' then members end) as days_61_90,
           max(case when daysdelay = 'Days 91-180' then members end) as days_91_180,
           max(case when daysdelay = 'Days over 180' then members end) as days_above_190
    from (select t.*,
                 row_number() over (partition by [Loan Officer] order by members desc) as seqnum
          from #temp2 t
         ) t
    group by [Loan Officer]
    ) x where 1=1
    order by [Loan Officer]
    

    【讨论】:

      猜你喜欢
      • 2013-08-09
      • 1970-01-01
      • 1970-01-01
      • 2013-12-22
      • 1970-01-01
      • 2010-09-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多