Tricky Select Needed

Please support our MS SQL advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Sep 2008
Posts: 2
Reputation: Kim R is an unknown quantity at this point 
Solved Threads: 0
Kim R Kim R is offline Offline
Newbie Poster

Tricky Select Needed

 
0
  #1
Sep 29th, 2008
Hi all,

I hope someone can help me with this. I have been racking my poor stupid brain for days over this. I need a way to be able to create a view of the data in a single table. Let's call it tblStatus. Here is a list of fields for the example:

StatusId (Primary Key for this table)
StatusTypeId (Foreign Key to a static list of status types)
EquipmentId (Foreign key linking to tblEquipment)
EndDate (Date that this record finishes/finished being in effect)

Now the purpose of tblStatus is to store records about the location and usage of a piece of equipment. The StatusTypeId field links into the type of usage the equipement is used for in the particular record. There is more data in the table but it does not affect the current example.

The resulting data I need is:

List of all StatusId and EquipmentId for only the LATEST (EndDate) for each EquipmentId.

  1. StatusID EquipmentID EndDate StatusTypeID
  2. 8427 14402 30-Sep-08 1
  3. 8148 14402 14-May-07 3
  4. 7669 14402 07-Jul-01 15
  5. 8272 14403 27-Feb-08 2
  6. 8287 14403 11-Sep-06 1
  7. 7998 14403 20-May-05 6
  8. 7670 14403 25-Dec-02 15

In the above example data only records 8427 and 8272 would be what I wanted returned.

Can anyone point me in the right direction please?
Last edited by Kim R; Sep 29th, 2008 at 1:09 am.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 2
Reputation: Kim R is an unknown quantity at this point 
Solved Threads: 0
Kim R Kim R is offline Offline
Newbie Poster

Re: Tricky Select Needed

 
0
  #2
Sep 29th, 2008
Looks like the following code might work:
  1. SELECT a.StatusID, a.equipmentid,b.endate, a.StatusTypeID
  2.  
  3. FROM table1 INNER JOIN
  4.  
  5. (
  6.  
  7. SELECT equipmentid, max(enddate) AS enddate FROM table1
  8.  
  9. GROUP BY equpimentid
  10.  
  11. ) B
  12.  
  13. ON a.equipmentid=b.equipmentid
  14.  
  15. WHERE a.endate=b.enddate
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the MS SQL Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC