简体   繁体   English

动态SQL Server数据透视表

[英]Dynamic SQL Server Pivot Table

I found a nice script that dynamically creates by column names for my pivot table, but I am not getting the assigned values back into the table. 我发现了一个很好的脚本,它可以通过我的数据透视表的列名动态创建,但是我没有将指定的值重新放回到表中。 Here is my starting table. 这是我的起始桌。

ORDER_ID    DSC_NAME        NAME
----------- --------------- -----------
2           34-1500-XXX     DWG_DOC
3           C0403           EQIP_1
4           C4054           EQIP_2
1           34-1500-013     PART
0           88-0000         PRCS

I run this SQL to generate my columns that I want in my pivot table 我运行此SQL以在我的数据透视表中生成我想要的列

DECLARE @cols AS NVARCHAR(MAX), @query  AS NVARCHAR(MAX);

select @cols = STUFF((SELECT distinct 
           ',' + QUOTENAME(NAME)
               FROM test
               FOR XML PATH(''), TYPE
               ).value('.', 'NVARCHAR(MAX)') 
               ,1,1,'')

This gives me the following output 这给了我以下输出

[DWG_DOC],[EQIP_1],[EQIP_2],[PART],[PRCS]

When I run the dynamic SQL for the Pivot Table 当我为数据透视表运行动态SQL时

SET @query = 'SELECT ' + @cols + ' from 
         (
            SELECT ORDER_ID,DSC_NAME
            FROM test
        ) x
        pivot 
        (
            MIN(ORDER_ID)
            for DSC_NAME in (' + @cols + ')
        ) p '

execute(@query)

I see this result... 我看到这个结果......

DWG_DOC     EQIP_1      EQIP_2      PART        PRCS
----------- ----------- ----------- ----------- -----------
NULL        NULL        NULL        NULL        NULL

I have tried several different options, but I not come up with a solution to why this is not working. 我已经尝试了几种不同的选择,但我没有想出解决方案,为什么这不起作用。

Desired Output would be where the column order is correct by the ORDER_ID 期望的输出将是ORDER_ID的列顺序正确的位置

PRCS       PART           DWG_DOC        EQIP_1    EQIP_2    
---------- -------------- -------------- --------- ---------
88-0000    34-1500-013    34-1500-XXX    C0403     C4054     

But this would also work my application as well. 但这也适用于我的应用程序。

DWG_DOC        EQIP_1    EQIP_2    PART           PRCS
-------------- --------- --------- -------------- -----------
34-1500-XXX    C0403     C4054     34-1500-013    88-0000

Remove the ORDER_ID from the selection, and select the column name : 从选择中删除ORDER_ID ,然后选择列name

SET @query = 'SELECT ' + @cols + ' from 
         (
            SELECT ORDER_ID, DSC_NAME -- <--- you didn't select the name here
            FROM test
        ) x
        pivot 
        (
            MIN(ORDER_ID)
            for DSC_NAME in (' + @cols + ')
        ) p '

And use MAX(DSC_Name) instead of MIN(ORDER_ID) . 并使用MAX(DSC_Name)代替MIN(ORDER_ID) Like this: 像这样:

SET @query = 'SELECT '+ @cols + ' from 
         (
            SELECT DSC_NAME, Name
            FROM test
        ) x
        pivot 
        (
            MAX(DSC_Name)
            for NAME in (' + @cols + ')
        ) p ';

SQL Fiddle Demo SQL小提琴演示

This will give you: 这会给你:

|     DWG_DOC | EQIP_1 | EQIP_2 |        PART |    PRCS |
---------------------------------------------------------
| 34-1500-XXX |  C0403 |  C4054 | 34-1500-013 | 88-0000 |

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

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