Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
abliss
Starting Member
6 Posts |
Posted - 2011-10-26 : 21:14:53
|
| Hello I'm currently trying to write a query to report when a service ticket is switched between sections.The table structure is as below:CREATE TABLE _Ticket_Audit( Ticket_Audit_ID int IDENTITY(1,1) NOT NULL, Ticket_RecID int NULL, Ticket_Status_RecID int NULL, Change_Date datetime NULL, Section int NULL, Team int NULL, Priority int NULL) GOA sample set of data is as follows:Ticket_RecID Ticket_Status_RecID Change_Date Section Team Priority19581 48 7/12/11 8:39 15 32 419581 48 7/12/11 8:40 15 32 419581 48 7/12/11 11:09 15 32 419581 48 7/12/11 11:09 15 32 419581 48 7/12/11 23:44 15 32 419581 48 7/12/11 23:48 23 0 419581 48 7/12/11 23:48 23 39 419581 102 7/12/11 23:48 23 39 419581 102 7/24/11 9:32 23 39 419581 102 7/24/11 9:33 17 0 419581 102 7/24/11 9:33 17 34 419581 57 7/24/11 9:33 17 34 419581 57 8/23/11 10:38 17 34 419581 55 8/23/11 10:38 17 34 4An example of the results I would need the query to return based off of the above information is as follows:19581 48 7/12/2011 23:44 15 32 419581 48 7/12/2011 23:48 23 0 419581 102 7/24/2011 9:32 23 39 419581 102 7/24/2011 9:33 17 0 4Thanks |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-10-27 : 02:41:48
|
| [code]SELECT t.*FROM Table tOUTER APPLY (SELECT TOP 1 Section FROM Table WHERE Ticket_RecID = t.Ticket_RecID AND Ticket_Status_RecID = t.Ticket_Status_RecID AND Change_Date > t.Change_Date ORDER BY Change_Date ASC) t1OUTER APPLY (SELECT TOP 1 Section FROM Table WHERE Ticket_RecID = t.Ticket_RecID AND Ticket_Status_RecID = t.Ticket_Status_RecID AND Change_Date < t.Change_Date ORDER BY Change_Date DESC) t2WHERE t1.Section <> t.SectionOR t2.Section <> t.Section[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
abliss
Starting Member
6 Posts |
Posted - 2011-11-01 : 21:12:22
|
Thanks for the help Visakh, it worked out really nice. Never used the "Outer" or "Cross" Apply, it works very nice and efficiently.quote: Originally posted by visakh16
SELECT t.*FROM Table tOUTER APPLY (SELECT TOP 1 Section FROM Table WHERE Ticket_RecID = t.Ticket_RecID AND Ticket_Status_RecID = t.Ticket_Status_RecID AND Change_Date > t.Change_Date ORDER BY Change_Date ASC) t1OUTER APPLY (SELECT TOP 1 Section FROM Table WHERE Ticket_RecID = t.Ticket_RecID AND Ticket_Status_RecID = t.Ticket_Status_RecID AND Change_Date < t.Change_Date ORDER BY Change_Date DESC) t2WHERE t1.Section <> t.SectionOR t2.Section <> t.Section ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
|
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
|
|
|
|
|