Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

how to exclude unwanted data

$
0
0

Environment: SQL server 2005 and Win 7

Tools: Management studio 2008 R2

Question: I have 381 records of which two are unwanted and let us call it Query A.  Whereas, Query B has an outcome, which identified the right two records to keep in Query A. How to exclude unwanted data from query outcome. If I would have used except statement or intersect, I would have miss the rest of the records in query A (279). I can not use merge statement because I have SQL server 2005

Example

Query A results
.
..
...
....
201303110    A    V    B    Banana    20130521    incorrect
201303110    N    B    A    Banana   20130521    correct
201303260    A    A    B    Jelly        20130521    correct
201303260    F    A    B    Jelly        20130326    incorrect
..
.
381 records


Query B results
201303110    N    B    A    Banana    20130521    correct
201303260    A    A    B    Jelly        20130521    correct

Result

279 records because we removed the two unwanted records and kept the desired ones

please advise

Declare @Table_source2 table (pn_id char(9), S nchar(2),C nchar(2),L nchar(2),first_nm varchar(10),last_nm varchar(20))
INSERT @Table_source VALUES ('123456789','A','V','B', 'Dan'         ,'Kramer'          ,'1/1/2005')  -- Exculde
INSERT @Table_source VALUES ('123456789','N','V','B', 'Dan'         ,'Kramer'          ,'1/1/2007')  -- Keep 
INSERT @Table_source VALUES ('987654321','A','A','B', 'Pamella'      ,'Slattery'        ,'1/1/2009')  -- Keep
INSERT @Table_source VALUES ('987654321','F','V','B', 'Pamella'     ,'Slattery'        ,'1/1/2005')  -- Exculde 
INSERT @Table_source VALUES ('112233350','Z','Z','Z', 'Pam' ,        'Smith'        ,'1/1/2006')  -- keep
INSERT @Table_source VALUES ('097754378','A','V','B', 'Mario',       'elvicio'        ,'1/1/2008')  -- keep 
INSERT @Table_source VALUES ('008610321','A','A','B', 'Samanta' ,    'Cohen'           ,'1/1/2008') -- keep



Declare @Table_source2 table (pn_id char(9), S nchar(2),C nchar(2),L nchar(2),first_nm varchar(10))
INSERT @Table_source VALUES ('987654321','A','A','B', 'Pamella'      ,'Slattery' )  -- Correct
INSERT @Table_source VALUES ('123456789','N','V','B', 'Dan'         ,'Kramer' )  -- Correct

Select * from @Table_source1
Select * from @Table_source2




Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>