我有一个包含两列的大表:
Bld_id
- 它有多个独特的公寓,因此Bld_id
可能会重复多次,具体取决于其中的公寓数量。
第二列是Appartment_Status
,它有四个可能的值:
所以我希望我的输出看起来像一个包含6列的表
Bld_id (unique)
Count(ACTIVE Status)
Count(NOT ACTIVE Status)
COUNT (NULL Status)
Count (blank Satus)
Count (Total statuses)
由所有独特的Bld_id
分组。
在一个列中显示以下两种状态的结果,名称为Count(No Status)
也是有益的。Count (NULL Status)
Count (blank Satus)
谢谢,
答案 0 :(得分:1)
让我们试试这个有趣的
with
--
-- Test case supplied
--
test(Building, Appartement, Status) as
(
select 1, 1, 'ACTIVE' from dual union all
select 1, 2, 'ACTIVE' from dual union all
select 1, 3, 'NOT ACTIVE' from dual union all
select 1, 4, 'BLANK' from dual union all
select 1, 5, NULL from dual union all
select 2, 1, 'ACTIVE' from dual union all
select 2, 2, 'BLANK' from dual union all
select 2, 3, 'NOT ACTIVE' from dual union all
select 2, 4, 'BLANK' from dual union all
select 2, 5, NULL from dual
)
--
-- SELECT statement
--
select Building,
sum(case when Status = 'ACTIVE' then 1 else 0 end) active,
sum(case when Status = 'NOT ACTIVE' then 1 else 0 end) NOT_active,
sum(case when Status = 'BLANK' then 1 else 0 end) Blanks,
sum(case when Status is null then 1 else 0 end) IS_NULLS,
sum(case when Status is null or status = 'BLANK' then 1 else 0 end) no_status
from test
group by building;
结果:
BUILDING ACTIVE NOT_ACTIVE BLANKS IS_NULLS NO_STATUS
---------- ---------- ---------- ---------- ---------- ----------
1 2 1 1 1 2
2 1 1 2 1 3
那是你在找什么?