繁体   English   中英

我想在SQL Server 2008中使用数据透视表将行作为列,但列具有字符串值,因此无法对其应用聚合函数

[英]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

我需要将行转换为列,但是没有数值,因为数据透视函数需要在从行转换为列时应用聚合函数

这是我在SQL Server中的表:

在此处输入图片说明

需要像这样检索数据:

在此处输入图片说明

我尝试了此查询,但显示错误

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] 

无需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’

暂无
暂无

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

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