我有一个包含creditAmount
和terminal_ID
字段的表格,其中我的值为> 0且某些值为< 0.我想计算> 0的行和每条记录的terminal_id。
我试过了:
SELECT terminal_ID, COUNT(to_number(credit_amount)) AS count
FROM isevaHarmsReports1 where to_number(credit_amount) > 0 group by terminal_ID;
如果我执行上述查询,它只给出credit_amount>用户的count和terminal_ID。 0但不是哪个< 0
if credit amount is < 0 I want only terminal_id
if credit amount is > 0 I want only terminal_id and count
如何根据条件计算行?
答案 0 :(得分:3)
试试这个
select terminal_id,
count(case when credit_amount > 0 then 1 end) as count_gt0,
count(case when credit_amount < 0 then 1 end) as count_lt0
from your_table
group by terminal_id
如果credit_amount在其他case
范围内,1
表达式会null
。聚合函数count
不考虑空值。
答案 1 :(得分:0)
你可以尝试一下:
SELECT terminal_id, COUNT(*)
FROM isevaHarmsReports1
WHERE TO_NUMBER(creditAmount) > 0
GROUP BY terminal_id
UNION
SELECT terminal_id, NULL
FROM isevaHarmsReports1
WHERE TO_NUMBER(creditAmount) < 0
GROUP BY terminal_id