Задать вопрос
Ответы пользователя по тегу Transact-SQL
  • Pivot. Как реализовать переворот нескольких строк в таблице?

    xvladimirov
    @xvladimirov Автор вопроса
    Нашел решение, если кому понадобится.
    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
    Ответ написан
    Комментировать