如何在SQLite中进行表格透视?

时间:2015-08-12 11:01:40

标签: sql sqlite pivot

我已经构建了一个查询,按天和小时返回已发布的帖子:

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
)

我得到的结果如下:

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

现在我想调整这些结果,并获得类似的结果:

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

这是我试过的查询:

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

然而,它并没有返回预期的结果。

我尝试将group by servdayofweek转换为group by servdayofweek, "00" etc但我收到此错误:

  

滥用聚合:count():

我如何表格转移我的数据?

SQLite的-JDBC-3.8.10.1

1 个答案:

答案 0 :(得分:0)

尝试使用此查询,它使用sum进行条件聚合,我认为这是您想要的。我将日期函数移动到派生表,而不必重复它们。

select 
    pos,
    case pos
       when 0 then 'Sunday'
       when 1 then 'Monday'
       when 2 then 'Tuesday'
       when 3 then 'Wednesday'
       when 4 then 'Thursday'
       when 5 then 'Friday'
       when 6 then 'Saturday' 
    end as servdayofweek,
    sum(case h when '00' then 1 else 0 end) as "00",
    -- more rows here
    sum(case h when '23' then 1 else 0 end) as "23"
from (
    select 
       strftime('%w', timestamp) pos, 
       strftime('%H', timestamp) h, 
       * -- maybe this should be timestamp or some id?
    from posts
) p
group by pos
order by pos;