Pregunta

I'm new to SQL somewhat so bear with me if this is a n00b question. So my code runs something akin to the following:

(select "Balance."CodeValue" AS "CodeValue"
       , "Balance"."OtherValue" AS "OtherValue" 
from "SomeDB"."dbo"."AValue" "Balance"
where ("Balance"."CodeValue" between 'A' and'Z' 
    or "Balance"."CodeValue" in ('ABCDEFG')) 
and "Balance"."CodeValue" NOT in ('XYZ', '1234', 'Etc') 
or "Balance"."CodeValue" between 'A' and 'Z') "Balance" 
on "SomeMatrix"."CodeValue" = "Balance"."CodeValue"

Reading it, it would seem that it checks for the "Balance"."CodeValue" to be between A and Z or in 'ABCDEFG' and not in 'XYZ', '1234', 'Etc' or between A and Z. Wouldn't the two checks for A and Z cancel each other out?

Thank you ahead of time for your assistance.

¿Fue útil?

Solución 2

As written above you are correct, the first bit isn't doing anything because it's negated by the last bit:

;WITH cte AS (SELECT 'XYZ' AS CodeValue
              UNION 
              SELECT 'A')
SELECT *
FROM cte
WHERE (CodeValue between 'A' and'Z'  or CodeValue in ('ABCDEFG')) 
      AND CodeValue NOT in ('XYZ', '1234', 'Etc') 
      OR CodeValue between 'A' and 'Z' 

Will Return XYZ even though XYZ is listed in the NOT IN portion.

Demonstration: SQL Fiddle

Otros consejos

select Balance.CodeValue AS CodeValue
       ,Balance.OtherValue AS OtherValue 
from SomeDB.dbo.AValue Balance INNER JOIN SomeMatrix
on SomeMatrix.CodeValue = Balance.CodeValue
where 
(
  Balance.CodeValue between 'A' and'Z' ----\
OR                                          -- Either of this is true
  Balance.CodeValue in ('ABCDEFG')     ----/
)     
AND                                    -- AND
(
  Balance.CodeValue 
         NOT IN ('XYZ', '1234', 'Etc')  ----\
OR                                           -- Either of this is true
  Balance.CodeValue between 'A' and 'Z' ----/
) 

The precedence of operator is NOT --> AND --> OR

When you have a bit complex/Tricky NOT INs , ANDs & ORs in your WHERE clause closing related conditions in parenthesis () makes it easier to read and debug your code.

No they wouldnt. SQL works on "restrictions" (litteraly). When you first run query

select * from table

It pulls out all rows from table Then after lets say this query

select * from table where table.column>5

It "hides"(again literally) all rows that are lesser or equal than 5

And so on.So when you type

select * from table table.column>5 AND table.column>5

It hides same values and you get the right result

EDIT: Actually it works on adding rows. Condition table.column>5 AND tavle.column>5 is going to be true or false for every specific row and that settles if row is added in result or not

I think you're confused by the order that "and" and "or are evaluated. Values that meet ANY of these three requirements will be returned:

1) Balance.CodeValue NOT in ('XYZ', '1234', 'Etc'), and Balance.CodeValue between 'A' and'Z'

2) Balance.CodeValue NOT in ('XYZ', '1234', 'Etc'), and Balance.CodeValue in ('ABCDEFG')

3) Balance.CodeValue between 'A' and 'Z'

The last "or" condition (Balance.CodeValue between 'A' and 'Z') is separate from the 3 previous conditions, because "and" is evaluated before "or". The first 3 conditions are kind of glued together.

(Sorry for my sloppy terminology! I know the logic, just not the right words to describe it.)

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top