Show last date - time field in event

I need to find an event by the last time entered - it may have one or 10 items for the event, I only want to see the last one. It is not a datetime by default. so I have to concatenate to make it one. So I'm not sure on the where statement to find the last event entered by time performed.

dbo.Trip_Interventions.dateperformed + ' ' + dbo.Trip_Interventions.timeperformed AS [Intervention Time]

Thanks in advance
srodgers45Asked:
Who is Participating?
 
Mark ElySenior Coldfusion DeveloperCommented:
If you want only the fields you highlighted in yellow

WHERE dbo.FDC_Trips.atstime= (SELECT MAX(dbo.FDC_Trips.atstime) FROM dbo.FDC_Trips)



SELECT     TOP (100) PERCENT dbo.FDC_Trips.tdate, dbo.FDC_Trips.job, dbo.View_CAD_EID_by_tdatejob.[CAD EID], dbo.Companies.name AS Agency, 
                      dbo.Companies.code AS [Agency Code], dbo.FDC_Trips.atsdate + ' ' + dbo.FDC_Trips.atstime AS [At Scene Time], 
                      dbo.Trip_Interventions.dateperformed + ' ' + dbo.Trip_Interventions.timeperformed AS [Intervention Time], 
                      dbo.FDC_Trip_Times.txcare_date + ' ' + dbo.FDC_Trip_Times.txcare_time AS [Tx Care Time], dbo.Trip_Interventions.pta AS [Intervention PTA]
FROM         dbo.FDC_Trips INNER JOIN
                      dbo.FDC_Trip_Times ON dbo.FDC_Trips.tdate = dbo.FDC_Trip_Times.tdate AND dbo.FDC_Trips.job = dbo.FDC_Trip_Times.job INNER JOIN
                      dbo.Trip_Interventions ON dbo.FDC_Trips.tdate = dbo.Trip_Interventions.tdate AND dbo.FDC_Trips.job = dbo.Trip_Interventions.job LEFT OUTER JOIN
                      dbo.View_CAD_EID_by_tdatejob ON dbo.FDC_Trips.tdate = dbo.View_CAD_EID_by_tdatejob.TDate AND 
                      dbo.FDC_Trips.job = dbo.View_CAD_EID_by_tdatejob.Job LEFT OUTER JOIN
                      dbo.Companies ON dbo.FDC_Trips.cmpy = dbo.Companies.code
WHERE dbo.FDC_Trips.atstime = (SELECT MAX(dbo.FDC_Trips.atstime) FROM dbo.FDC_Trips)
ORDER BY dbo.View_CAD_EID_by_tdatejob.[CAD EID] DESC

Open in new window

0
 
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Give us the schema of the tables(s) you're working with.  A data mockup would be nice too.
0
 
Mark ElySenior Coldfusion DeveloperCommented:
It is hard to grasp the breath of your question without more information; however you can use:

SELECT IDENT_CURRENT(‘tablename’)

It returns last identity value produced in a table.  It is not limited by scope or session; but it is limited to a specific table.  With this id you can get the information you want.  

Your code might look something like this:
SELECT * 
FROM NameOfYourTable
WHERE PKID = (SELECT IDENT_CURRENT ('NameOfYourTable'))

Open in new window

0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
srodgers45Author Commented:
Does this help?

SELECT     TOP (100) PERCENT dbo.FDC_Trips.tdate, dbo.FDC_Trips.job, dbo.View_CAD_EID_by_tdatejob.[CAD EID], dbo.Companies.name AS Agency,
                      dbo.Companies.code AS [Agency Code], dbo.FDC_Trips.atsdate + ' ' + dbo.FDC_Trips.atstime AS [At Scene Time],
                      dbo.Trip_Interventions.dateperformed + ' ' + dbo.Trip_Interventions.timeperformed AS [Intervention Time],
                      dbo.FDC_Trip_Times.txcare_date + ' ' + dbo.FDC_Trip_Times.txcare_time AS [Tx Care Time], dbo.Trip_Interventions.pta AS [Intervention PTA]
FROM         dbo.FDC_Trips INNER JOIN
                      dbo.FDC_Trip_Times ON dbo.FDC_Trips.tdate = dbo.FDC_Trip_Times.tdate AND dbo.FDC_Trips.job = dbo.FDC_Trip_Times.job INNER JOIN
                      dbo.Trip_Interventions ON dbo.FDC_Trips.tdate = dbo.Trip_Interventions.tdate AND dbo.FDC_Trips.job = dbo.Trip_Interventions.job LEFT OUTER JOIN
                      dbo.View_CAD_EID_by_tdatejob ON dbo.FDC_Trips.tdate = dbo.View_CAD_EID_by_tdatejob.TDate AND
                      dbo.FDC_Trips.job = dbo.View_CAD_EID_by_tdatejob.Job LEFT OUTER JOIN
                      dbo.Companies ON dbo.FDC_Trips.cmpy = dbo.Companies.code
WHERE     (dbo.FDC_Trips.tdate > '2015-05-01')
ORDER BY dbo.View_CAD_EID_by_tdatejob.[CAD EID] DESC
0
 
Vitor MontalvãoMSSQL Senior EngineerCommented:
if the fields are well formatted (date and time) then this may work for you:
SELECT TOP 1 dbo.FDC_Trips.tdate, dbo.FDC_Trips.job, dbo.View_CAD_EID_by_tdatejob.[CAD EID], dbo.Companies.name AS Agency, 
			dbo.Companies.code AS [Agency Code], dbo.FDC_Trips.atsdate + ' ' + dbo.FDC_Trips.atstime AS [At Scene Time], 
            dbo.Trip_Interventions.dateperformed + ' ' + dbo.Trip_Interventions.timeperformed AS [Intervention Time], 
            dbo.FDC_Trip_Times.txcare_date + ' ' + dbo.FDC_Trip_Times.txcare_time AS [Tx Care Time], dbo.Trip_Interventions.pta AS [Intervention PTA]
FROM dbo.FDC_Trips 
	INNER JOIN dbo.FDC_Trip_Times ON dbo.FDC_Trips.tdate = dbo.FDC_Trip_Times.tdate AND dbo.FDC_Trips.job = dbo.FDC_Trip_Times.job 
	INNER JOIN dbo.Trip_Interventions ON dbo.FDC_Trips.tdate = dbo.Trip_Interventions.tdate AND dbo.FDC_Trips.job = dbo.Trip_Interventions.job 
	LEFT OUTER JOIN dbo.View_CAD_EID_by_tdatejob ON dbo.FDC_Trips.tdate = dbo.View_CAD_EID_by_tdatejob.TDate
													 AND dbo.FDC_Trips.job = dbo.View_CAD_EID_by_tdatejob.Job 
	LEFT OUTER JOIN dbo.Companies ON dbo.FDC_Trips.cmpy = dbo.Companies.code
WHERE  (dbo.FDC_Trips.tdate > '2015-05-01')
ORDER BY CAST(dbo.Trip_Interventions.dateperformed + ' ' + dbo.Trip_Interventions.timeperformed AS DATETIME) DESC 

Open in new window

If don't work please provide a sample of data for dbo.Trip_Interventions.dateperformed and dbo.Trip_Interventions.timeperformed fields.
0
 
srodgers45Author Commented:
That orders it by the intervention date, I only want to show the last event for that intervention. Sorry if I did not describe that better.

So regardless of how many interventions were present for a single event I only want to see the one that was last by datetime.

The highlighted section is an example where 4 events were entered, I only want to show the one with the latest intervention time of 06:20 for that event (0074-A)

Sample of current output
Thank you for the help
C--Users-srodgers-Desktop-Intervention-T
0
 
Mark ElySenior Coldfusion DeveloperCommented:
Change your where to

WHERE dbo.FDC_Trips.tdate = (SELECT MAX(dbo.FDC_Trips.tdate) FROM dbo.FDC_Trips)
0
 
Vitor MontalvãoMSSQL Senior EngineerCommented:
That orders it by the intervention date, I only want to show the last event for that intervention. Sorry if I did not describe that better.
Descending order by intervention date and time plus the TOP 1 will give you only the latest entry. Isn't what you want?
0
 
srodgers45Author Commented:
Thanks Vitor, I need too see all the records entered for the date ranges, but without the multiple rows if more than one intervention is entered for that single event. So I am trying to filter out all the other interventions for a single event by the last intervention time entered.

For example, the sample data highlighted above has 4 interventions entered, I only want to see one record (row) by the last intervention time, not the other 3 interventions for that one event (the last one entered has the intervention time of 06:20).

Hopefully that makes sense...

Thanks for your assistance and patience
0
 
Vitor MontalvãoMSSQL Senior EngineerCommented:
Hopefully that makes sense...
Or I'm not understanding you or you're not understanding me.
Did you try my query? What returns and what should be returned?
Better you explain me with samples.
Thanks.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.