SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 SELECT Query
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

bmsra79
Starting Member

24 Posts

Posted - 09/26/2012 :  07:00:52  Show Profile  Reply with Quote
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 - 09/28/2012 :  13:26:37  Show Profile  Reply with Quote
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

India
52309 Posts

Posted - 09/28/2012 :  13:40:44  Show Profile  Reply with Quote

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)


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

Go to Top of Page

bmsra79
Starting Member

24 Posts

Posted - 09/28/2012 :  14:11:16  Show Profile  Reply with Quote
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

India
52309 Posts

Posted - 09/28/2012 :  15:55:29  Show Profile  Reply with Quote
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 - 10/08/2012 :  08:27:50  Show Profile  Reply with Quote
Ok. Forget about the Flag. It was created by me.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52309 Posts

Posted - 10/08/2012 :  20:00:38  Show Profile  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000