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

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

Related

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

Changing when to if statement in SQL server

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,

How to filter Even and Odd numbers and move/copy them to an empty column?

i have a simple problem. i have a column with numbers. i need to filter them to even and odd numbers, but if a number is even than i need to copy or move it to "even-number-column" in the same table. if the number is odd then move it to "odd-number-column" in the same table.
this is my code:
select distinct all_numbers, even-number-column, odd-number-column
case when all_numbers%2=0
then UPDATE my_table SET even-number-column = all_numbers
else UPDATE my_table SET odd-number-column = all_numbers
end
from my_table ;
If you want a SELECT:
select
distinct all_numbers,
CASE
WHEN when all_numbers%2 = 0 THEN all_numbers
ELSE 0 -- or NULL
END as even-number-column,
CASE
WHEN when all_numbers%2 = 1 THEN all_numbers
ELSE 0 -- or NULL
END as odd-number-column
from my_table ;`
If you want an update
UPDATE my_table
SET
even-number-column = CASE
WHEN when all_numbers%2 = 0 THEN all_numbers
ELSE even-number-column -- or change for 0 or NULL
END,
odd-number-column = CASE
WHEN when all_numbers%2 = 1 THEN all_numbers
ELSE odd-number-column -- or change for 0 or NULL
END
Use an array to make it simple
update t
set
odd = (array[null, all_numbers])[all_numbers % 2 + 1],
even = (array[all_numbers, null])[all_numbers % 2 + 1]

SQL - "IF" in Where Clause

I'm trying to write a stored procedure that will have 6 bit value flags as parameters and a couple of other values.
The pseudo sql I want to write is something like:
SELECT *
FROM theTable
WHERE
IF #flagA = 1 THEN theTable.A = 1
IF #flagB = 1 THEN theTable.B = 1
IF #flagC = 1 THEN theTable.CValue = #cValue
etc
Any ideas how I can do this in SQL or am I best reverting to building the SQL in C# (where this SP will be called from)?
SELECT *
FROM theTable
WHERE
(#flagA = 0 or (#flagA = 1 AND theTable.A = 1 ))
and (#flagB = 0 or (#flagB = 1 AND theTable.B = 1 ))
and (#flagC = 0 or (#flagC = 1 AND theTable.CValue = #cValue ))
Note: I am assuming your bit flags are non-nullable. If that is not the case, you will need to use ISNULL.

Normalizing Data on a Table

I need help to transform data on a table, reducing a series of columns to one single column. An example follows below:
Frequency_1 integer,
Frequency_2 integer,
Frequency_3 integer,
Frequency_4 integer,
These columns currently hold 1 or 0. Only one column will hold 1.
The new column should be defined as
Frequency integer
And this new column should hold a value between 1 and 4, depending on which of the old columns had its value = 1.
Could you suggest an SQL command to accomplish this?
You could come up with something more complicated if you want, but why not just do this?
SELECT Frequency_1 +
(Frequency_2 * 2) +
(Frequency_3 * 3) +
(Frequency_4 * 4) AS Frequency
To actually make the change, you can create the column first, update the value in the new column, then delete the old columns.
SELECT
CASE WHEN Frequency_1 = 1 THEN 1
WHEN Frequency_2 = 1 THEN 2
WHEN Frequency_3 = 1 THEN 3
WHEN Frequency_4 = 1 THEN 4
ELSE 0 END AS Frequency
FROM TABLE
update table_name
set frequency =
case when frequency_1 = 1 then 1 else
case when frequency_2 = 1 then 2 else
case when frequency_3 = 1 then 3 else
case when frequency_4 = 1 then 4
end
end
end
end
Like this:
select Frequency =
Frequency_1 * 1 +
Frequency_2 * 2 +
Frequency_3 * 3 +
Frequency_4 * 4
from ATable
or this:
select Frequency = case
when Frequency_1 = 1 then 1
when Frequency_2 = 1 then 2
when Frequency_3 = 1 then 3
when Frequency_4 = 1 then 4
else 0
end
from ATable