我是 Oracle 的新手。我有一个包含三列的 Oracle 表:serialno
, item_category
和 item_status
.在第三列中,行的值为 serviceable
, under_repair
或 condemned
.
我想使用 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
最佳答案
您可以在 COUNT 函数内使用 CASE 或 DECODE 语句。
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
关于sql - 同一列上的不同值计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18297185/