Answer the question
In order to leave comments, you need to log in
Pivot. How can I reverse multiple rows in a table?
Good time Lord.
There is a table, like
It needs to be flipped, so that the sync_table values become row headers, without repeating, and date_max become columns.
Something like this:
Here is the code, how it turned out:
select sync_table,
[2014-02-14],[2014-02-15],[2014-02-16],[2014-02-17],[2014-02-18]
from
(
select
CAST([date] AS VARCHAR(24))+'' as [date_max]
, sync_table
, max(ms_duration) as max_time
from #t
group by [date], sync_table
having sync_table is not null
)x
PIVOT
(
MAX([max_time]) FOR [date_max] IN ([2014-02-14],[2014-02-15],[2014-02-16],[2014-02-17],[2014-02-18])
)xx
select sync_table,
[2014-02-14-MAX],[2014-02-15-MAX],[2014-02-16-MAX],[2014-02-17-MAX],[2014-02-18-MAX]
,[2014-02-14-MIN],[2014-02-15-MIN],[2014-02-16-MIN],[2014-02-17-MIN],[2014-02-18-MIN]
from
(
select
CAST([date] AS VARCHAR(24))+'-MAX' as [date_max]
, CAST([date] AS VARCHAR(24))+'-MIN' as [date_min]
, sync_table
, max(ms_duration) as max_time
, AVG(ms_duration) as avg_time
--, MIN(ms_duration) as min_time
from #t
group by [date], sync_table
having sync_table is not null
)x
PIVOT
(
MAX([max_time]) FOR [date_max] IN ([2014-02-14-MAX],[2014-02-15-MAX],[2014-02-16-MAX],[2014-02-17-MAX],[2014-02-18-MAX])
)xx
PIVOT
(
MAX([avg_time]) FOR [date_min] IN ([2014-02-14-MIN],[2014-02-15-MIN],[2014-02-16-MIN],[2014-02-17-MIN],[2014-02-18-MIN])
)xxx
Answer the question
In order to leave comments, you need to log in
Found a solution if anyone needs it.
select sync_table,[2014-02-14-MAX],[2014-02-15-MAX],[2014-02-16-MAX],[2014-02-17-MAX],[2014-02-18-MAX],[2014-02-14-MIN],[2014-02-15-MIN],[2014-02-16-MIN],[2014-02-17-MIN],[2014-02-18-MIN],[2014-02-14-AVG],[2014-02-15-AVG],[2014-02-16-AVG],[2014-02-17-AVG],[2014-02-18-AVG]
from
(
select * from(
select
cast([date] as varchar(12))+'-MAX' as date_
, sync_table
, MAX(ms_duration) as _time
from #t
group by [date], sync_table
having sync_table is not null
UNION ALL(
select
cast([date] as varchar(12))+'-AVG' as date_
, sync_table
, AVG(ms_duration) as _time
from #t
group by [date], sync_table
having sync_table is not null
)
UNION ALL(
select
cast([date] as varchar(12))+'-MIN' as date_
, sync_table
, MIN(ms_duration) as _time
from #t
group by [date], sync_table
having sync_table is not null
)
)united --order by sync_table, date_, _time
)x
PIVOT
(
MAX(_time) FOR date_ IN ([2014-02-14-MAX],[2014-02-15-MAX],[2014-02-16-MAX],[2014-02-17-MAX],[2014-02-18-MAX],[2014-02-14-MIN],[2014-02-15-MIN],[2014-02-16-MIN],[2014-02-17-MIN],[2014-02-18-MIN],[2014-02-14-AVG],[2014-02-15-AVG],[2014-02-16-AVG],[2014-02-17-AVG],[2014-02-18-AVG])
)xx
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question