M
MNY Intern
Right now I have probably over 20 queries to get what I'm looking for, and
I'm convinced theres an easier way of doing it...
I have a table with all the fines all the cars in our company incurs. I want
to use 1 query to get the top ten fine incurring cars between date 1 and date
2 from each center, there are 18 centers.
For example:
If I wanted to do top 2--
Center 1 has cars 1-5 each incurring fines daily.
Center 2 has cars 6-10, etc as shown in my table
I specify a range of dates, get the 2 cars from each center that have the
greatest total $amount in fines for that period of time.
Right now my SQL for 1 query that does top 10 for 1 center looks like this:
SELECT TOP 10 [NEW TICKETS].[CTR NAME], [NEW TICKETS].CENTER, [NEW
TICKETS].CARNO, Sum([NEW TICKETS].[new fine]) AS [SumOfnew fine]
FROM ([NEW TICKETS] INNER JOIN [Base Period Rundates] ON [NEW
TICKETS].RUNDATE = [Base Period Rundates].RUNDATE) INNER JOIN VEHICLE ON [NEW
TICKETS].CARNO = VEHICLE.CARNO
GROUP BY [NEW TICKETS].[CTR NAME], [NEW TICKETS].CENTER, [NEW TICKETS].CARNO
HAVING ((([NEW TICKETS].CENTER)="1010"))
ORDER BY Sum([NEW TICKETS].[new fine]) DESC;
I'm convinced theres an easier way of doing it...
I have a table with all the fines all the cars in our company incurs. I want
to use 1 query to get the top ten fine incurring cars between date 1 and date
2 from each center, there are 18 centers.
For example:
If I wanted to do top 2--
Center 1 has cars 1-5 each incurring fines daily.
Center 2 has cars 6-10, etc as shown in my table
I specify a range of dates, get the 2 cars from each center that have the
greatest total $amount in fines for that period of time.
Right now my SQL for 1 query that does top 10 for 1 center looks like this:
SELECT TOP 10 [NEW TICKETS].[CTR NAME], [NEW TICKETS].CENTER, [NEW
TICKETS].CARNO, Sum([NEW TICKETS].[new fine]) AS [SumOfnew fine]
FROM ([NEW TICKETS] INNER JOIN [Base Period Rundates] ON [NEW
TICKETS].RUNDATE = [Base Period Rundates].RUNDATE) INNER JOIN VEHICLE ON [NEW
TICKETS].CARNO = VEHICLE.CARNO
GROUP BY [NEW TICKETS].[CTR NAME], [NEW TICKETS].CENTER, [NEW TICKETS].CARNO
HAVING ((([NEW TICKETS].CENTER)="1010"))
ORDER BY Sum([NEW TICKETS].[new fine]) DESC;