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