Home » SQL & PL/SQL » SQL & PL/SQL » pb with SELECT SUM(X), SUM(Y) where X,Y = result of subquery
pb with SELECT SUM(X), SUM(Y) where X,Y = result of subquery [message #22267] Wed, 02 October 2002 04:54 Go to next message
Charlie Gautier
Messages: 2
Registered: October 2002
Junior Member
The following query :
SELECT SUM(NBANOMALIE2), SUM(NBANOMALIE4)
FROM
(SELECT COUNT(*) AS "NBANOMALIE4" FROM CPN A, ANOCPN B WHERE A.CODENS= '128' AND B.CODANO='4' AND A.NUMFOR = B.NUMFOR) ,
(SELECT COUNT(*) AS "NBANOMALIE2" FROM CPN A, ANOCPN B WHERE A.CODENS= '128' AND B.CODANO='2' AND A.NUMFOR = B.NUMFOR);

is NOT working under Oracle 8.1.7.2 although it works fine under 8.1.6. -
Result given under 8.1.6 :
SUM(NBANOMALIE2) SUM(NBANOMALIE4)
0 1 (correct)
Under 8.1.7.2 :
0 0 (bad)
if query changed to "SELECT SUM(NBANOMALIE4), SUM(NBANOMALIE2)" or if the 2 Select count(*) are swaped:
Under 8.1.7.2 :
1 1 (bad too !)

It seems that under 8.1.7.2 there is a mix between returned values, related with the order of subqueries ?

Has anybody a workaround ? any idea ?

Thanks for any help

Charlie jcgautier@ifrance.com
Re: pb with SELECT SUM(X), SUM(Y) where X,Y = result of subquery [message #22268 is a reply to message #22267] Wed, 02 October 2002 05:18 Go to previous messageGo to next message
Mike T
Messages: 32
Registered: August 2002
Member
Why not Group By:

SELECT B.CODANO, COUNT(*)
FROM CPN A, ANOCPN B
WHERE A.CODENS= '128' AND B.CODANO IN ('2','4') AND A.NUMFOR = B.NUMFOR
GROUP BY B.CODANO;

If you only want the counts, try Decode:

SELECT DECODE(B.CODANO,'2',1,0) NBANOMALIE2, DECODE(B.CODANO,'4',1,0) NBANOMALIE4
FROM CPN A, ANOCPN B
WHERE A.CODENS= '128' AND B.CODANO IN ('2','4') AND A.NUMFOR = B.NUMFOR;
Re: pb with SELECT SUM(X), SUM(Y) where X,Y = result of subquery [message #22305 is a reply to message #22267] Thu, 03 October 2002 06:07 Go to previous message
Charlie Gautier
Messages: 2
Registered: October 2002
Junior Member
the 2nd solution works fine !!

Thanks to Decode(), and you !

Thanks again

Charlie jcgautier@ifrance.com
Previous Topic: regarding sql*net configuration
Next Topic: Update by cursor
Goto Forum:
  


Current Time: Mon May 06 13:27:10 CDT 2024