快捷搜索: 王者荣耀 脱发

Postgresql 动态统计某一列的某一值出现的次数

实例解析:

select to_char(log.date, yyyy-MM-dd HH24) as hour, log.exten, sum(case log.grade when 1 then 1 else 0 end) as "1", sum(case log.grade when 2 then 1 else 0 end) as "2", sum(case log.grade when 3 then 1 else 0 end) as "3", sum(case log.grade when 4 then 1 else 0 end) as "4", sum(case log.grade when 5 then 1 else 0 end) as "5", log.direction from iface_satisfaction_investigation as log where log.date >= 2017-08-03 00:00:00 and log.date < 2017-08-04 00:00:00 group by hour,log.exten,log.direction order by hour,log.exten,log.direction asc

to_char:用于查询时间格式化,to_char(log.date, yyyy-MM-dd HH24),大致的结果是:2017-08-03 13

sum():毫无疑问是用来计算总和的。

sum(case log.grade when 1 then 1 else 0 end)是计算什么呢

他的意思就是:

计算grade这个列的值为1的时候有多少行,后面的sum(……)就类推了。

其他的也没有什么好讲的了

经验分享 程序员 微信小程序 职场和发展