Oracle 10G -- simple subquery ?

Below works OK for one record, but fails with
"single-row subquery returns more than one row"
when removing "and IDENTITY = 'ABC123'"

How can I get the attached by
changing my below SUPP_AGE_ANALYSIS_REP
table logic on Oracle 10G ?

----------------------------------------------------------------

SELECT
  distinct IDENTITY,
  PARTY_NAME,
--------------------------------
(
  SELECT
  sum(OPEN_DOM_AMOUNT) as OPEN_AMOUNT
  from SUPP_AGE_ANALYSIS_REP
  where RESULT_KEY = (select max(RESULT_KEY) from SUPP_AGE_ANALYSIS_REP)
  and INVOICE_DATE < SYSDATE - 30
  and IDENTITY = 'ABC123'
  group by IDENTITY
) as History,
--------------------------------
(
  SELECT
  sum(OPEN_DOM_AMOUNT) as OPEN_AMOUNT
  from SUPP_AGE_ANALYSIS_REP
  where RESULT_KEY = (select max(RESULT_KEY) from SUPP_AGE_ANALYSIS_REP)
  and INVOICE_DATE between SYSDATE - 30 and SYSDATE - 23
  and IDENTITY = 'ABC123'
  group by IDENTITY
) as WK_01,
--------------------------------
(
  SELECT
  sum(OPEN_DOM_AMOUNT) as OPEN_AMOUNT
  from SUPP_AGE_ANALYSIS_REP
  where RESULT_KEY = (select max(RESULT_KEY) from SUPP_AGE_ANALYSIS_REP)
  and INVOICE_DATE between SYSDATE - 6 and SYSDATE
  and IDENTITY = 'ABC123'
  group by IDENTITY
) as WK_02
-------------------------------
from SUPP_AGE_ANALYSIS_REP
where RESULT_KEY = (select max(RESULT_KEY) from SUPP_AGE_ANALYSIS_REP)
finance_teacherAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sdstuberCommented:
this might be functionally different but should be more efficient
if party_name and identity are 1-to-1


  SELECT identity,
         party_name,
         SUM(CASE WHEN invoice_date < SYSDATE - 30 THEN open_dom_amount END) AS history,
         SUM(CASE WHEN invoice_date BETWEEN SYSDATE - 30 AND SYSDATE - 23 THEN open_dom_amount END)
             AS wk_01,
         SUM(CASE WHEN invoice_date BETWEEN SYSDATE - 6 AND SYSDATE THEN open_dom_amount END)
             AS wk_02
    FROM supp_age_analysis_rep
   WHERE result_key = (SELECT MAX(result_key) FROM supp_age_analysis_rep)
GROUP BY identity, party_name
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sdstuberCommented:
this should be functionally identical to what you started with  but much more expensive than my previous post


SELECT DISTINCT
       identity,
       party_name,
       --------------------------------
       (  SELECT SUM(open_dom_amount) AS open_amount
            FROM supp_age_analysis_rep
           WHERE result_key = x.result_key AND invoice_date < SYSDATE - 30 AND identity = x.identity
        GROUP BY identity)
           AS history,
       --------------------------------
       (  SELECT SUM(open_dom_amount) AS open_amount
            FROM supp_age_analysis_rep
           WHERE result_key = x.result_key
             AND invoice_date BETWEEN SYSDATE - 30 AND SYSDATE - 23
             AND identity = x.identity
        GROUP BY identity)
           AS wk_01,
       --------------------------------
       (  SELECT SUM(open_dom_amount) AS open_amount
            FROM supp_age_analysis_rep
           WHERE result_key = x.result_key
             AND invoice_date BETWEEN SYSDATE - 6 AND SYSDATE
             AND identity = x.identity
        GROUP BY identity)
           AS wk_02
  -------------------------------
  FROM supp_age_analysis_rep x
 WHERE result_key = (SELECT MAX(result_key) FROM supp_age_analysis_rep)
0
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
The group by is superfluous in each of the correlated subqueries.
I would definitely prefer the first query of sdstuber.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Oracle Database

From novice to tech pro — start learning today.