SELECT RIGHT(LEFT(a.JobOrders, 16), 3) as OrderSuffix, COUNT(1) as TotalCount
FROM Orders a, Status b
WHERE a.JobOrders = b.JobOrders
AND b.Status = 'Finished'
GROUP BY RIGHT(LEFT(a.JobOrders, 16), 3)
ORDER BY TotalCount Desc
Is there a way to include in that SELECT
query the TOP (1)
Date
and TIME
fields of table Status
where RIGHT(LEFT(a.JobOrders, 16), 3) = <3 letter value>
?
What I am currently doing is I get first the distinct values, OrderSuffix
. Put it on a datatable. For each row, I am sending an SQL where RIGHT(LEFT(a.JobOrders, 16), 3) = <3 letter value>
to get the TOP (1)
Date
and Time
.