SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Where condition not equal to multiple columns - PUZZLE - who can solve this?


Where condition not equal to multiple columns - PUZZLE - who can solve this?

Author
Message
Mach1000
Mach1000
Forum Newbie
Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)

Group: General Forum Members
Points: 5 Visits: 121
Concerning the puzzle below, need a SQL guru to tell me how they solved this problem.
Goal:
To get statement 2 to produce the same results as statement 1 without having to string together the columns.

DECLARE @Table1 TABLE
(
Column1 int IDENTITY(1,1),
Column2 varchar(12),
Column3 varchar(12)
)

INSERT INTO @Table1 (Column2, Column3) VALUES ('UK','U')
INSERT INTO @Table1 (Column2, Column3) VALUES ('AP','U')
INSERT INTO @Table1 (Column2, Column3) VALUES ('AP','O')
INSERT INTO @Table1 (Column2, Column3) VALUES ('UK','O')

--Statement 1
select * from @Table1
where (column2 + column3 != 'APU')

--Statement 2
select * from @Table1
where (column2 != 'AP' and column3 != 'U')
Phil Parkin
Phil Parkin
SSCoach
SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)

Group: General Forum Members
Points: 18812 Visits: 20459
--Statement 2
select * from @Table1
where (column2 != 'AP' or column3 != 'U')




Help us to help you. For better, quicker and more-focused answers to your questions, consider following the advice in this link.

If the answer to your question can be found with a brief Google search, please perform the search yourself, rather than expecting one of the SSC members to do it for you.

Please surround any code or links you post with the appropriate IFCode formatting tags. It helps readability a lot.
Evil Kraig F
Evil Kraig F
SSCrazy Eights
SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)

Group: General Forum Members
Points: 8651 Visits: 7660
EDIT: Scratch this, bad code. Sorry about that.


- Craig Farrell

Never stop learning, even if it hurts. Ego bruises are practically mandatory as you learn unless you've never risked enough to make a mistake.

For better assistance in answering your questions | Forum Netiquette
For index/tuning help, follow these directions. |Tally Tables

Twitter: @AnyWayDBA
Evil Kraig F
Evil Kraig F
SSCrazy Eights
SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)

Group: General Forum Members
Points: 8651 Visits: 7660
*facepalm* sorry about the earlier code. Try this again:

Alternatively:


SELECT
*,
CASE WHEN column2 <> 'AP' THEN 1 ELSE 0 END AS APtest,
CASE WHEN column3 <> 'U' THEN 1 ELSE 0 END AS Utest,
CASE WHEN column2 <> 'AP' AND column3 <> 'U' THEN 1 ELSE 0 END AS APUtest
FROM
@Table1
WHERE
NOT ( column2 = 'AP' AND column3 = 'U')




- Craig Farrell

Never stop learning, even if it hurts. Ego bruises are practically mandatory as you learn unless you've never risked enough to make a mistake.

For better assistance in answering your questions | Forum Netiquette
For index/tuning help, follow these directions. |Tally Tables

Twitter: @AnyWayDBA
Lynn Pettis
Lynn Pettis
SSC-Dedicated
SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)

Group: General Forum Members
Points: 39968 Visits: 38564
Evil Kraig F (1/17/2013)
*facepalm* sorry about the earlier code. Try this again:

Alternatively:


SELECT
*,
CASE WHEN column2 <> 'AP' THEN 1 ELSE 0 END AS APtest,
CASE WHEN column3 <> 'U' THEN 1 ELSE 0 END AS Utest,
CASE WHEN column2 <> 'AP' AND column3 <> 'U' THEN 1 ELSE 0 END AS APUtest
FROM
@Table1
WHERE
NOT ( column2 = 'AP' AND column3 = 'U')



Phil Parkin (1/17/2013)



--Statement 2
select * from @Table1
where (column2 != 'AP' or column3 != 'U')





For those not versed in boolean logic:

NOT ( column2 = 'AP' AND column3 = 'U') -->
NOT(column2 = 'AP') OR NOT(column3 = 'U') -->
column2 != 'AP' or column3 != 'U'

Which means: NOT ( column2 = 'AP' AND column3 = 'U') is equivalent to: column2 != 'AP' or column3 != 'U'.

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Mach1000
Mach1000
Forum Newbie
Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)

Group: General Forum Members
Points: 5 Visits: 121
Phil,
Thanks for the help. I had tried the or logic, but something else must have affected the results.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search