接到一个需求要求查询近七天每十分钟的下单量,要求显示格式如:
时间 | 下单量 |
---|---|
2020-11-12 12:10:00 | 3 |
2020-11-12 12:20:00 | 5 |
00分到9分59秒为一个时段,10分到19分59秒为一个时段。。。。
以下是实现代码(以下都是用greenplum数据库):
select to_char(create_time, 'yyyy-mm-dd hh24'), trunc(to_char(create_time, 'mi')::numeric*1.00/10), sum(order_qty)
from dwd.dwd_order_base_info_d
group by to_char(create_time, 'yyyy-mm-dd hh24'), trunc(to_char(create_time, 'mi')::numeric*1.00/10)
order by to_char(create_time, 'yyyy-mm-dd hh24'), trunc(to_char(create_time, 'mi')::numeric*1.00/10)
思路:先截取时间戳到小时部分按现实进行分组(有1到24小时),再截取分钟除以10取到分钟的第一位,按分钟的第一位进行分组(分别有0,1…6),先按小时进行分组再按分钟分钟这样就可以达到按每十分钟一个时段了
以下是存储过程实现:
CREATE OR REPLACE FUNCTION dm_dept_pd.f_dm_order_base_info_7days_10min(v_etl_date varchar,v_last_etl_date varchar,v_runtype bpchar,v_param varchar)
RETURNS text
LANGUAGE plpgsql
VOLATILE
AS $$
---------------------------------------------------------------------------------------------------------------------------
-- step2:查询近七天的数据放到临时表
drop table if exists tmp_order_base_info_7days;
create temp table tmp_order_base_info_7days
as
select create_time
,