简体   繁体   English

如何在T-SQL中将行转换为列,并将其写入临时表?

[英]How to convert rows to column in T-SQL, and write it in a temp table?

This is a question maybe already asked. 这可能是一个问题。

My query is 我的疑问是

SELECT Year, Month, Line, SUM(value) as total FROM myTable

I've the following query result table: 我有以下查询结果表:

Year  Month   Line     Total
-------------------------------------------
2011     2      B1     5203510.00
2011     3      B1     2228850.00
2011     4      B1     7258075.00
2011     5      B1     6305370.00
2011     6      B1     5540180.00
2011     7      B1     7624430.00
2011     8      B1     4042300.00
2011     9      B1     3308870.00
2011    10      B1     4983875.00
2011    11      B1     4636500.00
2011    12      B1     3987350.00
2012     1      B1      518400.00

I would like the following: 我想要以下内容:

Year Line  Jan    Feb   Mar   Apr ..... December

2011 B1      0    52035  2228 725 ..... 3987350
2012 B1     51840 ... ... .... 

Please, can you help me how to translate query SQL from rows to columns? 请问,您能帮我解决如何将查询SQL从行转换为列的问题吗?

Essentially, you need to PIVOT your data. 基本上,您需要PIVOT数据。 There are several examples on SO on how to do this. 有关如何执行此操作的SO有几个示例。 The tricky part is to convert the month number to a month name . 棘手的部分是将month number转换为month name

This is accomplished in the example with DATENAME(month, DateAdd(month, [Month], 0)-1) 这在DATENAME(month, DateAdd(month, [Month], 0)-1)的示例中完成DATENAME(month, DateAdd(month, [Month], 0)-1)

SQL Statement SQL语句

SELECT  *
FROM    ( 
          SELECT  Year, Line, Total, mnt = DATENAME(month, DateAdd(month, [Month], 0)-1)
          FROM    myTable
        ) mt          
PIVOT   (MAX(Total) FOR [mnt] IN ([January],[February],[March],[April],[May],[June],[July],[August],[September],[October],[November],[December])) AS PVT

Test script 测试脚本

;WITH myTable AS (
  SELECT * FROM (VALUES
    (2011     , 2      , 'B1',      5203510.00)
    , (2011     , 3      , 'B1',      2228850.00)
    , (2011     , 4      , 'B1',      7258075.00)
    , (2011     , 5      , 'B1',      6305370.00)
    , (2011     , 6      , 'B1',      5540180.00)
    , (2011     , 7      , 'B1',      7624430.00)
    , (2011     , 8      , 'B1',      4042300.00)
    , (2011     , 9      , 'B1',      3308870.00)
    , (2011    , 10      , 'B1',      4983875.00)
    , (2011    , 11      , 'B1',      4636500.00)
    , (2011    , 12      , 'B1',      3987350.00)
    , (2012     , 1      , 'B1',       518400.00)
  ) AS myTable (Year, Month, Line, Total)
)  
SELECT  *
FROM    ( 
          SELECT  Year, Line, Total, mnt = DATENAME(month, DateAdd(month, [Month], 0)-1)
          FROM    myTable
        ) mt          
PIVOT   (MAX(Total) FOR [mnt] IN ([January],[February],[March],[April],[May],[June],[July],[August],[September],[October],[November],[December])) AS PVT

What you're trying to do is pivot the data. 你要做的是转动数据。 I will just use the Month and Total (relevant) columns. 我将只使用Month和Total(相关)列。

If you're using MS SQL 2008 or up: 如果您使用的是MS SQL 2008或更高版本:

SELECT [1] AS Jan, [2] AS Feb, .. [12] AS Dec,
       Total
FROM ( SELECT Month, Total FROM tableA ) AS SOURCE
PIVOT
( MAX(Total) AS Total
  FOR
  Month IN ([1],[2],...[12]) ) AS PIVOT

PIVOT是您寻求的T-SQL关键字。

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

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