简体   繁体   中英

ORA-00937: not a single-group group function for sum function

I am trying to sum up the COUNT(IHID.RSID_PROD_N) by IHID.CS_ID but facing a problem. How to solve it?

SELECT 
  IHID.CS_ID ,IHID.RSID_PROD_N,COUNT(IHID.RSID_PROD_N),
  RSPF.RSPF_PROD_N,COUNT(RSPF.RSPF_PROD_N),sum(COUNT(IHID.RSID_PROD_N))
from IHIH 
JOIN IHID
  ON ihih.rsih_invoice_n = ihid.rsih_invoice_n AND ihih.cs_id = ihid.cs_id 
JOIN RSPF 
  ON ihih.cs_id = rspf.cs_id AND ihid.rsid_prod_n=rspf.rspf_prod_n
WHERE rspf_desc LIKE '%SCISSOR LIFT'
GROUP BY IHID.CS_ID, IHID.RSID_PROD_N,RSPF.RSPF_PROD_N,IHID.CS_ID;

The table is something like this

16     SJIII4626    1      SJIII4626    1
16     SJIII4632    1      SJIII4632    1

I want 1+1=2 for 16

Its not grouping as you would like because of the unique values in IHID.RSID_PROD_N and RSPF.RSPF_PROD_N. Remove those columns and it will group as expected.

One option is to use your current query (almost unchanged) as a CTE, and then apply SUM to a COUNT which you couldn't have done in a nested manner. Something like this:

with your_current_query as
  -- removed nested SUM(COUNT)
  (select 
     ihid.cs_id,
     ihid.rsid_prod_n,
     rspf.rspf_prod_n,
     count(ihid.rsid_prod_n) cnt_rsid
     count(rspf.rspf_prod_n) cnt_rspf
   from ihih join ihid on ihih.rsih_invoice_n = ihid.rsih_invoice_n 
              and ihih.cs_id = ihid.cs_id 
             join rspf on ihih.cs_id = rspf.cs_id 
              and ihid.rsid_prod_n=rspf.rspf_prod_n
   where rspf_desc like '%SCISSOR LIFT'
   group by ihid.cs_id, 
            ihid.rsid_prod_n,
            rspf.rspf_prod_n
  )
select cs_id,
       rsid_prod_n,
       rspf_prod_n,
       cnt_rsid,
       cnt_rspf,
       sum(cnt_rsid) sum_cnt_rsid   --> this represents nested SUM(COUNT)
from your_current_query
group by cs_id,
         rsid_prod_n,
         rspf_prod_n,
         cnt_rsid,
         cnt_rspf;

I think you need analytic functions rather than aggregates here. Something like:

SELECT 
  IHID.CS_ID 
  ,IHID.RSID_PROD_N
  ,row_number() over (partition by IHID.CS_ID order by IHID.RSID_PROD_N) as IHID_RSID_PROD_N
  ,RSPF.RSPF_PROD_N
  ,row_number() over (partition by IHID.CS_ID order by RSPF.RSPF_PROD_N) as RSPF_RSPF_PROD_N
  ,COUNT(IHID.RSID_PROD_N) over (partition by IHID.CS_ID) as sum_count
from IHIH 
JOIN IHID
  ON ihih.rsih_invoice_n = ihid.rsih_invoice_n AND ihih.cs_id = ihid.cs_id 
JOIN RSPF 
  ON ihih.cs_id = rspf.cs_id AND ihid.rsid_prod_n=rspf.rspf_prod_n
WHERE rspf_desc LIKE '%SCISSOR LIFT'
;

Not entirely sure because your question lacks a complete test case.

If this answer isn't quite what you want please edit your question to provide table structures and sample input data together with required output derived from that data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM