2015年4月9日 天氣冷
Name Type Nullable Default Comments
------------ ------------- -------- ------- ----------
ID VARCHAR2(50) 消息id
SERVICE_ID VARCHAR2(20) Y 服務ID
REQ_TIME DATE Y 要求時間
INVOKE_TIME DATE Y 調用時間
STATUS CHAR(1) Y '0' 0:失敗,1:成功
RESP_TIME DATE Y 響應時間
USER_NAME VARCHAR2(20) Y 用戶名
SERVICE_TIME DATE Y 調用服務結束時間
DESCN VARCHAR2(256) Y 描寫
--方式1
select req_time,
sum(decode(status, '0', 1, 0)) fail,
sum(decode(status, '1', 1, 0)) success
from gw_log
group by req_time;
履行結果以下:
![]()
固然,用了decode()函數,那也能夠用case函數了。
--方式2
select distinct a.req_time, a.fail, b.success
from (select req_time,count(*) fail
from gw_log
where status = '0'
group by req_time) a
right join
(select req_time, count(*) success
from gw_log
where status = '1'
group by req_time) b on a.req_time = b.req_time
【參考:select * from (select * from emp) e cross join (select * from book) b】
履行結果以下:
count 無記錄未返回0, 由于有 group by 子句的.
如果是不分組(即沒有 Group By) 那是1定會返回1個 0 的.
要讓有 分組 的count返回 0 , 則需要使用外連接
--方式3
select *
from (select a.req_time, count(*) success
from gw_log a
where a.status = '1'
group by req_time
union
select b.req_time, count(*) fail
from gw_log b
where b.status = '0'
group by b.req_time) g
履行結果以下: