简体   繁体   English

SQL Server中的多个列上的数据透视表

[英]Pivot table in SQL Server on multiple columns

I have a table that shows data like this 我有一个表格显示这样的数据

表中的数据

I need data in this format 我需要这种格式的数据

              GIN      APINV        AR Rec       Requisitions
Total         8          11           77
Pending       7           6           77
New           1          77           0
Approved      0           5           0
Rejected      1           0           0

And so on... 等等...

What I already know is I have to use PIVOT but I have only worked with PIVOT using a single column. 我已经知道的是我必须使用PIVOT但我只使用PIVOT使用一个列。

Combination of CROSS APPLY and PIVOT should do: CROSS APPLYPIVOT组合应该:

select *
from (
    select t.appname, x.status, x.val
    from your_table t
    cross apply (
        values 
            ('Total', t.total),
            ('Pending', t.pending),
            ('New', t.New),
            ('Approved', t.Approved),
            ('Rejected', t.Rejected)
    ) x (status, val)
) t pivot (
    sum(val) 
    for appname in (
        [GIN],[APINV],....
    )
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM