Pivot table for data per hour

空扰寡人 提交于 2019-12-01 09:40:20
Erwin Brandstetter
SELECT * FROM crosstab(
       'SELECT cola, EXTRACT(HOUR from colb) AS h, count(*) AS ct
        FROM   fooo
        GROUP  BY 1, 2
        ORDER  BY 1, 2'

       ,'SELECT g::float8 FROM generate_series(0,23) g'
   ) AS ct (cola text
      , h00 int, h01 int, h02 int, h03 int, h04 int, h05 int
      , h06 int, h07 int, h08 int, h09 int, h10 int, h11 int
      , h12 int, h13 int, h14 int, h15 int, h16 int, h17 int
      , h18 int, h19 int, h20 int, h21 int, h22 int, h23 int);

Asides:
You forgot the hour 0 / 24.
Using column names with leading character to obviate the need for double-quotes.

Details:

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!