?
Solved

how do I only show the record with the max note created date?

Posted on 2012-04-04
8
Medium Priority
?
195 Views
Last Modified: 2012-04-16
SELECT tl_Aircraft.Registration, tm_AircraftDutySchedules.AircraftDutyScheduleID, tm_AircraftDutySchedules.AircraftID, tm_AircraftDutySchedules.StartAirportID,
                      tm_AircraftDutySchedules.EndAirportID, tm_AircraftDutySchedules.AircraftDutyLookupID, tm_AircraftDutySchedules.StartTime, tm_AircraftDutySchedules.EndTime,
                      tm_AircraftDutySchedules.StartTimeLocal, tm_AircraftDutySchedules.EndTimeLocal, tm_AircraftDutySchedules.HasUserNotes, tm_AircraftDutySchedules.Description,
                      tm_AircraftDutySchedules.IsTraxManaged, tm_AircraftDutySchedules.BWAircraftMaintenanceNoteID, tm_AircraftDutySchedules.StartVendorID,
                      tm_AircraftDutySchedules.EndVendorID, tm_AircraftDutySchedules.OrigStartTime, tm_AircraftDutySchedules.OrigEndTime, tm_Notes.NoteTypeID,
                      tm_Notes.DateCreated, tm_Notes.NoteText, tm_Notes.isactive,
                      datediff(mi,origstarttime,starttime) as TotalMinMoved
                     
FROM         tm_AircraftDutySchedules INNER JOIN
                      tl_Aircraft ON tm_AircraftDutySchedules.AircraftID = tl_Aircraft.AircraftID INNER JOIN
                      tm_Notes ON tm_AircraftDutySchedules.AircraftDutyScheduleID = tm_Notes.EntityKeyID
WHERE     (tm_AircraftDutySchedules.StartTime <> tm_AircraftDutySchedules.OrigStartTime) AND (tm_AircraftDutySchedules.StartTime >= GETDATE())
                  and (notetext like '%This aircraft schedule was auto-delayed%')
                 
order by tl_Aircraft.Registration asc


I tried max on tm_Notes.DateCreated but didn't have any luck.
0
Comment
Question by:fwstealer
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 14

Expert Comment

by:Christopher Gordon
ID: 37805826
SELECT  top 1 tl_Aircraft.Registration, tm_AircraftDutySchedules.AircraftDutyScheduleID, tm_AircraftDutySchedules.AircraftID, tm_AircraftDutySchedules.StartAirportID,
                      tm_AircraftDutySchedules.EndAirportID, tm_AircraftDutySchedules.AircraftDutyLookupID, tm_AircraftDutySchedules.StartTime, tm_AircraftDutySchedules.EndTime,
                      tm_AircraftDutySchedules.StartTimeLocal, tm_AircraftDutySchedules.EndTimeLocal, tm_AircraftDutySchedules.HasUserNotes, tm_AircraftDutySchedules.Description,
                      tm_AircraftDutySchedules.IsTraxManaged, tm_AircraftDutySchedules.BWAircraftMaintenanceNoteID, tm_AircraftDutySchedules.StartVendorID,
                      tm_AircraftDutySchedules.EndVendorID, tm_AircraftDutySchedules.OrigStartTime, tm_AircraftDutySchedules.OrigEndTime, tm_Notes.NoteTypeID,
                      tm_Notes.DateCreated, tm_Notes.NoteText, tm_Notes.isactive,
 
                      datediff(mi,origstarttime,starttime) as TotalMinMoved,
                     
                      ROW_NUMBER() over (Order by tm_Notes.DateCreated DESC) as Instance_Counter
                     
FROM         tm_AircraftDutySchedules INNER JOIN
                      tl_Aircraft ON tm_AircraftDutySchedules.AircraftID = tl_Aircraft.AircraftID INNER JOIN
                      tm_Notes ON tm_AircraftDutySchedules.AircraftDutyScheduleID = tm_Notes.EntityKeyID
WHERE     (tm_AircraftDutySchedules.StartTime <> tm_AircraftDutySchedules.OrigStartTime) AND (tm_AircraftDutySchedules.StartTime >= GETDATE())
                  and (notetext like '%This aircraft schedule was auto-delayed%')
                 
order by tm_Notes.DateCreated desc
0
 
LVL 14

Accepted Solution

by:
Christopher Gordon earned 2000 total points
ID: 37805831
Also, you can get rid of that ROW_NUMBER() / Instance_Counter field.  It's not necessary.
0
 
LVL 70

Expert Comment

by:Scott Pletcher
ID: 37806846
...unchanged...
FROM         tm_AircraftDutySchedules INNER JOIN
                      tl_Aircraft ON tm_AircraftDutySchedules.AircraftID = tl_Aircraft.AircraftID INNER JOIN (
                      SELECT *, ROW_NUMBER() OVER (PARTITION BY EntityKeyID ORDER BY DateCreated DESC) AS rownum
                      FROM tm_Notes tm_Notes_Rownum) AS                      
                      tm_Notes ON tm_AircraftDutySchedules.AircraftDutyScheduleID = tm_Notes.EntityKeyID AND tm_Notes.rownum = 1
...unchanged...
0
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 
LVL 32

Expert Comment

by:Ephraim Wangoya
ID: 37810157
Here is another variation

SELECT tl_Aircraft.Registration, tm_AircraftDutySchedules.AircraftDutyScheduleID, tm_AircraftDutySchedules.AircraftID, tm_AircraftDutySchedules.StartAirportID, 
        tm_AircraftDutySchedules.EndAirportID, tm_AircraftDutySchedules.AircraftDutyLookupID, tm_AircraftDutySchedules.StartTime, tm_AircraftDutySchedules.EndTime, 
                      tm_AircraftDutySchedules.StartTimeLocal, tm_AircraftDutySchedules.EndTimeLocal, tm_AircraftDutySchedules.HasUserNotes, tm_AircraftDutySchedules.Description, 
                      tm_AircraftDutySchedules.IsTraxManaged, tm_AircraftDutySchedules.BWAircraftMaintenanceNoteID, tm_AircraftDutySchedules.StartVendorID, 
                      tm_AircraftDutySchedules.EndVendorID, tm_AircraftDutySchedules.OrigStartTime, tm_AircraftDutySchedules.OrigEndTime, tm_Notes.NoteTypeID, 
                      tm_Notes.DateCreated, tm_Notes.NoteText, tm_Notes.isactive,
                      datediff(mi,origstarttime,starttime) as TotalMinMoved
                      
FROM         tm_AircraftDutySchedules 
INNER JOIN tl_Aircraft ON tm_AircraftDutySchedules.AircraftID = tl_Aircraft.AircraftID 
INNER JOIN (select EntityKeyID, max(DateCreated) DateCreated from tm_Notes group by AircraftDutyScheduleID) A on tm_AircraftDutySchedules.AircraftDutyScheduleID = A.EntityKeyID
INNER JOIN tm_Notes ON A.EntityKeyID = tm_Notes.EntityKeyID and A.DateCreated = tm_Notes.DateCreated	
WHERE (tm_AircraftDutySchedules.StartTime <> tm_AircraftDutySchedules.OrigStartTime)
AND (tm_AircraftDutySchedules.StartTime >= GETDATE())
and (notetext like '%This aircraft schedule was auto-delayed%') 
                  
order by tl_Aircraft.Registration asc

Open in new window

0
 
LVL 32

Expert Comment

by:Ephraim Wangoya
ID: 37810159
correction

SELECT tl_Aircraft.Registration, tm_AircraftDutySchedules.AircraftDutyScheduleID, tm_AircraftDutySchedules.AircraftID, tm_AircraftDutySchedules.StartAirportID, 
        tm_AircraftDutySchedules.EndAirportID, tm_AircraftDutySchedules.AircraftDutyLookupID, tm_AircraftDutySchedules.StartTime, tm_AircraftDutySchedules.EndTime, 
                      tm_AircraftDutySchedules.StartTimeLocal, tm_AircraftDutySchedules.EndTimeLocal, tm_AircraftDutySchedules.HasUserNotes, tm_AircraftDutySchedules.Description, 
                      tm_AircraftDutySchedules.IsTraxManaged, tm_AircraftDutySchedules.BWAircraftMaintenanceNoteID, tm_AircraftDutySchedules.StartVendorID, 
                      tm_AircraftDutySchedules.EndVendorID, tm_AircraftDutySchedules.OrigStartTime, tm_AircraftDutySchedules.OrigEndTime, tm_Notes.NoteTypeID, 
                      tm_Notes.DateCreated, tm_Notes.NoteText, tm_Notes.isactive,
                      datediff(mi,origstarttime,starttime) as TotalMinMoved
                      
FROM         tm_AircraftDutySchedules 
INNER JOIN tl_Aircraft ON tm_AircraftDutySchedules.AircraftID = tl_Aircraft.AircraftID 
INNER JOIN (select EntityKeyID, max(DateCreated) DateCreated from tm_Notes group by EntityKeyID) A on tm_AircraftDutySchedules.AircraftDutyScheduleID = A.EntityKeyID
INNER JOIN tm_Notes ON A.EntityKeyID = tm_Notes.EntityKeyID and A.DateCreated = tm_Notes.DateCreated	
WHERE (tm_AircraftDutySchedules.StartTime <> tm_AircraftDutySchedules.OrigStartTime)
AND (tm_AircraftDutySchedules.StartTime >= GETDATE())
and (notetext like '%This aircraft schedule was auto-delayed%') 
                  
order by tl_Aircraft.Registration asc

Open in new window

0
 

Author Comment

by:fwstealer
ID: 37810703
scott - not quite following you on that
ewangoya - that query returned no rows
0
 
LVL 70

Expert Comment

by:Scott Pletcher
ID: 37811816
SELECT tl_Aircraft.Registration, tm_AircraftDutySchedules.AircraftDutyScheduleID, tm_AircraftDutySchedules.AircraftID, tm_AircraftDutySchedules.StartAirportID,
                      tm_AircraftDutySchedules.EndAirportID, tm_AircraftDutySchedules.AircraftDutyLookupID, tm_AircraftDutySchedules.StartTime, tm_AircraftDutySchedules.EndTime,
                      tm_AircraftDutySchedules.StartTimeLocal, tm_AircraftDutySchedules.EndTimeLocal, tm_AircraftDutySchedules.HasUserNotes, tm_AircraftDutySchedules.Description,
                      tm_AircraftDutySchedules.IsTraxManaged, tm_AircraftDutySchedules.BWAircraftMaintenanceNoteID, tm_AircraftDutySchedules.StartVendorID,
                      tm_AircraftDutySchedules.EndVendorID, tm_AircraftDutySchedules.OrigStartTime, tm_AircraftDutySchedules.OrigEndTime, tm_Notes.NoteTypeID,
                      tm_Notes.DateCreated, tm_Notes.NoteText, tm_Notes.isactive,
                      datediff(mi,origstarttime,starttime) as TotalMinMoved

FROM         tm_AircraftDutySchedules INNER JOIN
                      tl_Aircraft ON tm_AircraftDutySchedules.AircraftID = tl_Aircraft.AircraftID INNER JOIN (
                      SELECT *, ROW_NUMBER() OVER (PARTITION BY EntityKeyID ORDER BY DateCreated DESC) AS rownum
                      FROM tm_Notes tm_Notes_Rownum) AS                      
                      tm_Notes ON tm_AircraftDutySchedules.AircraftDutyScheduleID = tm_Notes.EntityKeyID AND tm_Notes.rownum = 1                    

WHERE     (tm_AircraftDutySchedules.StartTime <> tm_AircraftDutySchedules.OrigStartTime) AND (tm_AircraftDutySchedules.StartTime >= GETDATE())
                  and (notetext like '%This aircraft schedule was auto-delayed%')
                 
order by tl_Aircraft.Registration asc
0
 
LVL 13

Expert Comment

by:Ashok
ID: 37818082
Did you try gohord's solution?

I think he has it the correct way.

Ashok
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question