-2

我需要帮助修复查询,以便它返回正确的行数。我有下表,其中每个 ProductID 有 12 行。我想要将这 12 行转换为 1 行,每个产品 ID 有 12 个设置列。

生成的动态代码只创建 1 行

--Table this belongs to 

   Create table #Attributes
    (
            ProductID uniqueIdentifier,
            PAID Varchar(48), 
            Label nvarchar(50),
            AttrValue nvarchar(3072),
            unit nvarchar(50) 
    )
 . . . . . . .

select *
      from
      (
        select col + cast(rn as varchar(10)) new_col, val
        from 
        (
          select 
          Cast(PAID as NVarchar(3072)) PAID
          ,Cast (ProductID as NVarchar(3072)) ProductID
           ,Cast (Label  as NVarchar(3072)) Label
           ,Cast (Value as NVarchar(3072)) Value
           ,Cast (unit as NVarchar(3072))  unit  
            ,row_number() over(partition by ProductID order by ProductID) rn
          from #Attributes
        ) x
        unpivot
        (
          val
          for col in ([ProductID],[PAID],[Label],[Value],[unit])
        ) u
      ) x1
      pivot
      (
        max(val)
        for new_col in
          ([ProductID1],[PAID1],[Label1],[Value1],[unit1],[ProductID2],[PAID2],[Label2],[Value2],[unit2],[ProductID3],[PAID3],[Label3],[Value3],[unit3],[ProductID4],[PAID4],[Label4],[Value4],[unit4],[ProductID5],[PAID5],[Label5],[Value5],[unit5],[ProductID6],[PAID6],[Label6],[Value6],[unit6],[ProductID7],[PAID7],[Label7],[Value7],[unit7],[ProductID8],[PAID8],[Label8],[Value8],[unit8],[ProductID9],[PAID9],[Label9],[Value9],[unit9],[ProductID10],[PAID10],[Label10],[Value10],[unit10],[ProductID11],[PAID11],[Label11],[Value11],[unit11],[ProductID12],[PAID12],[Label12],[Value12],[unit12],[ProductID13],[PAID13],[Label13],[Value13],[unit13],[ProductID14],[PAID14],[Label14],[Value14],[unit14],[ProductID15],[PAID15],[Label15],[Value15],[unit15],[ProductID16],[PAID16],[Label16],[Value16],[unit16],[ProductID17],[PAID17],[Label17],[Value17],[unit17],[ProductID18],[PAID18],[Label18],[Value18],[unit18],[ProductID19],[PAID19],[Label19],[Value19],[unit19],[ProductID20],[PAID20],[Label20],[Value20],[unit20],[ProductID21],[PAID21],[Label21],[Value21],[unit21])
      ) p

当前代码仅生成 1 行数据并忽略我表中的其他 ProductID。我会很感激一些帮助来解决这个问题。谢谢。

下面是一个表格(想想没有列分隔线的 Excel。产品 ID 字段有值,但 PAID、LABEL、VALUE 和 UNIT 列中没有值。这就是它们在表中显示为空白的原因。

PRODUCTID                                  PAID       LABEL         VALUE        UNIT
--------------------------------------------------------------------------------------
F4D58DCE-8EED-40E3-BF4C-07349BEC0A3E                          
F4D58DCE-8EED-40E3-BF4C-07349BEC0A3E                          
F4D58DCE-8EED-40E3-BF4C-07349BEC0A3E                          
F4D58DCE-8EED-40E3-BF4C-07349BEC0A3E                          
F4D58DCE-8EED-40E3-BF4C-07349BEC0A3E                          
F4D58DCE-8EED-40E3-BF4C-07349BEC0A3E                          
F4D58DCE-8EED-40E3-BF4C-07349BEC0A3E                          
F4D58DCE-8EED-40E3-BF4C-07349BEC0A3E                          
F4D58DCE-8EED-40E3-BF4C-07349BEC0A3E                          
F4D58DCE-8EED-40E3-BF4C-07349BEC0A3E                          
F4D58DCE-8EED-40E3-BF4C-07349BEC0A3E                          
F4D58DCE-8EED-40E3-BF4C-07349BEC0A3E                          
F4D58DCE-8EED-40E3-BF4C-07349BEC0A3E                          
F4D58DCE-8EED-40E3-BF4C-07349BEC0A3E                          
F4D58DCE-8EED-40E3-BF4C-07349BEC0A3E                          
F4D58DCE-8EED-40E3-BF4C-07349BEC0A3E                          
F4D58DCE-8EED-40E3-BF4C-07349BEC0A3E                          
F4D58DCE-8EED-40E3-BF4C-07349BEC0A3E                          
F4D58DCE-8EED-40E3-BF4C-07349BEC0A3E                          
F4D58DCE-8EED-40E3-BF4C-07349BEC0A3E                          
F4D58DCE-8EED-40E3-BF4C-07349BEC0A3E                          
ACF57CF7-7206-46F5-A341-16E1B9828DBC                          
ACF57CF7-7206-46F5-A341-16E1B9828DBC                          
ACF57CF7-7206-46F5-A341-16E1B9828DBC                          
ACF57CF7-7206-46F5-A341-16E1B9828DBC                          
ACF57CF7-7206-46F5-A341-16E1B9828DBC                          
ACF57CF7-7206-46F5-A341-16E1B9828DBC                          
ACF57CF7-7206-46F5-A341-16E1B9828DBC                          
ACF57CF7-7206-46F5-A341-16E1B9828DBC                          
ACF57CF7-7206-46F5-A341-16E1B9828DBC                          
ACF57CF7-7206-46F5-A341-16E1B9828DBC                          
ACF57CF7-7206-46F5-A341-16E1B9828DBC                          
ACF57CF7-7206-46F5-A341-16E1B9828DBC                          
ACF57CF7-7206-46F5-A341-16E1B9828DBC                          
ACF57CF7-7206-46F5-A341-16E1B9828DBC                          
ACF57CF7-7206-46F5-A341-16E1B9828DBC                          
ACF57CF7-7206-46F5-A341-16E1B9828DBC                          
ACF57CF7-7206-46F5-A341-16E1B9828DBC                          
ACF57CF7-7206-46F5-A341-16E1B9828DBC                          
ACF57CF7-7206-46F5-A341-16E1B9828DBC                          
ACF57CF7-7206-46F5-A341-16E1B9828DBC                          
ACF57CF7-7206-46F5-A341-16E1B9828DBC                          
95CF8634-DF1C-4E12-9584-56D726F9D3FD                          
95CF8634-DF1C-4E12-9584-56D726F9D3FD                          
95CF8634-DF1C-4E12-9584-56D726F9D3FD                          
95CF8634-DF1C-4E12-9584-56D726F9D3FD                          
95CF8634-DF1C-4E12-9584-56D726F9D3FD                          
95CF8634-DF1C-4E12-9584-56D726F9D3FD                          
95CF8634-DF1C-4E12-9584-56D726F9D3FD                          
95CF8634-DF1C-4E12-9584-56D726F9D3FD                          
95CF8634-DF1C-4E12-9584-56D726F9D3FD                          
95CF8634-DF1C-4E12-9584-56D726F9D3FD                          
95CF8634-DF1C-4E12-9584-56D726F9D3FD                          
95CF8634-DF1C-4E12-9584-56D726F9D3FD                          
95CF8634-DF1C-4E12-9584-56D726F9D3FD                          
95CF8634-DF1C-4E12-9584-56D726F9D3FD                          
95CF8634-DF1C-4E12-9584-56D726F9D3FD                          
95CF8634-DF1C-4E12-9584-56D726F9D3FD                          
95CF8634-DF1C-4E12-9584-56D726F9D3FD                          
95CF8634-DF1C-4E12-9584-56D726F9D3FD                          
95CF8634-DF1C-4E12-9584-56D726F9D3FD                          
95CF8634-DF1C-4E12-9584-56D726F9D3FD                          
95CF8634-DF1C-4E12-9584-56D726F9D3FD                          
C4196FB0-AAE1-4BC4-A6E3-630B90D249C1                          
C4196FB0-AAE1-4BC4-A6E3-630B90D249C1                          
C4196FB0-AAE1-4BC4-A6E3-630B90D249C1                          
C4196FB0-AAE1-4BC4-A6E3-630B90D249C1                          
C4196FB0-AAE1-4BC4-A6E3-630B90D249C1                          
C4196FB0-AAE1-4BC4-A6E3-630B90D249C1                          
C4196FB0-AAE1-4BC4-A6E3-630B90D249C1                          
C4196FB0-AAE1-4BC4-A6E3-630B90D249C1                          
C4196FB0-AAE1-4BC4-A6E3-630B90D249C1                          
C4196FB0-AAE1-4BC4-A6E3-630B90D249C1                          
C4196FB0-AAE1-4BC4-A6E3-630B90D249C1                          
C4196FB0-AAE1-4BC4-A6E3-630B90D249C1                          
C4196FB0-AAE1-4BC4-A6E3-630B90D249C1                          
C4196FB0-AAE1-4BC4-A6E3-630B90D249C1                          
C4196FB0-AAE1-4BC4-A6E3-630B90D249C1                          
C4196FB0-AAE1-4BC4-A6E3-630B90D249C1                          
C4196FB0-AAE1-4BC4-A6E3-630B90D249C1                          
C4196FB0-AAE1-4BC4-A6E3-630B90D249C1                          
C4196FB0-AAE1-4BC4-A6E3-630B90D249C1                          
C4196FB0-AAE1-4BC4-A6E3-630B90D249C1                          
C4196FB0-AAE1-4BC4-A6E3-630B90D249C1                          
A5A24B87-E4E7-4282-8BB1-7413198A7D1A                          
A5A24B87-E4E7-4282-8BB1-7413198A7D1A                          
A5A24B87-E4E7-4282-8BB1-7413198A7D1A                          
A5A24B87-E4E7-4282-8BB1-7413198A7D1A                          
A5A24B87-E4E7-4282-8BB1-7413198A7D1A                          
A5A24B87-E4E7-4282-8BB1-7413198A7D1A                          
A5A24B87-E4E7-4282-8BB1-7413198A7D1A                          
A5A24B87-E4E7-4282-8BB1-7413198A7D1A                          
A5A24B87-E4E7-4282-8BB1-7413198A7D1A                          
A5A24B87-E4E7-4282-8BB1-7413198A7D1A                          
A5A24B87-E4E7-4282-8BB1-7413198A7D1A                          
A5A24B87-E4E7-4282-8BB1-7413198A7D1A                          
A5A24B87-E4E7-4282-8BB1-7413198A7D1A                          
A5A24B87-E4E7-4282-8BB1-7413198A7D1A                          
A5A24B87-E4E7-4282-8BB1-7413198A7D1A                          
A5A24B87-E4E7-4282-8BB1-7413198A7D1A                          
A5A24B87-E4E7-4282-8BB1-7413198A7D1A                          
A5A24B87-E4E7-4282-8BB1-7413198A7D1A                          
A5A24B87-E4E7-4282-8BB1-7413198A7D1A                          
A5A24B87-E4E7-4282-8BB1-7413198A7D1A                          
A5A24B87-E4E7-4282-8BB1-7413198A7D1A                          
7EECE72B-26C5-4306-9706-85E344243122                          
7EECE72B-26C5-4306-9706-85E344243122                          
7EECE72B-26C5-4306-9706-85E344243122                          
7EECE72B-26C5-4306-9706-85E344243122                          
7EECE72B-26C5-4306-9706-85E344243122                          
7EECE72B-26C5-4306-9706-85E344243122                          
7EECE72B-26C5-4306-9706-85E344243122                          
7EECE72B-26C5-4306-9706-85E344243122                          
7EECE72B-26C5-4306-9706-85E344243122                          
7EECE72B-26C5-4306-9706-85E344243122                          
7EECE72B-26C5-4306-9706-85E344243122                          
7EECE72B-26C5-4306-9706-85E344243122                          
7EECE72B-26C5-4306-9706-85E344243122                          
7EECE72B-26C5-4306-9706-85E344243122                          
7EECE72B-26C5-4306-9706-85E344243122                          
7EECE72B-26C5-4306-9706-85E344243122                          
7EECE72B-26C5-4306-9706-85E344243122                          
7EECE72B-26C5-4306-9706-85E344243122                          
7EECE72B-26C5-4306-9706-85E344243122                          
7EECE72B-26C5-4306-9706-85E344243122                          
7EECE72B-26C5-4306-9706-85E344243122                          
60B1AB86-E0EB-41AE-858C-AAD4744FD49C                          
60B1AB86-E0EB-41AE-858C-AAD4744FD49C                          
60B1AB86-E0EB-41AE-858C-AAD4744FD49C                          
60B1AB86-E0EB-41AE-858C-AAD4744FD49C                          
60B1AB86-E0EB-41AE-858C-AAD4744FD49C                          
60B1AB86-E0EB-41AE-858C-AAD4744FD49C                          
60B1AB86-E0EB-41AE-858C-AAD4744FD49C                          
60B1AB86-E0EB-41AE-858C-AAD4744FD49C                          
60B1AB86-E0EB-41AE-858C-AAD4744FD49C                          
60B1AB86-E0EB-41AE-858C-AAD4744FD49C                          
60B1AB86-E0EB-41AE-858C-AAD4744FD49C                          
60B1AB86-E0EB-41AE-858C-AAD4744FD49C                          
60B1AB86-E0EB-41AE-858C-AAD4744FD49C                          
60B1AB86-E0EB-41AE-858C-AAD4744FD49C                          
60B1AB86-E0EB-41AE-858C-AAD4744FD49C                          
60B1AB86-E0EB-41AE-858C-AAD4744FD49C                          
60B1AB86-E0EB-41AE-858C-AAD4744FD49C                          
60B1AB86-E0EB-41AE-858C-AAD4744FD49C                          
60B1AB86-E0EB-41AE-858C-AAD4744FD49C                          
60B1AB86-E0EB-41AE-858C-AAD4744FD49C                          
60B1AB86-E0EB-41AE-858C-AAD4744FD49C                          
44CDDEC6-4889-491B-A896-B719C5B9F3E4                          
44CDDEC6-4889-491B-A896-B719C5B9F3E4                          
44CDDEC6-4889-491B-A896-B719C5B9F3E4                          
44CDDEC6-4889-491B-A896-B719C5B9F3E4                          
44CDDEC6-4889-491B-A896-B719C5B9F3E4                          
44CDDEC6-4889-491B-A896-B719C5B9F3E4                          
44CDDEC6-4889-491B-A896-B719C5B9F3E4                          
44CDDEC6-4889-491B-A896-B719C5B9F3E4                          
44CDDEC6-4889-491B-A896-B719C5B9F3E4                          
44CDDEC6-4889-491B-A896-B719C5B9F3E4                          
44CDDEC6-4889-491B-A896-B719C5B9F3E4                          
44CDDEC6-4889-491B-A896-B719C5B9F3E4                          
44CDDEC6-4889-491B-A896-B719C5B9F3E4                          
44CDDEC6-4889-491B-A896-B719C5B9F3E4                          
44CDDEC6-4889-491B-A896-B719C5B9F3E4                          
44CDDEC6-4889-491B-A896-B719C5B9F3E4                          
44CDDEC6-4889-491B-A896-B719C5B9F3E4                          
44CDDEC6-4889-491B-A896-B719C5B9F3E4                          
44CDDEC6-4889-491B-A896-B719C5B9F3E4                          
44CDDEC6-4889-491B-A896-B719C5B9F3E4                          
44CDDEC6-4889-491B-A896-B719C5B9F3E4                          
3866BBA2-624A-43B7-A04D-DE8ADF5DF739                          
3866BBA2-624A-43B7-A04D-DE8ADF5DF739                          
3866BBA2-624A-43B7-A04D-DE8ADF5DF739                          
3866BBA2-624A-43B7-A04D-DE8ADF5DF739                          
3866BBA2-624A-43B7-A04D-DE8ADF5DF739                          
3866BBA2-624A-43B7-A04D-DE8ADF5DF739                          
3866BBA2-624A-43B7-A04D-DE8ADF5DF739                          
3866BBA2-624A-43B7-A04D-DE8ADF5DF739                          
3866BBA2-624A-43B7-A04D-DE8ADF5DF739                          
3866BBA2-624A-43B7-A04D-DE8ADF5DF739                          
3866BBA2-624A-43B7-A04D-DE8ADF5DF739                          
3866BBA2-624A-43B7-A04D-DE8ADF5DF739                          
3866BBA2-624A-43B7-A04D-DE8ADF5DF739                          
3866BBA2-624A-43B7-A04D-DE8ADF5DF739                          
3866BBA2-624A-43B7-A04D-DE8ADF5DF739                          
3866BBA2-624A-43B7-A04D-DE8ADF5DF739                          
3866BBA2-624A-43B7-A04D-DE8ADF5DF739                          
3866BBA2-624A-43B7-A04D-DE8ADF5DF739                          
3866BBA2-624A-43B7-A04D-DE8ADF5DF739                          
3866BBA2-624A-43B7-A04D-DE8ADF5DF739                          
3866BBA2-624A-43B7-A04D-DE8ADF5DF739                          
AA0A3639-2A36-4731-BF3F-F278D54A99C8                          
AA0A3639-2A36-4731-BF3F-F278D54A99C8                          
AA0A3639-2A36-4731-BF3F-F278D54A99C8                          
AA0A3639-2A36-4731-BF3F-F278D54A99C8                          
AA0A3639-2A36-4731-BF3F-F278D54A99C8                          
AA0A3639-2A36-4731-BF3F-F278D54A99C8                          
AA0A3639-2A36-4731-BF3F-F278D54A99C8                          
AA0A3639-2A36-4731-BF3F-F278D54A99C8                          
AA0A3639-2A36-4731-BF3F-F278D54A99C8                          
AA0A3639-2A36-4731-BF3F-F278D54A99C8                          
AA0A3639-2A36-4731-BF3F-F278D54A99C8                          
AA0A3639-2A36-4731-BF3F-F278D54A99C8                          
AA0A3639-2A36-4731-BF3F-F278D54A99C8                          
AA0A3639-2A36-4731-BF3F-F278D54A99C8                          
AA0A3639-2A36-4731-BF3F-F278D54A99C8                          
AA0A3639-2A36-4731-BF3F-F278D54A99C8                          
AA0A3639-2A36-4731-BF3F-F278D54A99C8                          
AA0A3639-2A36-4731-BF3F-F278D54A99C8                          
AA0A3639-2A36-4731-BF3F-F278D54A99C8                          
AA0A3639-2A36-4731-BF3F-F278D54A99C8                          
AA0A3639-2A36-4731-BF3F-F278D54A99C8                          
4

1 回答 1

1

ProductID从 UNPIVOT 列列表中排除,并相应ProductID*地从 PIVOT 列列表中排除所有列。此外,ProductID在子查询的 SELECT 中包含:

select *
      from
      (
        select [ProductID], col + cast(rn as varchar(10)) new_col, val
        from 
        (
          select 
          Cast(PAID as NVarchar(3072)) PAID
          ,Cast (ProductID as NVarchar(3072)) ProductID
           ,Cast (Label  as NVarchar(3072)) Label
           ,Cast (Value as NVarchar(3072)) Value
           ,Cast (unit as NVarchar(3072))  unit  
            ,row_number() over(partition by ProductID order by ProductID) rn
          from #Attributes
        ) x
        unpivot
        (
          val
          for col in ([ProductID],[PAID],[Label],[Value],[unit])
        ) u
      ) x1
      pivot
      (
        max(val)
        for new_col in
          ([ProductID1],[PAID1],[Label1],[Value1],[unit1],
           [ProductID2],[PAID2],[Label2],[Value2],[unit2],
           [ProductID3],[PAID3],[Label3],[Value3],[unit3],
           [ProductID4],[PAID4],[Label4],[Value4],[unit4],
           [ProductID5],[PAID5],[Label5],[Value5],[unit5],
           [ProductID6],[PAID6],[Label6],[Value6],[unit6],
           [ProductID7],[PAID7],[Label7],[Value7],[unit7],
           [ProductID8],[PAID8],[Label8],[Value8],[unit8],
           [ProductID9],[PAID9],[Label9],[Value9],[unit9],
           [ProductID10],[PAID10],[Label10],[Value10],[unit10],
           [ProductID11],[PAID11],[Label11],[Value11],[unit11],
           [ProductID12],[PAID12],[Label12],[Value12],[unit12],
           [ProductID13],[PAID13],[Label13],[Value13],[unit13],
           [ProductID14],[PAID14],[Label14],[Value14],[unit14],
           [ProductID15],[PAID15],[Label15],[Value15],[unit15],
           [ProductID16],[PAID16],[Label16],[Value16],[unit16],
           [ProductID17],[PAID17],[Label17],[Value17],[unit17],
           [ProductID18],[PAID18],[Label18],[Value18],[unit18],
           [ProductID19],[PAID19],[Label19],[Value19],[unit19],
           [ProductID20],[PAID20],[Label20],[Value20],[unit20],
           [ProductID21],[PAID21],[Label21],[Value21],[unit21])
      ) p

更新:上述解决方案的解释。

在您的查询中,x子查询返回如下数据:

ProductID  PAID   Label   Value   unit   rn
---------  -----  ------  ------  -----  ---
A          ...    ...     ...     ...    1
A          ...    ...     ...     ...    2
...        ...    ...     ...     ...    ...
B          ...    ...     ...     ...    1
...        ...    ...     ...     ...    ...

UNPIVOT子句将它们转换为:

rn   col        val
---  ---------  -----
1    ProductID  A
1    PAID       ...
1    Label      ...
1    Value      ...
1    unit       ...
2    ProductID  A
2    PAID       ...
2    Label      ...
2    Value      ...
2    unit       ...
...  ...        ...
1    ProductID  B
1    PAID       ...
1    Label      ...
1    Value      ...
1    unit       ...

SELECT 子句连接前两列以返回以下内容:

new_col     val
----------  -----
ProductID1  A
PAID1       ...
Label1      ...
Value1      ...
unit1       ...
ProductID2  A
PAID2       ...
Label2      ...
Value2      ...
unit2       ...
...         ...
ProductID1  B
PAID1       ...
Label1      ...
Value1      ...
unit1       ...
...         ...

那是最终被旋转的行集。请注意,此时 SQL Server 无法将产品A的属性与产品的属性区分开来B。PIVOT 子句的作用类似于 GROUP BY,new_col通过应用MAX()它们将具有相同值的所有值折叠为一个。如果存在非透视列,则将根据该列中的值将行分成组。但是上面的行集中没有一个,因此,所有行都折叠成一个。

相反,您需要的是使您的最终但一个行集看起来像这样:

ProductID  new_col  val
---------  -------  -----
A          PAID1    ...
A          Label1   ...
A          Value1   ...
A          unit1    ...
A          PAID2    ...
A          Label2   ...
A          Value2   ...
A          unit2    ...
...        ...      ...
B          PAID1    ...
B          Label1   ...
B          Value1   ...
B          unit1    ...
...        ...      ...

也就是说,该ProductID列允许 SQL Server 区分PAID1属于 productA和属于B.

要获得该行集,您只需ProductIDUNPIVOT子句中排除并将其拉到未透视的列旁边。并且由于ProductID从 UNPIVOT 列列表中排除,所有ProductID1ProductID2也应该从 PIVOT 中排除,您可以在上面的查询中看到。

于 2012-10-13T22:41:27.847 回答