SQL Server PIVOT 列数据

SQL Server PIVOT Column Data(SQL Server PIVOT 列数据)
本文介绍了SQL Server PIVOT 列数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表格,数据如下:

I have a table with data as given below:

DATE            Price
----------      ------
31/12/2009  10
31/12/2009  11
31/12/2009  12
30/12/2009  20
30/12/2009  21
30/12/2009  22
29/12/2009  30
29/12/2009  32
29/12/2009  31

我想将这些数据转换如下:

I want to convert this data as given below:

31/12/2009  30/12/2009  29/12/2009
----------  ----------  ----------
10          10          10
11          11          11
12          12          12

但日期列中的值是动态的.所以,我不知道如何使用 SQL Server Pivot 来使用它.

But the values in the date column is dynamic. So, I dont know how to use this using SQL Server Pivot.

请告诉我如何获取这些数据.

Could you please let me know how to get this data.

以下是复制此场景的脚本:

Given below is the script to replicate this scenario:

CREATE TABLE TEMP(EffectiveDate DATETIME,Price INT)
INSERT INTO TEMP(EffectiveDate,Price)
SELECT GETDATE(),10
UNION ALL
SELECT GETDATE(),11
UNION ALL
SELECT GETDATE(),12
UNION ALL
SELECT GETDATE()-1,20
UNION ALL
SELECT GETDATE()-1,21
UNION ALL
SELECT GETDATE()-1,22
UNION ALL
SELECT GETDATE()-2,30
UNION ALL
SELECT GETDATE()-2,32
UNION ALL
SELECT GETDATE()-2,31

SELECT CONVERT(VARCHAR,EffectiveDATE,103) AS 'DATE',Price FROM Temp

提前致谢,

马赫什

推荐答案

好的,正如我提到的,你的数据没有意义,但也许这会有所帮助.

OK, as I mentioned, your data does not make sense, but maybe this can help.

创建动态数据透视表的唯一方法是创建动态 sql.

The only way to create a dynamic pivot, is by creating dynamic sql.

此外,PIVOT 要求您使用聚合函数 (SUM, AVG, COUNT).

Also, PIVOT requires that you use an Aggregate function (SUM, AVG, COUNT).

好的,看看能不能帮到你.

Ok, let see if this can help you.

CREATE TABLE #TEMP  (EffectiveDate DATETIME,Price INT) 
INSERT INTO #TEMP(EffectiveDate,Price) 
SELECT GETDATE(),10 
UNION ALL 
SELECT GETDATE(),11 
UNION ALL 
SELECT GETDATE(),12 
UNION ALL 
SELECT GETDATE()-1,20 
UNION ALL 
SELECT GETDATE()-1,21 
UNION ALL 
SELECT GETDATE()-1,22 
UNION ALL 
SELECT GETDATE()-2,30 
UNION ALL 
SELECT GETDATE()-2,32 
UNION ALL 
SELECT GETDATE()-2,31 

DECLARE @Cols VARCHAR(MAX)
SELECT @cols = COALESCE(@cols + ',[' + colName + ']',
                         '[' + colName + ']')
FROM    (
            SELECT DISTINCT 
                    CONVERT(VARCHAR,EffectiveDATE,103) colName
            FROM    #TEMP
        ) s
ORDER BY colName DESC

DECLARE @query VARCHAR(MAX)
SET @query = N'SELECT *
FROM
(SELECT CONVERT(VARCHAR,EffectiveDATE,103) AS ''DATE'',Price
            FROM    #TEMP) p
PIVOT
(
SUM(Price) FOR DATE IN
( '+
@cols +' )
) AS pvt' 

EXECUTE(@query)


DROP TABLE #TEMP

这篇关于SQL Server PIVOT 列数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Execute complex raw SQL query in EF6(在EF6中执行复杂的原始SQL查询)
SSIS: Model design issue causing duplications - can two fact tables be connected?(SSIS:模型设计问题导致重复-两个事实表可以连接吗?)
SQL Server Graph Database - shortest path using multiple edge types(SQL Server图形数据库-使用多种边类型的最短路径)
Invalid column name when using EF Core filtered includes(使用EF核心过滤包括时无效的列名)
How should make faster SQL Server filtering procedure with many parameters(如何让多参数的SQL Server过滤程序更快)
How can I generate an entity–relationship (ER) diagram of a database using Microsoft SQL Server Management Studio?(如何使用Microsoft SQL Server Management Studio生成数据库的实体关系(ER)图?)