So I am fairly new to SQL programming and have a questions. I wrote this query
Code:
SELECT EMP_ID, SUM(ESTIMATE_LINE_QTY * SERVICE_COST) AS totalSales
FROM scf_estimate, scf_estimate_line, scf_service
WHERE scf_estimate.ESTIMATE_ID = scf_estimate_line.ESTIMATE_ID
AND scf_service.SERVICE_ID = scf_estimate_line.SERVICE_ID
AND scf_estimate.ESTIMATE_ID IN
(
SELECT ESTIMATE_ID
FROM scf_estimate_line
WHERE INVOICE_ID IS NOT NULL
)
GROUP BY EMP_ID;
Which returns
Code:
EMP_ID totalSales
14 5972.2
15 60103.35
16 52888.5
22 8458.5
23 42228.8
I am now trying to figure out how to go about and get a result that only shows the MIN or MAX of the totalSales column. I imagine I have to subquery the table that results from the query above but I am not sure on how to do that. Any advice would be great.
Thanks
Chad
EDIT --------
So I tried this
Code:
SELECT d.EMP_ID, MAX(d.totalSales)
FROM
(
SELECT EMP_ID, SUM(ESTIMATE_LINE_QTY * SERVICE_COST) AS totalSales
FROM scf_estimate, scf_estimate_line, scf_service
WHERE scf_estimate.ESTIMATE_ID = scf_estimate_line.ESTIMATE_ID
AND scf_service.SERVICE_ID = scf_estimate_line.SERVICE_ID
AND scf_estimate.ESTIMATE_ID IN
(
SELECT ESTIMATE_ID
FROM scf_estimate_line
WHERE INVOICE_ID IS NOT NULL
)
GROUP BY EMP_ID
) AS d;
And it results in this
Code:
EMP_ID MAX(d.totalSales)
14 60103.35
As you can see it shows the correct totalSales value but not the correct EMP_ID
Any thoughts
Chad