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 2005 Forums
 Transact-SQL (2005)
 Where !=

Author  Topic 

PBoy
Starting Member

22 Posts

Posted - 2010-05-24 : 17:18:59
Hi All,

Just new to the joys of SQL I have been getting on ok but Im a little stuck and hope to get some help.

What Im lookings to do is to using the where clause if possible to create a query that show a list on transaction on that have a perticual value that does not match, for example we have a table that holds who a person reports to on it, and a table that hold expenses data, however when the expenses do into the workflow process the reports to is picked up and used, but when the expenses is in workflow the reports to could be changed (for what ever reason) is there a way using the where clause to say only show expenses where the reports to in (table one) does not match the reports to (table two)?

I thought maybe something like Where table1.reports_to != table2.reports_to

Would that even work or is it just daft?

Cheers
Patrick

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-05-24 : 17:34:20
Using NOT EXISTS:

SELECT ...
FROM Table1 t1
WHERE NOT EXISTS (SELECT * FROM Table2 t2 ON t1.Column1 = t2.Column1)
...

You can also use an OUTER JOIN:

SELECT ...
FROM Table1 t1
LEFT JOIN Table2 t2
ON t1.Column1 = t2.Column1
WHERE t2.Column1 IS NULL
...

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

PBoy
Starting Member

22 Posts

Posted - 2010-05-26 : 18:30:26
Thanks Tara, I used the NOT EXISTS on a few other tables and it worked great :)
Cheers
Patrick
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-05-26 : 19:15:18
You're welcome, glad to help.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -