Sql 在oracle中是否可以将条件放在计数函数中?

Sql 在oracle中是否可以将条件放在计数函数中?,sql,oracle,aggregate-functions,Sql,Oracle,Aggregate Functions,我有一个带有creditAmount和terminal_ID字段的表,其中的值大于0,有些值小于0。我想计算大于0的行以及每条记录的终端id 我试过这个: SELECT terminal_ID, COUNT(to_number(credit_amount)) AS count FROM isevaHarmsReports1 where to_number(credit_amount) > 0 group by terminal_ID; 如果我执行上面的查询,它只给出贷方金额>0的用户

我有一个带有creditAmount和terminal_ID字段的表,其中的值大于0,有些值小于0。我想计算大于0的行以及每条记录的终端id

我试过这个:

SELECT terminal_ID, COUNT(to_number(credit_amount)) AS count 
FROM isevaHarmsReports1 where to_number(credit_amount) > 0 group by  terminal_ID;
如果我执行上面的查询,它只给出贷方金额>0的用户的计数和终端ID,而不是<0的

if credit amount is < 0 I want only terminal_id
if credit amount is > 0 I want only terminal_id and count
如何根据条件计算行数?

您可以尝试以下方法:

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
试试这个

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在else null范围内,则case表达式传递1。聚合函数count不考虑空值。

您需要一个条件选择,因此我认为您必须使用存储过程。您使用的是\u numbercredit\u amount。这意味着您将此值存储为字符串。这是正确的,还是它真的被存储为一个数字?