Solved

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

Posted on 2012-04-04
8
184 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 500 total points
ID: 37805831
Also, you can get rid of that ROW_NUMBER() / Instance_Counter field.  It's not necessary.
0
 
LVL 69

Expert Comment

by:ScottPletcher
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
 
LVL 32

Expert Comment

by:ewangoya
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 32

Expert Comment

by:ewangoya
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 69

Expert Comment

by:ScottPletcher
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Audit has been really one of the more interesting, most useful, yet difficult to maintain topics in the history of SQL Server. In earlier versions of SQL people had very few options for auditing in SQL Server. It typically meant using SQL Trace …
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

758 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now