Select 包含多个计数子查询的查询 (Netezza SQL)

Select query with multiple sub queries for counts (Netezza SQL)

我正在尝试创建一个在 3 个时间段获取计数的报告:上个月、去年那个月和年初至今。

我之前在切换 where 子句时使用了如下所示的 3 个单独的查询,但我希望能够将所有 3 个查询合并为一个查询。

我尝试过使用 case 语句,但似乎无法让它发挥作用。仅供参考 app_dateYYYY-MM-DD

Select count(application_id)
from application_data a
where to_char(app_date, 'YYYYMM' = to_char(current_date, 'YYYYMM')-1
--where to_char(app_date, 'YYYYMM' = to_char(current_date, 'YYYYMM')-101
--where to_char(app_date, 'YYYY') = to_char(current_date, 'YYYY') and to_char(app_date, 'YYYYMM') <> to_char(current_date, 'YYYYMM')

示例数据:

App_ID          App_date  
123519          2018-02-17  
123521          2018-02-18  
123522          2018-02-19  
123523          2018-02-23  
123518          2019-01-15  
123546          2019-02-21  
123547          2019-02-22  
123548          2019-02-15  
123542          2019-02-02  

期望的结果:

LastMonth      YTD       YoY  
4               5         4

我想你想要条件聚合:

Select sum(case when to_char(app_date, 'YYYYMM' = to_char(current_date, 'YYYYMM')-1 then 1 else 0 end),
       sum(case to_char(app_date, 'YYYYMM' = to_char(current_date, 'YYYYMM')-101 when then 1 else 0 end),
       sum(case when to_char(app_date, 'YYYY') = to_char(current_date, 'YYYY') and to_char(app_date, 'YYYYMM') <> to_char(current_date, 'YYYYMM') then 1 else 0 end)
from application_data a