Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Help reqd for eliminating a similar IDs- Query


Help reqd for eliminating a similar IDs- Query

Author
Message
Saravanakumar.Rathinam
Saravanakumar.Rathinam
SSC Rookie
SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)

Group: General Forum Members
Points: 49 Visits: 493
Hi all,

I need a basic query based on the below conditions

Sample records
ID BatchID Status
1 121 COMPLETE
2 121 PENDING
3 121 REWORK
4 122 ACK
5 123 TBK

Reqd Output
ID BatchID Status
4 122 ACK
5 123 TBK

If the Status = 'REWORK", I don't want the Particular batchID to the output.

Kindly help me to write the query.

thanks & regards
Saravanakumar.R
RyanRandall
RyanRandall
SSCommitted
SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)

Group: General Forum Members
Points: 1761 Visits: 4652
select * from myTable where BatchID not in (select BatchID from myTable where Status = 'Rework')

Ryan Randall

Solutions are easy. Understanding the problem, now, that's the hard part.
Saravanakumar.Rathinam
Saravanakumar.Rathinam
SSC Rookie
SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)

Group: General Forum Members
Points: 49 Visits: 493
Thanks for your reply,
I did the same, but the performance of the query is not really good, it taking such a long time to get the result, and every row the query touches twice.

is it possible to get same thing with better result using CTE / derived queries ?

thanks & regards
Saravanakumar.R
Michael Earl-395764
Michael Earl-395764
SSCrazy
SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)

Group: General Forum Members
Points: 2701 Visits: 23078
Here are two more options:

SELECT DISTINCT
   T1.*
FROM
   myTable T1
   LEFT JOIN myTable T2 ON T1.BatchID = T2.BatchID
                  AND T2.Status = 'Rework'
WHERE
   T2.BatchID IS NULL

SELECT
   T1.*
FROM
   myTable T1
WHERE
   NOT EXISTS (SELECT * FROM myTable T2 WHERE T2.Status = 'Rework' AND T2.BatchID = T1.BatchID)


The first should outperform the second if your indexing is correct.
sameyullah
sameyullah
SSC-Enthusiastic
SSC-Enthusiastic (174 reputation)SSC-Enthusiastic (174 reputation)SSC-Enthusiastic (174 reputation)SSC-Enthusiastic (174 reputation)SSC-Enthusiastic (174 reputation)SSC-Enthusiastic (174 reputation)SSC-Enthusiastic (174 reputation)SSC-Enthusiastic (174 reputation)

Group: General Forum Members
Points: 174 Visits: 59
select * from table1 where id not in (select id from table1 where status='rework')
Saravanakumar.Rathinam
Saravanakumar.Rathinam
SSC Rookie
SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)

Group: General Forum Members
Points: 49 Visits: 493
Thanks friend,

The mentioned query does not meet the requirement.

If we run the query BATCH ID 121 and the ID 1 & 2 are shown which is not required.

regards
Saravanakumar.R
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search