How do I compare two columns for equality in SQL Server?
What's wrong with CASE for this? In order to see the result, you'll need at least a byte, and that's what you get with a single character.
CASE WHEN COLUMN1 = COLUMN2 THEN '1' ELSE '0' END AS MyDesiredResult
should work fine, and for all intents and purposes accomplishes the same thing as using a bit field.
Redshift SQL: Check if same set of values are IN and NOT IN two different columns of table
"WHERE NOT IN" can create inefficient executions but this is so simple that I hope the Redshift query compiler should optimize this code. If the "IN" list gets long this can get very slow. Generally "WHERE NOT EXIST" is more efficient than "WHERE NOT IN" but again in this simple case it should be noticeable.
For something this simple (2 values) you can try -
buyers in ('EXAMPLE 1','EXAMPLE 2') and sellers <> 'EXAMPLE 1' and sellers <> 'EXAMPLE 2'
and see if there is any speed up. I hope this make no difference.
Checking multiple columns for one value
You can use the IN
predicate, like so:
SELECT * FROM table WHERE 123 IN(col1, col2, col3, col4);
SQL Fiddle Demo
it's the opposite version of IN.
No it is not, It is the same as using the OR
s the way you did in your question.
To clarify this:
The predicate IN
or set membership is defined as1:
Where the Value Expression
can be either 2:
So it is fine to do it this way, using the value expression 123
, which is a literal.
1, 2: Images from: SQL Queries for Mere Mortals(R): A Hands-On Guide to Data Manipulation in SQL
SQL WHERE.. IN clause multiple columns
You can make a derived table from the subquery, and join table1 to this derived table:
select * from table1 LEFT JOIN
(
Select CM_PLAN_ID, Individual_ID
From CRM_VCM_CURRENT_LEAD_STATUS
Where Lead_Key = :_Lead_Key
) table2
ON
table1.CM_PLAN_ID=table2.CM_PLAN_ID
AND table1.Individual=table2.Individual
WHERE table2.CM_PLAN_ID IS NOT NULL
Related Topics
Fill a Field With the Values from Another Field in Ms Access
Multiple Query Same Table But in Different Columns MySQL
Remove Multiple Keys from Jsonb Column in One Statement
Remove Numbers from String SQL Server
Sql Select All Data That Past 45 Days
Subtract Two Columns from Two Tables With Group By
Convert String to Date in Ms Access Query
Find Out Where MySQL Is Installed on MAC Os X
Check If a Row Exists, Otherwise Insert
Sql Query for Values Consisting of Only a Specific Character
Mysql Inner Join Select Only One Row from Second Table
How to Get Latest Records for Each Ledger Account Based on Variationid
Laravel Eloquent Mutiple Counts With Different Conditions in One Query
Splitting String into Multiple Rows in Oracle
Way to Find Data of a SQL Table With Same Status for Consecutive 3 Days
Unable to Get Min and Max Time from a Datetime Column from Every Backday
Sequelize Connect Etimeout When Connecting to Remote MySQL Db