开发者

Issue with rewriting an Access Query in SQL server

开发者 https://www.devze.com 2023-03-19 02:52 出处:网络
I am currently trying to replicate an MS Access query in SQL server and I have some issues going on with this query.

I am currently trying to replicate an MS Access query in SQL server and I have some issues going on with this query.

I am running getting too many records in SQL server with my new query. Original MS Access query - produces 64632 records

SELECT DISTINCT * FROM Deb D INNER JOIN Bals B ON D.ID = B.ID
WHERE 
(
    D.ID > 9999999
    AND (B.TYPE <> 'DV' Or B.TYPE Is Null)
    AND (D.SEC < 9999800 And D.SEC <> 9999000 And D.SEC <>9999100)
    AND (B.BR<>'C2' Or B.BR<>'TA')
)
OR
(
    D.ID > 9999999
    AND (B.TYPE <> 'DV' Or B.TYPE Is Null)
    AND (D.SEC < 9999800 And D.SEC <> 9999000 And D.SEC <>9999100)
    AND B.BR Is Null
)

New SQL version produces 64658 records

SELECT DISTINCT * FROM Deb D INNER JOIN Bals B ON D.ID = B.ID
WHERE 
    D.ID > 9999999
    AND (B.TYPE <> 'DV' Or B.TYPE Is Null)
    AND (D.SEC < 9999800 And D.SEC <> 9999000 And D.SEC <>9999100)
    AND (
            (B.BR<>'C2' Or B.BR<>'TA')
            Or
            (B.BR Is Null)

        )

As you can see I combined the items from the 2 original OR statements in开发者_如何学Python the WHERE clause because the only difference was this

(B.BR<>'C2' Or B.BR<>'TA')
Or
(B.BR Is Null)

I know the problem is coming from B.BR<>'C2' Or B.BR<>'TA' in SQL server. It is still including the items with the 'C2' and 'TA' values even though they should be excluded. I cannot figure out how to correct this. Suggestions?


It's hard to determine which extra rows you are getting, but I suspect they are rows where B.BR is either 'C2' or 'TA'. Since the following cannot eliminate both values at the same time:

B.BR <> 'C2' OR B.BR <> 'TA'

Try the following instead:

B.BR NOT IN ('C2', 'TA')
0

精彩评论

暂无评论...
验证码 换一张
取 消