×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Equipment Maintenance Query

Equipment Maintenance Query

Equipment Maintenance Query

(OP)
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'

RE: Equipment Maintenance Query

that's pretty cheeky, posting in the ANSI SQL forum when the first thing you do is set ANSI_NULLS off (why do you feel this is necessary, by the way?)

next time you will probably want to post in forum183: Microsoft SQL Server: Programming

smile

CODE

SELECT OINS.[insID]
     , OINS.[manufSN]
     , OINS.[customer]
     , OINS.[itemCode]
     , OINS.createdate
     , OSCL.createdate
     , OSCL.closedate
     , COALESCE(OSCL.closedate
               ,OINS.createdate) AS 'Start Date'
  FROM OINS
LEFT OUTER
  JOIN OSCL
    ON OSCL.insID = OINS.insID
   AND OSCL.closedate =
       ( SELECT MAX(closedate)
           FROM OSCL
          WHERE insID = OINS.insID )
 WHERE OINS.status = 'A'

r937.com | rudy.ca

RE: Equipment Maintenance Query

(OP)
Sorry.  I was working on multiple queries yesterday and forgot that was in there.  Thanks for the information!!

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close