Here is your query cleaned up a bit:
SELECT wc.WorkCenterCode, p.Name, SUM(col.Quantity*col.ActualPrice) AS "Total Sales"
FROM WorkCenter wc INNER JOIN
Product p
ON wc.WorkCenterCode = p.WorkCenterCode INNER JOIN
CustomerOrderLine col
ON p.ProductID = col.ProductID
GROUP BY wc.WorkCenterCode, p.Name
ORDER BY "Total Sales" DESC
LIMIT 1
Note the important change from double quotes to single quotes. This is especially important for the order by
clause so the clause actually does something, rather than sorting by a constant. The addition of table aliases makes the query easier to read.
If you are using Visual Studio, you should use top
rather than limit
:
SELECT TOP 1 wc.WorkCenterCode, p.Name, SUM(col.Quantity*col.ActualPrice) AS "Total Sales"
FROM WorkCenter wc INNER JOIN
Product p
ON wc.WorkCenterCode = p.WorkCenterCode INNER JOIN
CustomerOrderLine col
ON p.ProductID = col.ProductID
GROUP BY wc.WorkCenterCode, p.Name
ORDER BY "Total Sales" DESC;
EDIT:
For one row per work center, use this as a subquery with row_number()
:
SELECT WorkCenterCode, Name, "Total Sales"
FROM (SELECT wc.WorkCenterCode, p.Name, SUM(col.Quantity*col.ActualPrice) AS "Total Sales",
row_number() over (partition by wc.WorkCenterCode order by SUM(col.Quantity*col.ActualPrice) desc) as seqnum
FROM WorkCenter wc INNER JOIN
Product p
ON wc.WorkCenterCode = p.WorkCenterCode INNER JOIN
CustomerOrderLine col
ON p.ProductID = col.ProductID
GROUP BY wc.WorkCenterCode, p.Name
) t
WHERE seqnum = 1
ORDER BY "Total Sales" DESC;