I am using SQL Server 2005. I am attempting to find the last work order that was used for each rate category. I can find the date last used for a specific rate category, but am not able to get the ONE work_no associated with that date. The code below is still yielding multiple work orders for each category. Can anyone point me in the right direction? Thanks!
SELECT TOP (100) PERCENT dbo.rate_cat.rate_cat,
dbo.rate_cat.description,
max(dbo.[work].create_time) AS LastUseDate,
q.work_no
FROM dbo.[work]
LEFT OUTER JOIN dbo.rate_cat
ON dbo.[work].rate_cat = dbo.rate_cat.rate_cat
LEFT OUTER JOIN dbo.business
ON dbo.[work].bill_to_business_id = dbo.business.business_id
LEFT OUTER JOIN
(Select max(work_no) as work_no, create_time
From dbo.[work]
Group by create_time)q
ON work.create_time = q.create_time
and work.work_no = q.work_no
Group by dbo.rate_cat.rate_cat, dbo.rate_cat.description, q.work_no
ORDER BY dbo.rate_cat.rate_cat
SELECT TOP (100) PERCENT dbo.rate_cat.rate_cat,
dbo.rate_cat.description,
max(dbo.[work].create_time) AS LastUseDate,
q.work_no
FROM dbo.[work]
LEFT OUTER JOIN dbo.rate_cat
ON dbo.[work].rate_cat = dbo.rate_cat.rate_cat
LEFT OUTER JOIN dbo.business
ON dbo.[work].bill_to_business_id = dbo.business.business_id
LEFT OUTER JOIN
(Select max(work_no) as work_no, create_time
From dbo.[work]
Group by create_time)q
ON work.create_time = q.create_time
and work.work_no = q.work_no
Group by dbo.rate_cat.rate_cat, dbo.rate_cat.description, q.work_no
ORDER BY dbo.rate_cat.rate_cat