Based on the fact that your previous question was tagged with sql server, I am guessing that you need sql server syntax.
In order to get the result, you will need to use both the UNPIVOT
and the PIVOT
functions. The unpivot will take the TCount
and Count
columns and convert them to rows and then the PIVOT
will take the dates
and convert them to columns.
If you know the values ahead of time then you can hard-code the query:
select *
from
(
select date, value, col
from yourtable
unpivot
(
value
for col in (tcount, count)
) unpiv
) src
pivot
(
max(value)
for date in ([2013-02-06], [2013-02-12],
[2013-02-21], [2013-02-27])
) piv;
See SQL Fiddle with Demo
However, if you have an unknown number of dates, then you will need dynamic SQL:
DECLARE @cols AS NVARCHAR(MAX),
@query AS NVARCHAR(MAX)
select @cols = STUFF((SELECT distinct ',' + QUOTENAME(convert(varchar(10), Date, 120))
from yourtable
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
set @query = 'SELECT col, ' + @cols + ' from
(
select convert(varchar(10), Date, 120) date,
value, col
from yourtable
unpivot
(
value
for col in (tcount, count)
) unpiv
) src
pivot
(
max(value)
for date in (' + @cols + ')
) p '
execute(@query)
See SQL Fiddle with Demo
The result of both is:
| COL | 2013-02-06 | 2013-02-12 | 2013-02-21 | 2013-02-27 |
--------------------------------------------------------------
| COUNT | 35 | 23 | 54 | 12 |
| TCOUNT | 3500 | 4000 | 1000 | 5000 |