2013-09-30 35 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。编辑。 –

相关问题