所以我有一张名为Vouchers
的表:
ID_ENTITY -> Unique Key
ID_VOUCHER
VOUCHER_CODE
AMNT_VOUCHER_DENOM
ID_VOUCHER_STATUS
DAT_EXPIRE_DATE
我有一个计数查询:
select count(*),AMNT_VOUCHER_DENOM,ID_VOUCHER
from CUS_VOUCHERS
where ID_VOUCHER_STATUS = 0
and ID_VOUCHER = 1
and trunc(DAT_EXPIRE_DATE) >= trunc(sysdate)
group by AMNT_VOUCHER_DENOM,ID_VOUCHER;
结果:
-----------------------------------------
COUNT | AMNT_VOUCHER_DENOM | ID_VOUCHER
1 |100000 |1
| |
预期结果:
----------------------------------------
COUNT | AMNT_VOUCHER_DENOM | ID_VOUCHER
1 | 100000 |1
0 |150000 |1
0 |200000 |1
实际上我有很多AMNT_VOUCHER_DENOM
和ID_VOUCHER = 1
,但还有ID_VOUCHER_STATUS != 0
。
我该怎么办?
答案 0 :(得分:3)
删除ID_VOUCHER_STATUS上的过滤器,并使用case
语句对感兴趣的项目进行计数
select count(case when ID_VOUCHER_STATUS = 0 then 1 end) as no_of_sts_0
, AMNT_VOUCHER_DENOM,ID_VOUCHER
from CUS_VOUCHERS
where ID_VOUCHER = 1
and trunc(DAT_EXPIRE_DATE) >= trunc(sysdate)
group by AMNT_VOUCHER_DENOM,ID_VOUCHER;
答案 1 :(得分:0)
WHERE ID_VOUCHER_STATUS = 0 and ID_VOUCHER = 1
将排除所有AMNT_VOUCHER_DENOM
和ID_VOUCHER_STATUS != 0
。
因此您要执行:
select count(*),AMNT_VOUCHER_DENOM,ID_VOUCHER from CUS_VOUCHERS
where ID_VOUCHER = 1 and trunc(DAT_EXPIRE_DATE) >= trunc(sysdate)
group by AMNT_VOUCHER_DENOM,ID_VOUCHER;`