简体   繁体   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

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: 这是我在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 无需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.

相关问题 当我不想使用聚合时,是否应该使用Pivot将SQL Server 2008行值转换为列名? - Should I use Pivot to translate SQL Server 2008 row values to column names when I don't want to use an aggregate? 将行转置为列-使用id列在SQL Server 2008中进行透视 - Transpose rows to columns - pivot in SQL Server 2008 using an id column "在 sql server 2008 中使用带有列和行总计的数据透视表" - Using pivot table with column and row totals in sql server 2008 如果没有可在T-SQL PIVOT中用作聚合函数的列怎么办? - What if I don't have a column I can use as an aggregate function in my T-SQL PIVOT? 在SQL中,如何计算列中的值的数量,然后将其旋转以使列成为行? - In SQL, how can I count the number of values in a column and then pivot it so the column becomes the row? 在SQL Server 2008中,如何编写查询以按组明智地获取前10条记录,而我想在单行列(如数据透视表)中显示 - In SQL Server 2008, how to write query to get top 10 records in group wise and I want to display that in single row column like pivot SQL Server 2008数据透视表聚合函数问题 - SQL Server 2008 Pivot table aggregate function issue 使用PIVOT的动态列,我需要使用这些动态列以及聚合函数来按月生成报表-SQL Server 2012 - Dynamic columns using PIVOT and I need to use those dynamic columns along with aggregate function to generate reports month wise -SQL Server 2012 使用SQL Server 2008 R2在数据透视表查询中的聚合函数 - Aggregate function within Pivot table query using SQL Server 2008 R2 在使用sql中的一行的值定义新列之后,我想要一个新列,该列将是这些列的总和 - After Defining new columns using values of a row in sql I want a new column that will be the summation of the columns
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM