Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQL Server中多列上的透视表_Sql_Sql Server - Fatal编程技术网

SQL Server中多列上的透视表

SQL Server中多列上的透视表,sql,sql-server,Sql,Sql Server,我有一张表,上面显示了这样的数据 我需要这种格式的数据 GIN APINV AR Rec Requisitions Total 8 11 77 Pending 7 6 77 New 1 77 0 Approved 0 5

我有一张表,上面显示了这样的数据

我需要这种格式的数据

              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
等等


我已经知道的是我必须使用
PIVOT
,但我只使用了一列
PIVOT

交叉应用
PIVOT
的组合应该可以:

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],....
    )
);

交叉应用
枢轴
的组合应能:

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],....
    )
);

您预期输出中的列数是固定的,还是会变化/未知?@TimBiegeleisen请关注此链接,它也在工作。您预期输出中的列数是固定的,还是会变化/未知?@TimBiegeleisen请关注此链接,它也在工作。