Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8674927
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:49:35+00:00 2026-06-12T19:49:35+00:00

I am in need of help fixing a query so it returns the right

  • 0

I am in need of help fixing a query so it returns the right number of rows. I have the table below with 12 rows per ProductID in it. What I would like is to turn these 12 rows to 1 row with 12 set columns for each Product ID.

the generated dynamic code only creates 1 row

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

The current code, only generates 1 row of data and ignores other ProductID’s in my table. I would appreciate some help to fix this issue. Thanks.

BELOW here is a table(think Excel without the column dividing lines. The Product ID field has values but there are no values in the PAID, LABEL, VALUE and UNIT columns. That is why they appear blank in the table.

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T19:49:37+00:00Added an answer on June 12, 2026 at 7:49 pm

    Exclude ProductID from the UNPIVOT column list and, correspondingly, exclude all ProductID* columns from the PIVOT column list. Also, include ProductID in the subquery’s 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
    

    UPDATE: Explanation for the above solution.

    In your query, the x subquery returns data like this:

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

    The UNPIVOT clause transforms them like so:

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

    And the SELECT clause concatenates the first two columns to return the following:

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

    That is the rowset that is ultimately being pivoted. Note that at this point there is no way for SQL Server to tell product A‘s attributes from those of product B. The PIVOT clause acts similar to GROUP BY, collapsing all values with identical new_col values into one by applying MAX() to them. If there was a non-pivoted column, rows would be split into groups based on the values in that column. But there’s no one in the above rowset and, as a result, all the rows are collapsed into one.

    What you need instead is to make your final-but-one rowset look something like this:

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

    That is, the ProductID column allows SQL Server to distinguish between PAID1 belonging to product A and that belonging to B.

    To obtain that rowset, you just need to exclude ProductID from the UNPIVOT clause and pull it alongside the unpivoted columns instead. And since ProductID is excluded from the UNPIVOT column list, all the ProductID1, ProductID2 etc. should also be excluded from the PIVOT one, which you can see done in the query above.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

need help in error in database pivot. i have table tamed table_score like below:
need help/guide for sql select query, I have 2 table stock and stock_history, in
I have two problems with the below code that I need help fixing: 1)
Need help with a query that I wrote: I have three tables Company id
I need some help in fixing the below issue. I had transaction blocks in
I have the following code. I need help fixing it such that the Category
need help here, been fixing for hours but to no avail. i will like
I'm fixing bugs for some application, and I need help understanding the following lines
need help regarding USSD Gateway. I have to develop an app, which will directly
I need some help fixing a cross-thread exception. I am using Invoke which usually

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.