Author |
Topic |
AfcDons
Starting Member
10 Posts |
Posted - 2013-11-19 : 11:22:05
|
Hi,I am trying to write a query pulling the last te_event from a table by the latest te_date for a set of records. My query is below SELECT tickets.t_number, tickets.t_reference,ticket_events.te_event, ticket_events.te_date, FROM ticket_events INNER JOIN tickets ON ticket_events.te_system_ref = tickets.t_number INNER JOIN warrants_due ON tickets.t_number = warrants_due.wd_system_refWHERE tickets.t_number in (1091421,1091426,1091429,1091430,1091432,1091450)There are 25,000 tickets in the subquery. I can't work out where to put the MAX (ticket_events.te_date) statement.Any help appreciatedthanksJohn |
|
James K
Master Smack Fu Yak Hacker
3873 Posts |
Posted - 2013-11-19 : 14:26:00
|
I didn't quite follow your explanation, but perhaps this? SELECT * FROM (SELECT tickets.t_number , tickets.t_reference , ticket_events.te_event , ticket_events.te_date, ROW_NUMBER() OVER (PARTITION BY ticket_events.te_event ORDER BY ticket_events.te_date DESC) RNFROM ticket_events INNER JOIN tickets ON ticket_events.te_system_ref = tickets.t_number INNER JOIN warrants_due ON tickets.t_number = warrants_due.wd_system_refWHERE tickets.t_number IN ( 1091421, 1091426, 1091429, 1091430, 1091432, 1091450 )) s WHERE RN = 1; |
 |
|
AfcDons
Starting Member
10 Posts |
Posted - 2013-11-20 : 03:29:54
|
Hi James,Thanks for the reply but I didn't explain it very well.I have a table with Tickets and each of these tickets have a series of events that can happen to them stored in the events table. I need to pull out just the latest event for each ticket by the event date. I have 25,000 tickets to do this to. I know that MAX will bring up the latest date but I don't know where to put the MAX statement so I get all 25,000 results. I hope this is a little clearer.Thanks again for the help.John |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-11-20 : 04:10:19
|
quote: Originally posted by AfcDons Hi James,Thanks for the reply but I didn't explain it very well.I have a table with Tickets and each of these tickets have a series of events that can happen to them stored in the events table. I need to pull out just the latest event for each ticket by the event date. I have 25,000 tickets to do this to. I know that MAX will bring up the latest date but I don't know where to put the MAX statement so I get all 25,000 results. I hope this is a little clearer.Thanks again for the help.John
I think previous suggestion would give you what you want.Also thisSELECT tickets.t_number, tickets.t_reference,ticket_events.te_event, ticket_events.te_date, FROM ticket_events INNER JOINtickets ON ticket_events.te_system_ref = tickets.t_number INNER JOINwarrants_due ON tickets.t_number = warrants_due.wd_system_refWHERE tickets.t_number in (1091421,1091426,1091429,1091430,1091432,1091450)AND EXISTS (SELECT 1FROM FROM ticket_events te INNER JOIN tickets tON te.te_system_ref = t.t_numberWHERE t_number = ticket.t_numberGROUP BY t.t_numberHAVING MAX(te.te_date) = ticket_events.te_event) ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
 |
|
AfcDons
Starting Member
10 Posts |
Posted - 2013-11-22 : 03:51:36
|
Hi Visakh,I have run the code but it fails with the errorThe conversion of a varchar data type to a datetime data type resulted in an out-of-range value.I am confused by the lineHAVING MAX(ticket_events.te_date) = ticket_events.te_eventas te_date is a date field and te_event is varcharThe previous code from James didn't work as it pulled back all the records from the event table when I only want the latest.Thanks again for your help with this as it has been frustrating me for a while.John |
 |
|
bandi
Master Smack Fu Yak Hacker
2242 Posts |
|
AfcDons
Starting Member
10 Posts |
Posted - 2013-11-22 : 04:25:05
|
Hi,The Te_event column isn't a date column. It is a VarChar column.It has text in it.thanksJohn |
 |
|
bandi
Master Smack Fu Yak Hacker
2242 Posts |
Posted - 2013-11-22 : 04:32:44
|
May be its a typo.. try thisHAVING MAX(ticket_events.te_date) = ticket_events.te_date--Chandu |
 |
|
AfcDons
Starting Member
10 Posts |
Posted - 2013-11-22 : 04:52:14
|
Hi Chandu,Thanks for this but I am getting too many returns in the resultset. There are only six tickets so I was hoping for six results ( last event for each ticket) but I get 161 results ( all the events for each ticket)Thanks again for your help.John |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-11-22 : 05:41:38
|
quote: Originally posted by AfcDons Hi Chandu,Thanks for this but I am getting too many returns in the resultset. There are only six tickets so I was hoping for six results ( last event for each ticket) but I get 161 results ( all the events for each ticket)Thanks again for your help.John
show your used code please.does your te_date filed has timepart?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
 |
|
AfcDons
Starting Member
10 Posts |
Posted - 2013-11-22 : 06:00:08
|
Hi,some of the results look like belowt_number t_reference te_event te_date1091450 HZ9636390A Debt registration requested 2009-06-30 04:48:35.0001091450 HZ9636390A Debt Registration Re-Scheduled 2009-06-30 16:42:13.0001091450 HZ9636390A Debt registration requested 2009-07-01 04:55:57.0001091450 HZ9636390A Debt registration accepted 2009-07-01 15:55:37.000The te_date does have a timpart as above.Thanks for your helpJohn |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-11-22 : 06:59:24
|
i think the reason is not using proper aliastry thisSELECT t.t_number, t.t_reference,te.te_event, te.te_dateFROM ticket_events te INNER JOIN tickets tON te.te_system_ref = t.t_number INNER JOIN warrants_due wON t.t_number = w.wd_system_refWHERE t.t_number in (1091421,1091426,1091429,1091430,1091432,1091450)AND EXISTS (SELECT 1FROM ticket_events te1 INNER JOIN tickets t1ON te1.te_system_ref = t1.t_numberWHERE t1.t_number = t.t_numberGROUP BY t1.t_numberHAVING MAX(te1.te_date) = te.te_event) ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
 |
|
AfcDons
Starting Member
10 Posts |
Posted - 2013-11-22 : 07:29:36
|
Hi visakhI had to change the last bitHAVING MAX(te1.te_date) = te.te_event toHAVING MAX(te1.te_date) = te.te_dateAnd it is now working. Thanks very much for this. It will help me in the future.Thanks to everyone else who repliedJohn |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-11-22 : 07:30:29
|
quote: Originally posted by AfcDons Hi visakhI had to change the last bitHAVING MAX(te1.te_date) = te.te_event toHAVING MAX(te1.te_date) = te.te_dateAnd it is now working. Thanks very much for this. It will help me in the future.Thanks to everyone else who repliedJohn
Ah copy pasted it from earlier suggestion and forgot to change itanyways glad that you picked it up------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
 |
|
|