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.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 SELECT Query

Author  Topic 

bmsra79
Starting Member

24 Posts

Posted - 2012-09-26 : 07:00:52
I have a table like this.

Group,DateTime,ID,Status,Action
ClassA,4/30/12 7:36 PM,886914-001-0102,STOP-2,Delete
ClassA,5/3/12 4:59 PM,886914-001-0102,RESTART,Delete
ClassA,5/31/12 6:09 PM,886914-001-0102,STOP-1,Keep
ClassA,5/17/12 7:21 PM,336224-001-0102,STOP-2,Keep
ClassA,5/14/12 6:40 PM,259490-001-0102,STOP,Keep
ClassA,6/27/12 7:46 PM,277669-001-0102,STOP,Keep
ClassA,6/6/12 5:46 PM,151063-001-0102,STOP,Keep
ClassB,4/25/12 6:56 PM,428938-001-0102,STOP-4,Delete
ClassB,4/30/12 8:15 PM,428938-001-0102,RESTART,Delete
ClassB,5/24/12 2:38 PM,428938-001-0102,STOP-5,Keep
ClassB,6/8/12 7:22 PM,428938-001-0102,STOP-6,Keep


I 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, FLAG
1, 01JAN2012, STOP, 1
1, 04JAN2012, RESTART, 2
2, 01JAN2012, RESTART, 1
2, 04JAN2012, STOP-2, 2
2, 10JAN2012, STOP, 1
2, 15JAN2012, RESTART, 2

Expected Output:
ID, DATE, STATUS, FLAG
2, 01JAN2012, RESTART, 1
2, 04JAN2012, STOP-2, 2


I am looking for a SELECT statement excluding all records having (STATUS='RESTART' & FLAG = 2 or higher) & also 1 record before it having same ID
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-09-28 : 13:40:44
[code]
SELECT t.*
FROM table t
OUTER APPLY(SELECT TOP 1 STATUS,FLAG
FROM table
WHERE ID = t.ID
AND [DATE]> t.[DATE]
ORDER BY [DATE] ASC)t1
WHERE NOT( ISNULL(t1.STATUS,'') ='RESTART' AND ISNULL(t1.FLAG,-1) >=2)
AND NOT(t.STATUS='RESTART' AND t.FLAG >=2)
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

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.
Go to Top of Page

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 MVP
http://visakhm.blogspot.com/

Go to Top of Page

bmsra79
Starting Member

24 Posts

Posted - 2012-10-08 : 08:27:50
Ok. Forget about the Flag. It was created by me.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-10-08 : 20:00:38
then what about stop ones? why

2, 10JAN2012, STOP, 1

was not included in o/p

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -