programming-examples/sql/Union/5. Write a query to make a report of which salesman produce the largest and smallest orders on each date and arranged the orders number in smallest to the largest number.sql

18 lines
677 B
MySQL
Raw Normal View History

2019-11-18 14:05:53 +01:00
/*5. Write a query to make a report of which salesman produce the largest and smallest orders on each date and arranged the
orders number in smallest to the largest number. */
SELECT a.salesman_id, name, ord_no, 'highest on', ord_date
FROM salesman a, orders b
WHERE a.salesman_id =b.salesman_id
AND b.purch_amt=
(SELECT MAX (purch_amt)
FROM orders c
WHERE c.ord_date = b.ord_date)
UNION
SELECT a.salesman_id, name, ord_no, 'lowest on', ord_date
FROM salesman a, orders b
WHERE a.salesman_id =b.salesman_id
AND b.purch_amt=
(SELECT MIN (purch_amt)
FROM orders c
WHERE c.ord_date = b.ord_date)
ORDER BY 3;