SQL查询修复查询所需的一些帮助,以便每个唯一ID返回一行

时间:2012-10-13 21:37:02

标签: sql tsql

我需要帮助修复查询,以便返回正确的行数。我有下表,每个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列中没有值。这就是它们在表中显示为空白的原因。 / p>

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                          

1 个答案:

答案 0 :(得分:1)

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

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()值的所有值折叠为一个。如果存在非旋转列,则会根据该列中的值将行拆分为组。但是上面的行集中没有人,因此, all 行被折叠成一行。

你需要的是让你的最终但是一个行集看起来像这样:

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的{​​{1}}和属于A的{​​{1}}。

要获取该行集,您只需要从B子句中排除ProductID,然后将其与不透明的列一起拉出来。由于UNPIVOT被排除在UNPIVOT列列表之外,所有ProductIDProductID1等也应从PIVOT列表中排除,您可以在上面的查询中看到。< / p>