I am trying to write a query to display any equipment that is due to be sent in for yearly maintenance. It should run off of the creation date of the equipment entry if it hasn't been sent in before. If it has been sent in, it needs to run off of the closed date of our service call.
I created a case statement to display a column of the date I would like it to use (called 'Start Date'). First of all, how can I display only the most recent service call date if there are multiple service calls for that piece of equipment? Second, how can I use this case result in my where clause to show everything greater than a year from the 'Start Date'?
Any help would be greatly appreciated!! Below is what I have so far (OINS is the equipment card table and OSCL is the service call table):
Set ANSI_NULLS off
SELECT T0.[insID], T0.[manufSN], T0.[customer], T0.[itemCode], T0.createdate, T1.createdate, T1.closedate,
CASE T1.createdate
when NULL then T0.createdate
else T1.closedate
end AS 'Start Date'
FROM OINS T0 LEFT JOIN OSCL T1 ON T0.insID = T1.insID
WHERE T0.status = 'A'
I created a case statement to display a column of the date I would like it to use (called 'Start Date'). First of all, how can I display only the most recent service call date if there are multiple service calls for that piece of equipment? Second, how can I use this case result in my where clause to show everything greater than a year from the 'Start Date'?
Any help would be greatly appreciated!! Below is what I have so far (OINS is the equipment card table and OSCL is the service call table):
Set ANSI_NULLS off
SELECT T0.[insID], T0.[manufSN], T0.[customer], T0.[itemCode], T0.createdate, T1.createdate, T1.closedate,
CASE T1.createdate
when NULL then T0.createdate
else T1.closedate
end AS 'Start Date'
FROM OINS T0 LEFT JOIN OSCL T1 ON T0.insID = T1.insID
WHERE T0.status = 'A'