有一个客户表。我想在一个查询中列出活动和非活动状态。我怎么能这样做?
SELECT count(*) as ACTIVE,
count(*) as INACTIVE
FROM V_CUSTOMER
WHERE STATUS='a' AND STATUS='i'
答案 0 :(得分:5)
我们可以使用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
除非有大量具有STATUS其他值的记录,否则不需要WHERE子句,在这种情况下使用OR
而不是AND
:
WHERE STATUS='a' OR STATUS='d'
答案 1 :(得分:4)
尝试使用group by:
SELECT count(*), STATUS FROM V_CUSTOMER
Where STATUS='a' OR STATUS='d'
GROUP BY STATUS
答案 2 :(得分:3)
SELECT count(decode(status,'a',1)) as ACTIVE,
count(decode(status,'d',1)) as DEACTIVE
FROM V_CUSTOMER
WHERE STATUS='a' or STATUS='d'
答案 3 :(得分:1)
我认为你需要这样的东西:
select Status, count(*) from V_Customer
where STATUS='a' or STATUS='d'
group by STATUS
这将为您提供每个状态的记录数。