Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Return everything not in other table? 1

Status
Not open for further replies.

SQLNewbiexaus

Technical User
Feb 18, 2011
3
NZ
Iam sure this a simple one, but I still need help please??
I have these 2 script tables but I need to end up with one table which will return all records from the second table that are not in the first, so has been a status 97 but never been a status 5. Not sure if I should be nesting or putting in temp tables as I want to manipulate the data again from there.

Select Distinct
*
From
History (nolock)
Where
StatusCode = 5
And
DocumentType = 'VN'

Select Distinct
*
From
History (nolock)
Where
StatusCode = 97
And
DocumentType = 'VN'

 
Code:
SELECT *
FROM  (SELECT * FROM HISTORY WHERE STATUSCODE = 5 AND DOCUMENTTYPE = 'VN') H1
       LEFT OUTER JOIN
      (SELECT * FROM HISTORY WHERE STATUSCODE = 97 AND DOCUMENTTYPE = 'VN') H2
      ON
      H1.KEYFIELD = H2.KEYFIELD
WHERE H2.KEYFIELD IS NULL

I have used H1.KEYFIELD AND H2.KEYFIELD beacuse there are no field names in your example but you should be able to swap them for the right filed nams.

I love deadlines. I like the whooshing sound they make as they fly by
Douglas Adams
(1952-2001)
 
Try something like that:

Code:
Select Distinct
      * 
From
      History 
Where
      StatusCode = 97 
And
      DocumentType = 'VN'
And
      HistoryID not in 
              (Select Distinct 
                     HistoryID
               From
                     History
               Where
                     StatusCode = 5
               And
                     DocumentType = 'VN')

I hope this help.



Imobiliárias em Suzano
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top