Hello,
I'm working on a report from a service documentation database application with SQL backend. There is a table for work orders and a separate table for the work done related by wo_number. I need to report the last entry in the work done table only if it is a certain code.
work done table=wct.*. Fields: wo_number, sequence, response, wct_id, plus more fields
I've tried many variations of my sql statement but run into > 60 second execution time. There are just over 1 million entries.
Goal: List only work orders where the last entry(MAX sequence) has a response="PMU". (Note: PMU entry can be made multiple times on different dates. I'm looking for only work orders where the last entry was PMU. Other codes used after a PMU entry should exclude the work order from the report.)
My best attempt is:
SELECT wo_number, response, MAX(sequence) AS maxseq, COUNT (wo_number) AS maxcnt FROM wct WHERE wo_number > 550000 AND response='PMU' GROUP BY wo_number, response ORDER BY wo_number.
On one particular work order, this gives me maxseq = 22 and maxcnt = 18. I was expecting maxseq = 18 (as max number of PMU entries) and maxcnt = 26 (as total entries) based on the data in the actual table. I see that is because of the 'response='PMU' in the WHERE clause, but don't know how to fix it.
I am not a programmer. Any help is appreciated.
__________