0

I am trying to create a view with a right excluding join:

enter image description here

However there is a colon that i also want to do a WHERE on. In this case its:

WHERE table.colon != 'x';

for that reason i have tried:

WHERE A.key IS NULL AND table.colon != 'x'; 

but when i run it, no data is shown when i run it. Can anyone help me with this issue?

Thanks in advanced.

UPDATE, here is the query i am trying to run:

SELECT     dbo.DimServere.Servernavn, dbo.DimServere.Serverstatus, dbo.IderaPatchAnalyzer.IP_Adresse, dbo.IderaPatchAnalyzer.Release_, dbo.IderaPatchAnalyzer.Level_, 
                      dbo.IderaPatchAnalyzer.Edition_, dbo.IderaPatchAnalyzer.Build, dbo.IderaPatchAnalyzer.Updates_Available, dbo.IderaPatchAnalyzer.Supported_, 
                      dbo.IderaPatchAnalyzer.Support_Status
FROM         dbo.DimServere RIGHT OUTER JOIN
                      dbo.IderaPatchAnalyzer ON dbo.DimServere.TcpIpAddress = dbo.IderaPatchAnalyzer.IP_Adresse AND dbo.DimServere.Serverstatus <> 'Disposed/Retired'
WHERE     (dbo.DimServere.TcpIpAddress IS NULL)
PuchuKing33
  • 381
  • 3
  • 7
  • 19

2 Answers2

1

Move the condition into the JOIN clause.

SELECT *
FROM 
    Table1
    LEFT JOIN Table2
        ON Table1.Field = Table2.Field AND
        Table2.OtherField != 'x'
WHERE Table2.Field IS NULL
Rabbit
  • 507
  • 3
  • 9
  • hmm i have runned it and still no result – PuchuKing33 Nov 18 '15 at 00:54
  • What do you mean by no result? No rows come back at all? Or unexpected results are returned? – Rabbit Nov 18 '15 at 17:22
  • No rows come back at all. Just blank. As you can see i have updated the post with the query. Incl what you told me to try. – PuchuKing33 Nov 18 '15 at 17:24
  • That should only happen if for every tcp ip address in dbo.DimServere there is a matching tcp ip address in dbo.IderaPatchAnalyzer and none of them have a exactly equal to 'Disposed/Retired'. I notice there's a slash in the status, is that 2 different statuses or just one status? – Rabbit Nov 18 '15 at 17:34
  • There are a match. When i remove where != 'disposed/retired' it works. And its one status. – PuchuKing33 Nov 18 '15 at 17:37
  • Why do you return fields in the SELECT clause from the dbo.DimServere table? If the query is working like you expect, those fields will always be null. – Rabbit Nov 18 '15 at 17:51
0

The idea is correct. However, i guess there are null values in the dbo.DimServere.Serverstatus column. Because you are using an comparison operator, this condition always fails (comparison with null is unknown) and the join condition is not satisfied, (because you are ANDing it with another condition) and you don't get any results.

To overcome this, use isnull(dbo.DimServere.Serverstatus, 'somevalue') in the join condition.

SELECT dbo.DimServere.Servernavn, dbo.DimServere.Serverstatus,
dbo.IderaPatchAnalyzer.IP_Adresse, dbo.IderaPatchAnalyzer.Release_,
dbo.IderaPatchAnalyzer.Level_, dbo.IderaPatchAnalyzer.Edition_, 
dbo.IderaPatchAnalyzer.Build, dbo.IderaPatchAnalyzer.Updates_Available,    
dbo.IderaPatchAnalyzer.Supported_, dbo.IderaPatchAnalyzer.Support_Status
FROM dbo.DimServere RIGHT OUTER JOIN
dbo.IderaPatchAnalyzer ON dbo.DimServere.TcpIpAddress = dbo.IderaPatchAnalyzer.IP_Adresse 
AND isnull(dbo.DimServere.Serverstatus, 'somevalue') <> 'Disposed/Retired' 
WHERE dbo.DimServere.TcpIpAddress IS NULL
Vamsi Prabhala
  • 48,685
  • 4
  • 36
  • 58