简体   繁体   中英

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?

Essentially, you need to PIVOT your data. There are several examples on SO on how to do this. The tricky part is to convert the month number to a month name .

This is accomplished in the example with DATENAME(month, DateAdd(month, [Month], 0)-1)

SQL Statement

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.

If you're using MS SQL 2008 or up:

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关键字。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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