我有一个Oracle表,其数据如下所示:
ID BATCH STATUS
1 1 0
2 1 0
3 1 1
4 2 0
也就是说, ID 是主键,每个“批处理”会有多行,每行都会在 STATUS 列中显示状态代码。还有很多其他专栏,但这些是重要的专栏。
我需要编写一个查询,总结每个批次的状态代码; STATUS列中有三个可能的值,0,1和2,我希望输出看起来像这样:
BATCH STATUS0 STATUS1 STATUS2
1 2 1 0
2 1 0 0
这些数字将是重要的;对于批次1,有
对于批次2,有
有没有办法可以在一个查询中执行此操作,而无需为每个状态代码重写查询?即我可以轻松编写这样的查询,并运行三次:
SELECT batch, COUNT(status)
FROM table
WHERE status = 0
GROUP BY batch
我可以运行它,然后再次运行status = 1,再次运行status = 2,但我希望在一个查询中执行它。
如果它有所不同,除了 STATUS 列之外,还有另一个列,我可能想要用相同的方式进行总结 - 这是我不这样做的另一个原因想要在SELECT语句之后执行SELECT语句并合并所有结果。
答案 0 :(得分:6)
select batch
, count(case when status=1 then 1 end) status1
, count(case when status=2 then 1 end) status2
, count(case when status=3 then 1 end) status3
from table
group by batch;
这通常被称为“数据透视”查询,我写了一篇关于如何动态生成这些查询的文章on my blog。
使用DECODE的版本(特定于Oracle但不太详细):
select batch
, count(decode(status,1,1)) status1
, count(decode(status,2,1)) status2
, count(decode(status,3,1)) status3
from table
group by batch;
答案 1 :(得分:1)
select batch,
sum(select case when status = 0 then 1 else 0 end) status0,
sum(select case when status = 1 then 1 else 0 end) status1,
sum(select case when status = 2 then 1 else 0 end) status2
from table
group by batch
答案 2 :(得分:1)
select batch,
sum((decode(status,0,1,0)) status0,
sum((decode(status,1,1,0)) status1,
sum((decode(status,2,1,0)) status2,
from table
group by batch
答案 3 :(得分:1)
OP询问一种方法(SUM)相对于另一种方法(COUNT)是否有任何性能优势。在具有26K行的表上运行简单测试表明COUNT方法明显更快。 YMMV。
DECLARE
CURSOR B IS
select batch_id
FROM batch
WHERE ROWNUM < 2000;
v_t1 NUMBER;
v_t2 NUMBER;
v_c1 NUMBER;
v_c2 NUMBER;
v_opn INTEGER;
v_cls INTEGER;
v_btc VARCHAR2(100);
BEGIN
-- Loop using SUM
v_t1 := dbms_utility.get_time;
v_c1 := dbms_utility.get_cpu_time;
FOR R IN B LOOP
FOR R2 IN (SELECT batch_type_code
, SUM(decode(batch_status_code, 'CLOSED', 1, 0)) closed
, SUM(decode(batch_status_code, 'OPEN', 1, 0)) OPEN
, SUM(decode(batch_status_code, 'REWORK', 1, 0)) rework
FROM batch
GROUP BY batch_type_code) LOOP
v_opn := R2.open;
v_cls := R2.closed;
END LOOP;
END LOOP;
v_t2 := dbms_utility.get_time;
v_c2 := dbms_utility.get_cpu_time;
dbms_output.put_line('For loop using SUM:');
dbms_output.put_line('CPU seconds used: '||(v_c2 - v_c1)/100);
dbms_output.put_line('Elapsed time: '||(v_t2 - v_t1)/100);
-- Loop using COUNT
v_t1 := dbms_utility.get_time;
v_c1 := dbms_utility.get_cpu_time;
FOR R IN B LOOP
FOR R2 IN (SELECT batch_type_code
, COUNT(CASE WHEN batch_status_code = 'CLOSED' THEN 1 END) closed
, COUNT(CASE WHEN batch_status_code = 'OPEN' THEN 1 END) OPEN
, COUNT(CASE WHEN batch_status_code = 'REWORK' THEN 1 END) rework
FROM batch
GROUP BY batch_type_code) LOOP
v_opn := R2.open;
v_cls := R2.closed;
END LOOP;
END LOOP;
v_t2 := dbms_utility.get_time;
v_c2 := dbms_utility.get_cpu_time;
dbms_output.put_line('For loop using COUNT:');
dbms_output.put_line('CPU seconds used: '||(v_c2 - v_c1)/100);
dbms_output.put_line('Elapsed time: '||(v_t2 - v_t1)/100);
END;
/
这产生了以下输出:
For loop using SUM:
CPU seconds used: 40
Elapsed time: 40.09
For loop using COUNT:
CPU seconds used: 33.26
Elapsed time: 33.34
我重复测试了几次以消除缓存的任何影响。我还交换了select语句。结果全面相似。
编辑:这与我过去用a similar question回答的测试工具相同。