根据某些列进行分组后如何计算总记录

时间:2020-10-17 12:01:43

标签: sql oracle group-by

Requirement

附加查询:

    select abc.acc_entry_status account_status,count(abc.acc_entry_status) count from (
    select xlasl.application_short_name short_name, 
           xlasl.application_name source,
           to_char(xtl.transaction_date,'YYYY-MM-DD') trans_date,
           xth.char1 event_type,
           xtl.application_id application_id, 
           xtl.event_id event_id, 
           xtl.line_number line_number,
           xth.transaction_number transaction_number,
           to_char(xth.creation_date,'YYYY-MM-DD') creation_date,
           aeh.gl_transfer_status_code gl_transfer_status,
           aeh.ACCOUNTING_ENTRY_STATUS_CODE acc_entry_status, 
            to_char(aeh.gl_transfer_date,'YYYY-MM-DD') gl_transfer_date,
           aeh.period_name period_name
    from xla_transaction_headers xth, 
         xla_transaction_lines xtl, 
         xla_ae_headers aeh,
         xla_subledgers_vl xlasl
    where xth.application_id=xtl.application_id
        and xth.event_id=xtl.event_id
        and xth.event_id=aeh.event_id
        and xlasl.application_id=xth.application_id
        and xlasl.application_type_code='C'
            and aeh.parent_ae_header_id is null
        and xlasl.application_name=:p_source
        and trunc(xth.creation_date)=to_date(Substr(:p_creation_date,1,10),'YYYY-MM-DD'))  abc
    group by acc_entry_status

`

1 个答案:

答案 0 :(得分:0)

从不FROM子句中使用逗号。 始终使用正确的,明确的,标准,可读的JOIN语法。您应该修复此查询,并为以后的查询学习正确的SQL语法。

您可以使用条件聚合:

with q as (
      <your query here, hopefully fixed to use proper syntax
     )
select sum(case when account_status = 'I' then count else 0 end) as Invalid,
       sum(case when account_status = 'F' then count else 0 end) as Failed,
       sum(count) as total   
from q
where account_status in ('I', 'F')