Author |
Topic |
bmsra79
Starting Member
24 Posts |
Posted - 2012-09-26 : 07:00:52
|
I have a table like this. Group,DateTime,ID,Status,ActionClassA,4/30/12 7:36 PM,886914-001-0102,STOP-2,DeleteClassA,5/3/12 4:59 PM,886914-001-0102,RESTART,DeleteClassA,5/31/12 6:09 PM,886914-001-0102,STOP-1,KeepClassA,5/17/12 7:21 PM,336224-001-0102,STOP-2,KeepClassA,5/14/12 6:40 PM,259490-001-0102,STOP,KeepClassA,6/27/12 7:46 PM,277669-001-0102,STOP,KeepClassA,6/6/12 5:46 PM,151063-001-0102,STOP,KeepClassB,4/25/12 6:56 PM,428938-001-0102,STOP-4,DeleteClassB,4/30/12 8:15 PM,428938-001-0102,RESTART,DeleteClassB,5/24/12 2:38 PM,428938-001-0102,STOP-5,KeepClassB,6/8/12 7:22 PM,428938-001-0102,STOP-6,KeepI want a SELECT statement in SQL Server 2008, that can query all records with Action=keep. Discard all records with RESTART status along with the record just before it containing the same Group,ID and same Quarter. |
|
bmsra79
Starting Member
24 Posts |
Posted - 2012-09-28 : 13:26:37
|
Restating the requirement again:ID, DATE, STATUS, FLAG1, 01JAN2012, STOP, 11, 04JAN2012, RESTART, 22, 01JAN2012, RESTART, 12, 04JAN2012, STOP-2, 22, 10JAN2012, STOP, 12, 15JAN2012, RESTART, 2Expected Output:ID, DATE, STATUS, FLAG2, 01JAN2012, RESTART, 12, 04JAN2012, STOP-2, 2I am looking for a SELECT statement excluding all records having (STATUS='RESTART' & FLAG = 2 or higher) & also 1 record before it having same ID |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-09-28 : 13:40:44
|
[code]SELECT t.*FROM table tOUTER APPLY(SELECT TOP 1 STATUS,FLAG FROM table WHERE ID = t.ID AND [DATE]> t.[DATE] ORDER BY [DATE] ASC)t1WHERE NOT( ISNULL(t1.STATUS,'') ='RESTART' AND ISNULL(t1.FLAG,-1) >=2)AND NOT(t.STATUS='RESTART' AND t.FLAG >=2)[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
bmsra79
Starting Member
24 Posts |
Posted - 2012-09-28 : 14:11:16
|
The Query currently excludes all STATUS='RESTART' records.It should exclude only those STATUS=RESTART records having atleast 1 record before the RESTART record having same ID. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-09-28 : 15:55:29
|
quote: Originally posted by bmsra79 The Query currently excludes all STATUS='RESTART' records.It should exclude only those STATUS=RESTART records having atleast 1 record before the RESTART record having same ID.
then what about the flag condition?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
bmsra79
Starting Member
24 Posts |
Posted - 2012-10-08 : 08:27:50
|
Ok. Forget about the Flag. It was created by me. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-10-08 : 20:00:38
|
then what about stop ones? why2, 10JAN2012, STOP, 1was not included in o/p------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
|
|
|