mercredi 12 août 2015

How to do a table pivot in SQLite?

I have built a query returning published posts count by day and hour:

select
       day_id,
       case day_id
          when 0 then 'Sunday'
          when 1 then 'Monday'
          when 2 then 'Tuesday'
          when 3 then 'Wednesday'
          when 4 then 'Thursday'
          when 5 then 'Friday'
          else        'Saturday' 
      end as day,
      hour,
      post_published_count
from (
    select  
      cast(strftime('%w', datetime(p.timestamp, 'unixepoch')) as integer) as day_id               ,
      strftime('%H', datetime(p.timestamp, 'unixepoch'))                  as hour                 ,
      count(*)                                                            as post_published_count
    from 
      posts p
    group by
      day_id, hour
)

I get results like this:

DAY_ID | DAY    | HOUR  | POST_PUBLISHED_COUNT
-------+--------+-------+----------------------
0      | Sunday | 00    | 124
0      | Sunday | 01    | 187
...
0      | Sunday | 23    | 214
...

Now I'd like to pivot those results and obtain something like this:

DAY    | 00  | 01  | ... | 23
-------+-----+-----+ ... +-----
Sunday | 124 | 187 | ... | 214
-------+-----+-----+ ... +-----
...

Here is the query I tried:

select servdayofweek, "00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23"
from (
select 
  strftime('%w', datetime(p.timestamp, 'unixepoch')) pos,
   case cast (strftime('%w', datetime(p.timestamp, 'unixepoch')) as integer)
  when 0 then 'Sunday'
  when 1 then 'Monday'
  when 2 then 'Tuesday'
  when 3 then 'Wednesday'
  when 4 then 'Thursday'
  when 5 then 'Friday'
  else 'Saturday' end as servdayofweek,
  case strftime('%H', datetime(p.timestamp, 'unixepoch')) when '00' then count(*) else '' end as "00",
case strftime('%H', datetime(p.timestamp, 'unixepoch')) when '01' then count(*) else '' end as "01",
case strftime('%H', datetime(p.timestamp, 'unixepoch')) when '02' then count(*) else '' end as "02",
case strftime('%H', datetime(p.timestamp, 'unixepoch')) when '03' then count(*) else '' end as "03",
case strftime('%H', datetime(p.timestamp, 'unixepoch')) when '04' then count(*) else '' end as "04",
case strftime('%H', datetime(p.timestamp, 'unixepoch')) when '05' then count(*) else '' end as "05",
case strftime('%H', datetime(p.timestamp, 'unixepoch')) when '06' then count(*) else '' end as "06",
case strftime('%H', datetime(p.timestamp, 'unixepoch')) when '07' then count(*) else '' end as "07",
case strftime('%H', datetime(p.timestamp, 'unixepoch')) when '08' then count(*) else '' end as "08",
case strftime('%H', datetime(p.timestamp, 'unixepoch')) when '09' then count(*) else '' end as "09",
case strftime('%H', datetime(p.timestamp, 'unixepoch')) when '10' then count(*) else '' end as "10",
case strftime('%H', datetime(p.timestamp, 'unixepoch')) when '11' then count(*) else '' end as "11",
case strftime('%H', datetime(p.timestamp, 'unixepoch')) when '12' then count(*) else '' end as "12",
case strftime('%H', datetime(p.timestamp, 'unixepoch')) when '13' then count(*) else '' end as "13",
case strftime('%H', datetime(p.timestamp, 'unixepoch')) when '14' then count(*) else '' end as "14",
case strftime('%H', datetime(p.timestamp, 'unixepoch')) when '15' then count(*) else '' end as "15",
case strftime('%H', datetime(p.timestamp, 'unixepoch')) when '16' then count(*) else '' end as "16",
case strftime('%H', datetime(p.timestamp, 'unixepoch')) when '17' then count(*) else '' end as "17",
case strftime('%H', datetime(p.timestamp, 'unixepoch')) when '18' then count(*) else '' end as "18",
case strftime('%H', datetime(p.timestamp, 'unixepoch')) when '19' then count(*) else '' end as "19",
case strftime('%H', datetime(p.timestamp, 'unixepoch')) when '20' then count(*) else '' end as "20",
case strftime('%H', datetime(p.timestamp, 'unixepoch')) when '21' then count(*) else '' end as "21",
case strftime('%H', datetime(p.timestamp, 'unixepoch')) when '22' then count(*) else '' end as "22",
case strftime('%H', datetime(p.timestamp, 'unixepoch')) when '22' then count(*) else '' end as "23"
from posts p
group by servdayofweek
) t
order by pos

However it doesn't return the expected results.

I tried to turn group by servdayofweek into group by servdayofweek, "00" etc but I get this error:

misuse of aggregate: count():

How can I table pivot my data?

SQLite-jdbc-3.8.10.1

Aucun commentaire:

Enregistrer un commentaire