简体   繁体   中英

I want to make row as column using pivot in SQL Server 2008 but columns have string values so can not apply aggregate function to it

I need to convert rows to column, but there is no numeric values as pivot function requires aggregate function to be apply while converting to columns from rows

This is my table in SQL Server:

在此处输入图片说明

Data needs to be retrieved like this:

在此处输入图片说明

I tried this query but it shows error

SELECT 
    title, price
FROM  
    (SELECT 
         Product_Attributes_String_Value, Attribute_ID_Name, Product_ID 
     FROM
         [Products].[dbo].[Product_Attributes] 
     WHERE 
         Product_ID = '20734381') AS Tab1
PIVOT  
    (Product_Attributes_String_Value 
         FOR Attribute_ID_Name IN (title, price)) AS Tab2 
ORDER BY 
    [Tab2].[price] 

No need for PIVOT

SELECT a.Product_ID, 
    a.Product_Attributes_String_Value as title, 
    b.Product_Attributes_Double_Value as price
FROM [Products].[dbo].[Product_Attributes] a
    LEFT JOIN [Products].[dbo].[Product_Attributes] b 
        ON a.Product_ID = b.Product_ID 
        AND b.Attribute_ID_Name = ‘price’
WHERE a.Product_ID = ‘20734381’ 
    AND a.Attribute_ID_Name = ‘title’

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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