sql SELECT sum(type) lineysum,to_char(createtime,'yyyy-MM-dd HH24') linextime,8 as type FROM ad_log WHERE to_char(createtime, 'yyyy-MM-dd') =to_char(now()::timestamp + '-3 day', 'yyyy-MM-dd') GROUP BY to_char(createtime,'yyyy-MM-dd HH24') ORDER BY linextime;
结果
如何生成完整24小时,没有则为0?
SELECT timeconfig.time
FROM
( SELECT to_char(generate_series(to_date('20200724','yyyyMMdd'), to_date('20200725','yyyyMMdd'), '1 hours') ,'yyyy-MM-dd HH24') AS time) AS timeconfig
这段sql实现这个功能
这个不是最优解
下面这个才是
SELECT time_bucket_gapfill('1 day', createtime, now() - INTERVAL '1 month', now()) AS day,
COALESCE(SUM(num),0),
COALESCE(SUM(profit),0)
FROM rp_espipledgelog
WHERE createtime > now () - INTERVAL '1 month'
GROUP BY day
ORDER BY day;
select 24条记录,分别存放时间,每个小时一条,然后用这些记录left join 你查询出来的结果。其实不一定非得写sql吧,在后台代码里把不存在的时间不上0就行了