我有一个包含错误代码和输入时间(以及其他内容)的表.
我需要的是一种方法来计算最后一小时具有相同错误代码(我选择)的行数,并将结果字符串错误代码.
SELECT COUNT(*) || ',' || error_code as amount_and_code
FROM my_table
WHERE error_code in (5001, 5002, 5003, 5004, 5005)
AND entry_date >= (SYSDATE - 1/24)
group by error_code;
Run Code Online (Sandbox Code Playgroud)
我得到了明显的结果
AMOUNT_AND_CODE
---------------
4,5001
1,5002
2,5005
Run Code Online (Sandbox Code Playgroud)
我的问题是:我怎样才能返回0,error_code未找到的值.
我想得到的是
AMOUNT_AND_CODE
---------------
4,5001
1,5002
0,5003
0,5004
2,5005
Run Code Online (Sandbox Code Playgroud)
有没有办法获得我正在寻找的输出?
非常感谢你的帮助,mod.
编辑:我没有包含所有错误代码的表.
Edit2:Oracle8i企业版8.1.7.4.0版
小智 5
你可以尝试这样的事情:
SQL> create table nnn(error_code varchar2(4), entry_date date);
Table created.
SQL> insert into nnn values (5001, sysdate);
1 row created.
SQL> insert into nnn values (5003, sysdate - 10);
1 row created.
SQL>
SQL> with tbl as
2 (select 5001 error_code from dual union all
3 select 5002 error_code from dual union all
4 select 5003 error_code from dual union all
5 select 5004 error_code from dual)
6 select count(nnn.error_code), tbl.error_code
7 from nnn, tbl
8 where nnn.error_code(+) = tbl.error_code
9 and entry_date(+) >= (SYSDATE - 1/24)
10 group by tbl.error_code;
COUNT(NNN.ERROR_CODE) ERROR_CODE
--------------------- ----------
0 5003
1 5001
0 5002
0 5004
SQL>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3863 次 |
| 最近记录: |