Changing when to if statement in SQL server - sql

How do I change the following code to an if statement that returns a boolean 0 or 1 value? My end results I would like to have, is one column listing the interest rate of 2, and my results column with a 0 or 1 if the condition is true.
(Case when new_interestratevariability = 2
and (new_interestrateindex = 1 or new_interestrateindex = 2 or new_interestrateindex = 3 or new_interestrateindex = 4 or new_interestrateindex = 6)
and new_crms_dt = #Curr_Date
then 0 else 1 end) as CIEDIT_VAL_96,
Currently, I am getting something like below:
Results Table

To filter rows, use a Where clause. The Case statement in the Select clause will modify the value shown on the row.
Select *
from table
Where new_interestratevariability = 2
and new_interestrateindex IN (1,2,3,4,6)
and new_crms_dt = #Curr_Date

Found my answer, it was as simple as adding "not in" instead of just "in". Thanks everyone
(Case when new_interestratevariability = 2
and (new_interestrateindex not in(1,2,3,4,6))
and new_crms_dt = #Curr_Date
then 1 else 0 end) as CIEDIT_VAL_96,

Related

Can multiple 'OR' statements be combined if they have the same condition?

I have the below OR statement with each column = 1 as the condition. Is there a shorter way of doing this? I have tried HAVING and IN but neither worked.
(count(distinct case when (fiscal_mth_idnt_june21 = 1 or fiscal_mth_idnt_july21 = 1 or fiscal_mth_idnt_august21 = 1) then contact_key end)) / sum(fiscal_mth_idnt_may21)*100 as within_three_months
is the main code I am interested in.
The boolean expression:
fiscal_mth_idnt_june21 = 1 or fiscal_mth_idnt_july21 = 1 or fiscal_mth_idnt_august21 = 1
is equivalent to:
1 IN (fiscal_mth_idnt_june21, fiscal_mth_idnt_july21, fiscal_mth_idnt_august21)

How to include attribute created by CASE WHEN in the WHERE clause in HIVE?

This is a simple example of the problem:
SELECT CASE WHEN a = 5 THEN 0 ELSE 1 END AS x
FROM table
WHERE x = 0;
I will get an error message that x is not amongst the possible column names.
Could anyone advise me how to solve this?
SELECT
CASE WHEN a = 5 THEN 0
ELSE 1
END AS x FROM table
WHERE
CASE WHEN a = 5 THEN 0
ELSE 1
END=0

SQL How to use IF query to create a new column from others

I have tables where I have a result of 0-1 0 is gone and 1 is. I need to write a query, and I totally don't know how to. If New_Def = 0 a Default = 1 then 'NEW' If New_Def = 1 a Default = 1 then = OLD.
I think you are looking for case :
select * , case when New_Def = 0 and Default = 1 then 'NEW'
when New_Def = 1 and Default = 1 then 'OLD'
else 'Unknown'
end as Newcolumn
from table

Update Table with Aggregate function in where clause Bigquery

I have TableA with the following data
Name A B Final
Andy 1 1 2
Sam 1 0 2
I want to write an update query which will do the following:
If sum(A+B) <> Final then update row else do not Update
My Query
Update TableA
Set A = Case when Final in (1,2) then 1 else 0 End
Set B = Case When Final = 2 then 1 else 0
where final in (1,2) and **final <> sum(A+B)**
Since we can't use an aggregate function in update where clause I am not sure how to do the last part.
The query should only update row for Sam.
Thanks for you help!
Update TableA
Set A = Case when Final in (1,2) then 1 else 0 End
Set B = Case When Final = 2 then 1 else 0
where final in (1,2) and final <> A+B

query sql returning custom value

I have a table with 3 columns (containing Integer values that assume only values from 0 to 10). I want extract, with a single query, a table with 1 column. This column must assume a value based on the following logic:
If one of these three columns has value 0 ----> the value of column of table generated by query must be 0 too.
If none of the last three columns has value 0 ----> the value of column must assume the value 1.
You are looking for CASE construct or IF function:
SELECT CASE WHEN (t.field1 = 0 OR t.field2 = 0 OR t.field3 = 0) THEN 0
ELSE 1 END AS value
FROM t;
In this specific case you might also use the fact that any member being zero will zero the product:
SELECT CASE WHEN (t.field1*t.field2*t.field3 = 0) THEN 0 ELSE 1 END AS value
FROM t;
Or
SELECT IF((t.field1*t.field2*t.field3)=0, 0, 1) AS value FROM t;
This is a simple case statement. Assuming there are no NULL values, try this:
select (case when col1 = 0 or col2 = 0 or col3 = 0 then 0 else 1 end)
Try this
SELECT
CASE
WHEN column1 = 0 THEN 0
WHEN column2 = 0 THEN 0
WHEN column3 = 0 THEN 0
ELSE 1
END
FROM urtable