A
A
Alexey Vladimirov2014-02-19 09:08:31
SQL
Alexey Vladimirov, 2014-02-19 09:08:31

Pivot. How can I reverse multiple rows in a table?

Good time Lord.
There is a table, like
84f706e223b859ac9b07726a517710a74accc36e
It needs to be flipped, so that the sync_table values ​​become row headers, without repeating, and date_max become columns.
Something like this:
b8ea9fe0aa49f3667495931d19e1a3ab3333d5ec
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

But in this example, only one row is reversed (max_time), but it is necessary that both max_time and min_time are reversed, but if I do another pivot, it turns out like this:
a573f50bf4b778320a07faa5fe48ca80ab194f5e
That is, it seems to be correct, but, for example, in this example, the line Cars there should be one, and NULL should not be repeated an incomprehensible number of times.
Here is the code how the last picture turned out
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

Please tell me what is wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Vladimirov, 2014-02-19
@xvladimirov

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 question

Ask a Question

731 491 924 answers to any question