有一张客户表。我想在一个查询中列出事件和非事件状态。我该怎么做?
SELECT count(*) as ACTIVE,
count(*) as INACTIVE
FROM V_CUSTOMER
WHERE STATUS='a' AND STATUS='i'
最佳答案
我们可以使用 CASE 语句来翻译 STATUS 的两个值:
SELECT
sum(case when STATUS = 'a' then 1 else 0 end) as ACTIVE
, sum(case when STATUS = 'd' then 1 else 0 end) as DEACTIVE
FROM V_CUSTOMER
不需要 WHERE 子句,除非有大量具有其他 STATUS 值的记录,在这种情况下使用 OR
而不是 AND
:
WHERE STATUS='a' OR STATUS='d'
关于oracle - 复杂的 SQL 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3234592/