mysql presto 函数收集
阅读原文时间:2023年07月11日阅读:2

格式化日期

presto: select  date_format(CURRENT_DATE - INTERVAL '1' month, '%Y-%m')

mysql:date_format(DATE_ADD(data_date,INTERVAL 1 MONTH ), '%Y-%m')

计算环比同比思路

环比:(本月-上月)/上月

1,先查出本月月份和值

2,再查出上月的值 利用 interval + '1' month 让月份增加一个月,即让上个月的值变成本月的值

3,通过left join 月份来计算 环比 这样相同月份的值就是 表1为本月  表2为上月 通过环比公司即可实现计算

with a as(select month(cast(createymd as date)) month,count(task_id) tjl
from hive.bdc_dwd.dw_fact_task_new_ss_daily
where acct_time=FORMAT_DATETIME(DATE_ADD('day',-1,CURRENT_TIMESTAMP),'yyyy-MM-dd')
and order_mode not in (2,3,6,7)
and par_order_id = 0
AND (from_cid in (149,192,199,121,126,127,130,197,191,198) or category1id in (4782,5892))
AND category1id not in(4,9,21,448,2107,5787,5857,5788,4788,4791,3064)
and createymd >= '2019-05-01'
and category1id IN (1234, 1310, 2186, 2207, 2495, 5044, 5435,2976,1394,5,749,6,
5892, 5415, 5892,2035,2730,3429,3430,4794, 4792, 4791, 4790, 4788, 5415,4782,
4017, 4016, 3849, 3514, 3513, 3512,3430, 2841, 2839, 2834, 2832, 2730,2093, 1342, 471, 12, 13,
5997, 5705, 5234, 5180, 5179, 5178, 5122, 4789, 4348, 4347, 4063, 4062, 3850, 3128, 2976, 2766, 2559, 2481, 2243, 1941, 1590, 1394, 1387, 1233, 1232, 749, 1213)
group by 1
order by 1),
b as(select month(cast(createymd as date)) + 1 month,count(task_id) tjl
from hive.bdc_dwd.dw_fact_task_new_ss_daily
where acct_time=FORMAT_DATETIME(DATE_ADD('day',-1,CURRENT_TIMESTAMP),'yyyy-MM-dd')
and order_mode not in (2,3,6,7)
and par_order_id = 0
AND (from_cid in (149,192,199,121,126,127,130,197,191,198) or category1id in (4782,5892))
AND category1id not in(4,9,21,448,2107,5787,5857,5788,4788,4791,3064)
and createymd >= '2019-05-01'
and category1id IN (1234, 1310, 2186, 2207, 2495, 5044, 5435,2976,1394,5,749,6,
5892, 5415, 5892,2035,2730,3429,3430,4794, 4792, 4791, 4790, 4788, 5415,4782,
4017, 4016, 3849, 3514, 3513, 3512,3430, 2841, 2839, 2834, 2832, 2730,2093, 1342, 471, 12, 13,
5997, 5705, 5234, 5180, 5179, 5178, 5122, 4789, 4348, 4347, 4063, 4062, 3850, 3128, 2976, 2766, 2559, 2481, 2243, 1941, 1590, 1394, 1387, 1233, 1232, 749, 1213)
group by month(cast(createymd as date)) + 1
order by 1)
select month,case when hb is null or hb = 0 then 0 else hb end from(select a.month,(a.tjl-b.tjl) *1.0000/b.tjl hb
from a left join b on a.month = b.month
order by 1)