Skip to content
Advertisement

SQL Logic Check

I’m new to SQL/Teradata. The numbers I got from the 2nd query are not what I expected, which leads me to believe I am doing something wrong.

I have two conditions I want to count by:

Condition 1:

owner_id= 'x'
AND
acct_mgr_user_id= 'x'

Condition 2 (All other Permutations):

owner_id<> 'x'
acct_mgr_user_id= 'x'


owner_id= 'x'
acct_mgr_user_id<> 'x'


owner_id<> 'x'
acct_mgr_user_id<> 'x'

Query for Condition 1:

SELECT Count(acct_id) FROM nr_sf_acct
WHERE owner_id= 'x'
AND acct_mgr_user_id= 'x'
AND is_sf_acct_del_y_n= 'n';

I think this one is wrong . . .

Query for Condition 2

SELECT Count(acct_id) FROM nr_sf_acct
WHERE owner_id<> 'x'
OR acct_mgr_user_id<> 'x'
AND is_sf_acct_del_y_n= 'n';

In the query for set 2 I want to return a count for “all other permutations” as noted above.

‘x’ refers to a unique Id number within the table.

Any help is much appreciated.

Advertisement

Answer

You want to negate the 1st condition, so do:

WHERE (NOT (owner_id = 'x' AND acct_mgr_user_id = 'x')) AND (is_sf_acct_del_y_n = 'n')

or:

WHERE (owner_id <> 'x' OR acct_mgr_user_id <> 'x') AND (is_sf_acct_del_y_n = 'n')
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement