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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.