Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Query Help 2

Status
Not open for further replies.

fmrock

Programmer
Sep 5, 2006
510
US
I have the following query i am working on.

I have run the inner query with the union and it works fine, however, I am trying to group them together, and must have something wrong with the syntax.

I have tried severy variations with no luck. I even had some more cafine to see if that would help today.



Code:
Select 
	Group_Num, 
	Sum(U.Num_Accounts) "Num_Accounts", 
	Sum(U.Num_of_99053) "Num_of_99053",
	Sum(U.Charge_Amount) "Total_Of_Charges",
	Sum(U.Payment_Count) "Num_of_Payments",
	Sum(U.Payment_Amount) "Total_of_Payments"
from (
SELECT 
	Account.ACCTCPCODE "Group_Num", 
	count(Account.ACCTCODE) "Num_Accounts", 
	count(Charges.PRCODE) "Num_of_99053", 
	sum(Charges.PRAMOUNT) "Charge_Amount",
	0 "Payment_Count", 
	0 "Payment_Amount"
FROM   
	MEDACCOUNT MEDACCOUNT 
	JOIN MEDCHARGES MEDCHARGES ON ((Account.ACCTCPCODE=Charges.CPCODE) AND (Account.ACCTCODE=Charges.ACCOUNT) AND (Charges.TYPE='C') AND (Charges.SPLITFLAG IS  NULL)  AND (Charges.PRCODE='99053'))
WHERE 
	(Account.ACCTDEACTIVE IS  NULL)
	AND to_date(Account.ACCTLOGDATE,'j')>=TO_DATE ('01-09-2007 00:00:00', 'DD-MM-YYYY HH24:MI:SS')
	AND (Account.ACCTCPCODE = '155003')
GROUP BY
	Account.ACCTCPCODE
UNION ALL
SELECT 
	Account.ACCTCPCODE "Group_Num", 
	0 "Num_Accounts", 
	0 "Num_of_99053", 
	0 "Charge Amount",
	count(Charges_1.PRCODE) "Payment_Count", 
	sum(Charges_1.PRAMOUNT) "Payment_Amount"
FROM   
	MEDACCOUNT MEDACCOUNT 
	JOIN MEDCHARGES MEDCHARGES ON ((Account.ACCTCPCODE=Charges.CPCODE) AND (Account.ACCTCODE=Charges.ACCOUNT) AND (Charges.TYPE='C') AND (Charges.SPLITFLAG IS  NULL)  AND (Charges.PRCODE='99053'))
	JOIN MEDCHARGES Charges_1 ON ((Charges.CPCODE=Charges_1.CPCODE) AND (Charges.ACCOUNT=Charges_1.ACCOUNT) AND (Charges.SEQNO=Charges_1.APPLYSEQNO) AND (Charges_1.TYPE='P') AND (Charges_1.SPLITFLAG IS  NULL))
WHERE 
	(Account.ACCTDEACTIVE IS  NULL)
	AND to_date(Account.ACCTLOGDATE,'j')>=TO_DATE ('01-09-2007 00:00:00', 'DD-MM-YYYY HH24:MI:SS')
	AND (Account.ACCTCPCODE = '155003')
GROUP BY
	Account.ACCTCPCODE
) U
GROUP BY Group_Num
 
FMRock,

I don't benefit from caffeine, but a copy-and-paste of the error message would help. <grin>

[santa]Mufasa
(aka Dave of Sandy, Utah, USA)
[I provide low-cost, remote Database Administration services: www.dasages.com]
 
The error i am getting is

ERROR at line 44:
ORA-00904: "GROUP_NUM": invalid identifier
 
Yes, ,FM, my vote goes to Cmmrfrds for the correct answer..."Group_Num" (which you create in your in-line view) is not the same identifier as Group_Num to which you refer in your GROUP BY clause.

To fix it, I'd recommend your removing the double quotes in all references Group_Num or (less desirable), add quotes to Group_Num in the GROUP BY clause.

Let us know your findings.

[santa]Mufasa
(aka Dave of Sandy, Utah, USA)
[I provide low-cost, remote Database Administration services: www.dasages.com]
 
I will try this first thing in the morning when i get back to work. Thanks for all your help everyone.
 
Worked by taking off the double quotes
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top