Transpose rows and columns without aggregate [closed]

倾然丶 夕夏残阳落幕 提交于 2019-12-03 20:48:47

What you are trying to do is known as a PIVOT. There are two ways that these can be done, either with a static pivot where you hard-code all of the values to transform or a dynamic pivot where the values were determined at execution.

Static Version (See SQL Fiddle with Demo):

select *
from 
(
  select dt, field1, col, value
  from yourTable
  unpivot
  (
    value for col in (col1, col2, col3)
  ) u
)x1
pivot
(
  min(value)
  for dt in ([2012-07-02], [2012-07-03], [2012-07-04])
) p

Dynamic Pivot (See SQL Fiddle with Demo):

DECLARE @colsUnpivot AS NVARCHAR(MAX),
    @query  AS NVARCHAR(MAX),
    @colsPivot as  NVARCHAR(MAX)

select @colsUnpivot = stuff((select ','+quotename(C.name)
         from sys.columns as C
         where C.object_id = object_id('yourTable') and
               C.name like 'col%'
         for xml path('')), 1, 1, '')

select @colsPivot = STUFF((SELECT distinct ', ' + QUOTENAME(convert(char(10), dt, 120))
                    from yourTable
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')

set @query = 
          'SELECT col, field1, ' + +@colsPivot+ '
           FROM
           (
              select dt, field1, col, value
              from yourTable
              unpivot
              (
                value for col in ('+ @colsUnpivot+')
              ) u
           )x1
           pivot
           (
             min(value)
             for dt in ('+@colsPivot+')
           ) p '

exec(@query)

The dynamic pivot is a great option when you do not know the number of items that you need to convert into columns. Both will yield the same results.

Edit #1, if you want the date columns in a specific order - desc, etc, then you will use the following to get the dates (See SQL Fiddle with Demo):

select @colsPivot = STUFF((SELECT ', ' + QUOTENAME(convert(char(10), dt, 120))
                    from yourTable
                    group by dt
                    ORDER by dt desc
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')
Rob Farley

You don't need to do it without aggregates, but you do need to use Dynamic SQL to do it. Search for "SQL Dynamic Pivot" and you'll find plenty of posts about it. Like: SQL Server dynamic PIVOT query?

But better would be to do this in the client, as SQL's clients prefer to know what columns are being produced ahead of time.

You must use SQL Server Pivot query for your question.

You Can Use Below Query :

CREATE TABLE mytable(date DATE, Field1 NVARCHAR(50), col1 NVARCHAR(30), col2 NVARCHAR(30),col3 NVARCHAR(30))
INSERT INTO mytable(date,Field1,col1,col2,col3)
VALUES('2012/07/02','Customer1','CL','DS','RT')
...

SELECT Col, Field1, [2012/07/02],[2012/07/03],[2012/07/04]
FROM (SELECT Field1, 'Col1' AS Col, Date, Col1 AS ColVal
      FROM MyTable

      UNION ALL

      SELECT Field1, 'Col2' , Date, Col2 AS ColVal
      FROM MyTable

      UNION ALL 

      SELECT Field1, 'Col3' , Date, Col3 AS Col
      FROM MyTable
      )AS P
PIVOT (MIN(Colval) FOR Date IN ([2012/07/02],[2012/07/03],[2012/07/04])) AS Pvt
ORDER BY Field1, Col
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!