2013-09-30 37 views
2

我想寫一個sql,我想從一個具有不同狀態代碼的表中統計記錄。 我寫了這樣的從不同狀態代碼的單個表中計數記錄

select 
( 
    (
     select count(*) as "Entry" 
     from cn_grc_hdr hdr 
     where hdr.unit_code = '03' and 
      hdr.crt_dt > '12-may-2013' and 
      hdr.status = 'E' 
    ), 
    (
     select count(*) as "Authorised" 
     from cn_grc_hdr hdr 
     where hdr.unit_code = '03' and 
      hdr.crt_dt > '12-may-2013' and 
      hdr.status = 'A' 
    ) 
) 
from dual 

查詢當我執行此查詢它顯示了一個錯誤(Oracle SQL Developer中)

ORA-00907:缺少右括號00907. 00000 - 「缺少右括號「原因:行動:錯誤在行:5列:5

可能是我的格式是錯誤的。有人可以幫我寫這樣的查詢嗎?

+1

所以我不是100%肯定,但我認爲問題是,你有包裹在括號中的兩個子查詢。擺脫那些外括號。 –

+0

我除去外括號和從cn_grc_hdr HDR 其中hdr.unit_code = '03' 和 hdr.crt_dt> '12 -MAY-2013' 和 寫下面的查詢和 選擇 (SELECT COUNT(*) hdr.status = 'E')從cn_grc_hdr HDR 其中hdr.unit_code = '03' 和 hdr.crt_dt> '12 -MAY-2013' 和 HDR 「條目」 , (SELECT COUNT(*) .status ='A')「授權」 雙 是@neoistheone它工作。 – Ravi

+0

好的,我很高興我能幫上忙! –

回答

2

我已經重新編寫查詢

select DECODE(status, 'E', 'Entry', 'A', 'Authorised') as Status , count(*) 
FROM table 
where unit_code = '03' 
and crd_dt > to_date('12-May-2013', 'dd-MON-yyyy') 
and status in ('A', 'E') 
GROUP BY status; 
+0

執行它-------------> ORA-00923後:FROM關鍵字未找到預期 00923. 00000 - 「FROM關鍵字未找到預期」 *原因: *行動: 線路錯誤:29列:1 – Ravi

+1

我認爲你必須按狀態分組才能工作。 – dcp1986

+1

感謝dcp通知我的錯誤。忽略了sql。編輯。 –

相關問題