SQL Question

Vicken

Senior member
Oct 10, 1999
381
0
0
Trying to do an SQL statement, but I can't figure it out. I need to find which product has the highest overall sales and which has the lowest overall sales.

I have these columns to work from

PRODUCT
ProductID, ProductName

ORDER
OrderID, Itemid, ProductID, Price, QTY, Itemtotal

(Its from the Oracle test database)

(Where itemtotal is qty x price)

basically I need to find a the sum of itemtotal, and the min and max of it and reference that back to the product ID, but I have no clue how. All I know is I need two subqueries
 

Haircut

Platinum Member
Apr 23, 2000
2,248
0
0
There are a number of ways to acheive this, one way in Oracle is to use an analytic function (look them up in the help files if you don't know too much about them as they can be very handy)

The outer query is just to make sure we only get one row back here.

SELECT *
FROM
(SELECT FIRST_VALUE (productID) OVER (ORDER BY SUM(orde.ItemTotal)) Lowest_Sales
,FIRST_VALUE (productID) OVER (ORDER BY SUM(orde.ItemTotal) DESC) Highest_Sales
FROM Order orde
GROUP BY orde.ProductID
)
WHERE ROWNUM < 2

 

Vicken

Senior member
Oct 10, 1999
381
0
0
I need to do it using straight SQL, no SQLplus commands since we are not learning that yet.
 

chsh1ca

Golden Member
Feb 17, 2003
1,179
0
0
This should do it, if I'm not tooo rusty:
For Top Seller:
SELECT p.ProductID, p.ProductName, SUM(o.QTY) AS SALES FROM ORDER o, PRODUCT p WHERE o.ProductID = p.ProductID GROUP BY p.ProductID ORDER BY SALES DESC LIMIT 1

For Bottom Seller:
SELECT p.ProductID, p.ProductName, SUM(o.QTY) AS SALES FROM ORDER o, PRODUCT p WHERE o.ProductID = p.ProductID GROUP BY p.ProductID ORDER BY SALES ASC LIMIT 1

The LIMIT directive works with MySQL, and I believe most ANSI-SQL compliant DBs, tho I could be really off there. Most of my work was with MSSQL Server 2000 and MySQL/PostgreSQL.

An alternate way might be with a subquery:
SELECT p.ProductID, p.ProductName, (SELECT SUM(o.QTY) as SALES FROM ORDER o WHERE o.ProductID = p.ProductID) FROM PRODUCT p ORDER BY SALES DESC LIMIT 1
And so forth.
 

Vicken

Senior member
Oct 10, 1999
381
0
0
This is what I got so far.

I need to find the min and max of SUM(itemtot) this using subqueries only, straight ANSI SQL. 1 execution

SELECT product.prodid , descrip, SUM(itemtot)
FROM demo.item, demo.product
WHERE item.prodid = product.prodid
GROUP BY product.prodid, descrip


 

Vicken

Senior member
Oct 10, 1999
381
0
0
Several hours later.

1 SELECT product.prodid , descrip, SUM(itemtot)
2 FROM demo.item, demo.product
3 WHERE item.prodid = product.prodid
4 GROUP BY product.prodid, descrip
5 HAVING SUM(itemtot) =
6 (SELECT MIN("SUM(ITEMTOT)")
7 FROM
8 (SELECT product.prodid , descrip, SUM(itemtot)
9 FROM demo.item, demo.product
10 WHERE item.prodid = product.prodid
11 GROUP BY product.prodid, descrip))
12 OR SUM(itemtot) =
13 (SELECT MAX("SUM(ITEMTOT)")
14 FROM
15 (SELECT product.prodid , descrip, SUM(itemtot)
16 FROM demo.item, demo.product
17 WHERE item.prodid = product.prodid
18 GROUP BY product.prodid, descrip))
19 ORDER BY SUM(itemtot)

Heh, shows you why manufactuerers came up with extensions!
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |