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.
| Author |
Topic |
|
ZMike
Posting Yak Master
110 Posts |
Posted - 2008-08-22 : 17:39:37
|
| I'm having trouble with the following scenario.I have View1 and view2.. I want to see the account #s that do not match from file 1 and file 2. It sounds like I wanted to do a left join since View1 should have every account in it that I'd need. But if there is no match in View2 then those are the accounts that I want to see. Here is what I have. I'm guessing that I need to use the Not Exist but I've tried that a few ways to no avail.Any help would be greatly appriciated.SELECT DISTINCT Balancing_Part1.SiteID, Balancing_Part1.Account#, Balancing_Part1.Equipment_Status, Balancing_Part1.Equip_Detail_Status, Balancing_Part1.Item#, Balancing_Part1.Serial#, Balancing_Part1.Equipment_Type, Balancing_Part1.Service_Category_Code, Balancing_Part1.Service_Occurence, Balancing_Part1.Disconnect_DateFROM Balancing_Part1 LEFT JOIN Balancing_Part2 ON Balancing_Part1.Account# = Balancing_Part2.Account_Number AND Balancing_Part1.SiteID = Balancing_Part2.Site_IDWHERE Balancing_Part1.Account# <> Balancing_Part2.Account_Number ORDER BY Balancing_Part1.Account# |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2008-08-22 : 17:46:35
|
You can do something like:DECLARE @View1 TABLE(ID INT)DECLARE @View2 TABLE(ID INT)INSERT @View1SELECT 1UNION ALL SELECT 2UNION ALL SELECT 3UNION ALL SELECT 5UNION ALL SELECT 7UNION ALL SELECT 8INSERT @View2SELECT 1UNION ALL SELECT 2UNION ALL SELECT 4UNION ALL SELECT 5UNION ALL SELECT 9UNION ALL SELECT 10SELECT COALESCE(V1.ID, V2.ID) AS IDFROM @View1 AS V1FULL OUTER JOIN @View2 AS V2 ON V1.ID = V2.IDWHERE V1.ID IS NULL OR V2.ID IS NULL |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-08-23 : 01:09:05
|
quote: Originally posted by ZMike I'm having trouble with the following scenario.I have View1 and view2.. I want to see the account #s that do not match from file 1 and file 2. It sounds like I wanted to do a left join since View1 should have every account in it that I'd need. But if there is no match in View2 then those are the accounts that I want to see. Here is what I have. I'm guessing that I need to use the Not Exist but I've tried that a few ways to no avail.Any help would be greatly appriciated.SELECT DISTINCT Balancing_Part1.SiteID, Balancing_Part1.Account#, Balancing_Part1.Equipment_Status, Balancing_Part1.Equip_Detail_Status, Balancing_Part1.Item#, Balancing_Part1.Serial#, Balancing_Part1.Equipment_Type, Balancing_Part1.Service_Category_Code, Balancing_Part1.Service_Occurence, Balancing_Part1.Disconnect_DateFROM Balancing_Part1 LEFT JOIN Balancing_Part2 ON Balancing_Part1.Account# = Balancing_Part2.Account_Number AND Balancing_Part1.SiteID = Balancing_Part2.Site_IDWHERE Balancing_Part1.Account# <> Balancing_Part2.Account_Number Balancing_Part2.Account_Number IS NULLORDER BY Balancing_Part1.Account#
i think you need to modify like above |
 |
|
|
ZMike
Posting Yak Master
110 Posts |
Posted - 2008-08-24 : 17:37:29
|
| visakh16Thanks, Thats all I think I needed. I had tried that but wasnt at work to check the results and it looks like that was correct. I came back with a few more records then the SQL that I wrote in IBM , but it looks like I get the same records plus a few I wasnt getting in my other query.Thanks ! |
 |
|
|
|
|
|
|
|