简体   繁体   English

SQL Server-将列转换为行

[英]SQL Server - Convert columns to rows

I have a table on which simple select gives out put like below 我有一张桌子,简单的选择如下所示

在此处输入图片说明

I want to write a select statement to output like below 我想写一条选择语句输出如下

在此处输入图片说明

Can someone help me... 有人能帮我吗...

Since you are basically rotating your current columns of Sale , Income and Profit into rows and then move the month values to columns, then you will want to first unpivot the current columns, then pivot the months. 由于您基本上是将“ Sale ,“ Income和“ Profit当前列旋转到行中,然后将month值移动到列中,因此您将要先取消透视当前列,然后透视月份。

Depending on your version of SQL Server there are a few ways that you can unpivot the data. 根据您的SQL Server版本,有几种方法可以取消数据透视。 You can use the UNPIVOT function or CROSS APPLY: 您可以使用UNPIVOT函数或交叉应用:

select month, type, value
from yourtable
cross apply
(
  select 'Sale', sale union all
  select 'Income', Income union all
  select 'Profit', Profit
) c (type, value)

See SQL Fiddle with Demo . 请参阅带有演示的SQL Fiddle This will convert your current data into: 这会将您当前的数据转换为:

| MONTH |   TYPE | VALUE |
|-------|--------|-------|
|   Jan |   Sale |   100 |
|   Jan | Income |    50 |
|   Jan | Profit |    10 |
|   Feb |   Sale |    20 |
|   Feb | Income |    40 |

Then you can use the PIVOT function to convert the months into your column headers. 然后,您可以使用PIVOT函数将月份转换为列标题。

select type, Jan, Feb, Mar, Apr
from
(
  select month, type, value
  from yourtable
  cross apply
  (
    select 'Sale', sale union all
    select 'Income', Income union all
    select 'Profit', Profit
  ) c (type, value)
) d
pivot
(
  sum(value)
  for month in (Jan, Feb, Mar, Apr)
) piv;

See SQL Fiddle with Demo . 请参阅带有演示的SQL Fiddle

if you have an unknown number of months, then you can use dynamic SQL: 如果您有未知的月份数,则可以使用动态SQL:

DECLARE @cols AS NVARCHAR(MAX),
    @query  AS NVARCHAR(MAX)

select @cols = STUFF((SELECT distinct N',' + QUOTENAME(Month) 
                    from yourtable
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')

set @query = N'SELECT type, ' + @cols + N' 
            from 
            (
                select month, type, value
                from yourtable
                cross apply
                (
                  select ''Sale'', sale union all
                  select ''Income'', Income union all
                  select ''Profit'', Profit
                ) c (type, value)
            ) x
            pivot 
            (
                sum(value)
                for month in (' + @cols + N')
            ) p '

execute sp_executesql @query;

See SQL Fiddle with Demo 参见带有演示的SQL Fiddle

You Can Use UNPIVOT then PIVOT 您可以先使用UNPIVOT然后再使用PIVOT
THE BEST IS TO DO QUERY EMBEDED SQL create DISTINCT COLUMS of months with STUFF function then replace FOR oMonth IN ([January-2013], [February-2013], [March-2013], [April-2013]) 最好的方法是执行嵌入SQL,用STUFF函数创建几个月的DISTINCT COLUMS,然后替换FOR oMonth IN([2013年1月],[2013年2月],[2013年3月],[2013年4月])
here core query 这里核心查询

 SELECT  
*
FROM
(  SELECT      
 oMonth, value,col
 from (
 select DATENAME(month,oDate)   + '-' + CAST(YEAR(  oDate) as varchar) as oMonth,        Sales ,Income,Profit
 FROM            SalesSource 
 )A
unpivot
 (
  value for col in ( Sales ,Income,Profit)
 ) u

 ) as sourceTable
 PIVOT
 (
 sum( value)  
 FOR oMonth IN ([January-2013], [February-2013], [March-2013], [April-2013])
 ) AS PivotTable;

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

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