I am writing a SQL query that counts all of the errors that we logged between 2 dates. Unfortunately, some of the data in the table we are pulling from was logged incorrectly. To fix this, I tried to add some CASE statements into the SQL query to change the error name if it was logged before a specific date. The query I wrote works as intended, but it just seems to be a lot of CASE statements. Here is the code:
SELECT
case
when :Startdate <= '2025-6-13 00:00:00' and Fault = 'Inline fill timed out' or :Enddate <= '2025-6-13 00:00:0' and Fault = 'Inline fill timed out' then 'EOAT Mandrel Stripper Extend'
when :Startdate <= '2025-6-13 00:00:00' and Fault = 'Expected part present at shuttle' or :Enddate <= '2025-6-13 00:00:0' and Fault = 'Expected part present at shuttle' then 'EOAT Mandrel Stripper Retract'
when :Startdate <= '2025-6-13 00:00:00' and Fault = 'Escapement slide 1 (upper) extend' or :Enddate <= '2025-6-13 00:00:0' and Fault = 'Escapement slide 1 (upper) extend' then 'Inline fill timed out'
when :Startdate <= '2025-6-13 00:00:00' and Fault = 'Escapement slide 1 (upper) retract' or :Enddate <= '2025-6-13 00:00:0' and Fault = 'Escapement slide 1 (upper) retract' then 'Expected part present at shuttle'
when :Startdate <= '2025-6-13 00:00:00' and Fault = 'Escapement slide 2 (lower) extend' or :Enddate <= '2025-6-13 00:00:0' and Fault = 'Escapement slide 2 (lower) extend' then 'Escapement slide 1 (upper) extend'
when :Startdate <= '2025-6-13 00:00:00' and Fault = 'Escapement slide 2 (lower) retract' or :Enddate <= '2025-6-13 00:00:0' and Fault = 'Escapement slide 2 (lower) retract' then 'Escapement slide 1 (upper) retract'
when :Startdate <= '2025-6-13 00:00:00' and Fault = 'Escapement part eject extend' or :Enddate <= '2025-6-13 00:00:0' and Fault = 'Escapement part eject extend' then 'Escapement slide 2 (lower) extend'
when :Startdate <= '2025-6-13 00:00:00' and Fault = 'Escapement paret eject retract' or :Enddate <= '2025-6-13 00:00:0' and Fault = 'Escapement paret eject retract' then 'Escapement slide 2 (lower) retract'
when :Startdate <= '2025-6-13 00:00:00' and Fault = 'Escapement part containment extend' or :Enddate <= '2025-6-13 00:00:0' and Fault = 'Escapement part containment extend' then 'Escapement part eject extend'
when :Startdate <= '2025-6-13 00:00:00' and Fault = 'Escapement part containment retract' or :Enddate <= '2025-6-13 00:00:0' and Fault = 'Escapement part containment retract' then 'Escapement paret eject retract'
when :Startdate <= '2025-6-13 00:00:00' and Fault = 'IV_1 sensor timed out' or :Enddate <= '2025-6-13 00:00:0' and Fault = 'IV_1 sensor timed out' then 'Escapement part singulator extend'
else Fault
end as Fault,
count(Fault) as count
FROM (SELECT Datetime,
MachineID,
Fault
FROM AutomatedEquipmentFaultLog
WHERE MachineID = :MachineID and Datetime BETWEEN :Startdate and :Enddate and Fault != 'Bowl low parts' and Fault != 'Inline fill timed out')q
GROUP BY Fault
ORDER BY count desc
I also want to avoid switching the actual table values if possible.
Is there a better way to write this query?