Hi everyone! I have to take an SQL exam the next week and I have a problem with nested subqueries with oracle. The exercise say:
/* 11. Give routes for which the total monetary sales of products in the category 'Hand Ice' exceeded
total monetary sales of products in the category 'Scoop Ice'. */
The problem is that i'm trying to solve this without "WITH" clause. My version is:
/*
SELECT hvroutetemplate_nrid, sum(quantityprice)
FROM hvc_so0 a, hvc_hvvisitresult b, hvc_visitresultdetails c, hvc_ar0 d
WHERE a.so0_nrid = b.so0_nrid AND b.hvvisitresult_nrid = c.hvvisitresult_nrid AND d.ar0_nrid = c.ar0_nrid
AND upper(family) = 'HAND ICE'
GROUP BY hvroutetemplate_nrid
HAVING sum(quantityprice) > (SELECT sum(quantity*price)
FROM hvc_so0 a, hvc_hvvisitresult b, hvc_visitresultdetails c, hvc_ar0 d
WHERE a.so0_nrid = b.so0_nrid AND b.hvvisitresult_nrid = c.hvvisitresult_nrid AND d.ar0_nrid = c.ar0_nrid
AND upper(family) = 'SCOOP ICE'); */
My version return only 0 rows!! But i can't understand why...Do you have any ideas? It's really urgent. I thank you in advance for the help.
The solution using "WITH" clause is like:
/*
WITH
detailed_purchases AS(
SELECT a.family, a.price, b.quantity, d.hvroutetemplate_nrid
FROM hvc_ar0 a, hvc_visitresultdetails b, hvc_hvvisitresult c, hvc_so0 d
WHERE a.ar0_nrid = b.ar0_nrid AND b.hvvisitresult_nrid = c.hvvisitresult_nrid AND c.so0_nrid = d.so0_nrid)
SELECT DISTINCT hvroutetemplate_nrid
FROM detailed_purchases a
WHERE UPPER(family) = 'HAND ICE'
GROUP BY hvroutetemplate_nrid
HAVING SUM(pricequantity) > (SELECT SUM(pricequantity)
FROM detailed_purchases b
WHERE b.hvroutetemplate_nrid = a.hvroutetemplate_nrid
AND UPPER(b.family) = 'SCOOP ICE');
*/