Solved

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

Posted on 2012-04-04
8
187 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: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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
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.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

803 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