计数以将多个列作为SQL查询返回
这是一个很新的概念,但现在 我写了以下内容,以使用下面的状态按日期统计所有记录计数以将多个列作为SQL查询返回,sql,count,Sql,Count,这是一个很新的概念,但现在 我写了以下内容,以使用下面的状态按日期统计所有记录 Select trunc(create_date_time) as "CREATED DATE", count(*) as Cancelled from TASK_HDR where WHSE = 102 and INVN_NEED_TYPE = 101 and STAT_CODE = 99 and trunc(create_date_time)>= add_months(trunc
Select trunc(create_date_time) as "CREATED DATE", count(*) as Cancelled
from TASK_HDR
where WHSE = 102 and INVN_NEED_TYPE = 101 and STAT_CODE = 99
and trunc(create_date_time)>= add_months(trunc(sysdate,'mm'),-1)
Group BY trunc(create_date_time)
order by trunc(create_date_time) asc;
Select trunc(create_date_time) as "CREATED DATE", count(*) as Released
from TASK_HDR
where WHSE = 102 and INVN_NEED_TYPE = 101 and STAT_CODE = 10
and trunc(create_date_time)>= add_months(trunc(sysdate,'mm'),-1)
Group BY trunc(create_date_time)
order by trunc(create_date_time) asc;
我要做的是创建一个查询,这样我就可以同时返回一个查询和结果
Date Outstanding Completed
04-JAN-21 1 10
05-JAN-21 2 12
06-JAN-21 7 15
08-JAN-21 1 8
11-JAN-21 4 7
01-FEB-21 3 3
02-FEB-21 6 1
04-FEB-21 6 0
非常感谢您的帮助,使用
case
表达式进行条件聚合:
使用
case
表达式进行条件聚合:
这很好用,对Noob问题表示歉意。:)没问题,我们都是来学习的@GeoffBird,请将其标记为答案,这样对将来的其他人会有帮助。此操作非常有效,对Noob问题表示歉意。:)没问题,我们都是来学习的@GeoffBird,请将其标记为答案,以便将来对其他人有所帮助用你正在使用的数据库标记你的问题。用你正在使用的数据库标记你的问题。
Select trunc(create_date_time) as "CREATED DATE",
sum(case when STAT_CODE = 99 then 1 else 0 end) as Cancelled,
sum(case when STAT_CODE = 10 then 1 else 0 end) as Released
from TASK_HDR
where WHSE = 102 and INVN_NEED_TYPE = 101 and STAT_CODE in (99, 10)
and trunc(create_date_time)>= add_months(trunc(sysdate,'mm'),-1)
Group BY trunc(create_date_time)
order by trunc(create_date_time) asc;