Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
选择查询和计数的多个子查询(Netezza SQL)_Sql_Netezza - Fatal编程技术网

选择查询和计数的多个子查询(Netezza SQL)

选择查询和计数的多个子查询(Netezza SQL),sql,netezza,Sql,Netezza,我试图创建一个报告,在3个时间段获取计数:上个月、去年的那个月和今年至今 我以前在切换where子句时使用了3个单独的查询,如下所示,但我希望能够将所有3个查询合并到一个查询中 我尝试过案例陈述,但似乎无法实现。FYIapp_date是YYYY-MM-DD Select count(application_id) from application_data a where to_char(app_date, 'YYYYMM' = to_char(current_date, 'YYYYMM')-1

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

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

我尝试过案例陈述,但似乎无法实现。FYI
app_date
YYYY-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')
预期结果:

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
选择sum(当to_char(应用程序日期,'yyyyymm'=to_char(当前日期,'yyyyymm')时为1,否则为0结束),
总和(大小写到字符(应用程序日期'YYYYMM'=到字符(当前日期'yyyyymm')-101,当1或0结束时),
总和(当to_char(应用程序日期,'YYYY')=to_char(当前日期,'YYYY')和to_char(应用程序日期,'yyyyymm')至_char(当前日期,'yyyyyyymm')时,则为1,否则0结束)
从应用程序_数据a
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