Solved

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

Posted on 2012-04-04
8
190 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

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

Comparison of Amazon Drive, Google Drive, OneDrive

What is Best for Backup: Amazon Drive, Google Drive or MS OneDrive? In this free whitepaper we look at their performance, pricing, and platform availability to help you decide which cloud drive is right for your situation. Download and read the results of our testing for free!

Question has a verified solution.

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

Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

724 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