Sql 同一列上的不同值计数
我是甲骨文的新手。我有一个包含三列的Oracle表:Sql 同一列上的不同值计数,sql,oracle,pivot-table,Sql,Oracle,Pivot Table,我是甲骨文的新手。我有一个包含三列的Oracle表:serialno、item\u category和item\u status。在第三列中,行的值为可维修,处于维修或已报废 我想使用count来运行查询,以显示有多少是可用的,有多少是在修复中的,有多少是针对每个项目类别的 我想运行类似以下内容: select item_category , count(......) "total" , count (.....) "serviceable" , count(.....)"unde
serialno
、item\u category
和item\u status
。在第三列中,行的值为可维修
,处于维修
或已报废
我想使用count来运行查询,以显示有多少是可用的,有多少是在修复中的,有多少是针对每个项目类别的
我想运行类似以下内容:
select item_category
, count(......) "total"
, count (.....) "serviceable"
, count(.....)"under_repair"
, count(....) "condemned"
from my_table
group by item_category ......
我无法在计数内运行内部查询
我希望结果集是这样的:
item_category total serviceable under repair condemned
============= ===== ============ ============ ===========
chair 18 10 5 3
table 12 6 3 3
这是一个非常基本的“分组依据”查询。如果你搜索它,你会发现它是如何使用的
对于您的具体情况,您需要:
select item_category, item_status, count(*)
from <your table>
group by item_category, item_status;
根据您的需要更改列顺序您可以在COUNT函数中使用用例或解码语句
SELECT item_category,
COUNT (*) total,
COUNT (DECODE (item_status, 'serviceable', 1)) AS serviceable,
COUNT (DECODE (item_status, 'under_repair', 1)) AS under_repair,
COUNT (DECODE (item_status, 'condemned', 1)) AS condemned
FROM mytable
GROUP BY item_category;
输出:
ITEM_CATEGORY TOTAL SERVICEABLE UNDER_REPAIR CONDEMNED
----------------------------------------------------------------
chair 5 1 2 2
table 5 3 1 1
我有上升的倾向,所以当我忘记怎么做的时候,我有一个很容易找到的例子
核心条款在11g中是新的。因为那是5年多以前的事了,我希望你正在使用它
样本数据
create table t
(
serialno number(2,0),
item_category varchar2(30),
item_status varchar2(20)
);
insert into t ( serialno, item_category, item_status )
select
rownum serialno,
( case
when rownum <= 12 then 'table'
else 'chair'
end ) item_category,
( case
--table status
when rownum <= 12
and rownum <= 6
then 'servicable'
when rownum <= 12
and rownum between 7 and 9
then 'under_repair'
when rownum <= 12
and rownum > 9
then 'condemned'
--chair status
when rownum > 12
and rownum < 13 + 10
then 'servicable'
when rownum > 12
and rownum between 23 and 27
then 'under_repair'
when rownum > 12
and rownum > 27
then 'condemned'
end ) item_status
from
dual connect by level <= 30;
commit;
尽管如此,我还是更喜欢@Ramblin'Man的方法(除了用CASE代替DECODE)
编辑
刚意识到我漏掉了全栏。我不确定是否有办法使用PIVOT子句获得该列,也许其他人知道如何获得该列。也可能是我不经常使用它的原因 是的,你的方法是一个更好的解决方案。因为我写了这个答案,所以这个问题用一些示例输出进行了编辑。这个答案不再是最合适的。“兰布林”的那个看起来好多了,不错。顺便说一句,如果你在答案旁边打勾,你会得到2分
create table t
(
serialno number(2,0),
item_category varchar2(30),
item_status varchar2(20)
);
insert into t ( serialno, item_category, item_status )
select
rownum serialno,
( case
when rownum <= 12 then 'table'
else 'chair'
end ) item_category,
( case
--table status
when rownum <= 12
and rownum <= 6
then 'servicable'
when rownum <= 12
and rownum between 7 and 9
then 'under_repair'
when rownum <= 12
and rownum > 9
then 'condemned'
--chair status
when rownum > 12
and rownum < 13 + 10
then 'servicable'
when rownum > 12
and rownum between 23 and 27
then 'under_repair'
when rownum > 12
and rownum > 27
then 'condemned'
end ) item_status
from
dual connect by level <= 30;
commit;
select *
from
(
select
item_status stat,
item_category,
item_status
from t
)
pivot
(
count( item_status )
for stat in ( 'servicable' as "servicable", 'under_repair' as "under_repair", 'condemned' as "condemned" )
);
ITEM_CATEGORY servicable under_repair condemned
------------- ---------- ------------ ----------
chair 10 5 3
table 6 3 3