Normalizing Data on a Table - sql

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

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

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

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,

Join Two Select Statements With Different Columns And Condition

I'm not good at asking question, so i'll give an example of what i want to have.
if i = 1 and xi = 0 then
select a,b,c,d,e,f,g where z = 1
elseif i=0 and xi = 1 then
select a,c,f,h,l,n where w = var
elseif i=1 and xi=1 then
select a,b,c,d,e,f,g, where z = 1
union all
select a,c,f,h,l,n where w = var
end if
How can I join the 2 select statement if their columns are not equal and they both have a unique condition?
Based on the conditions you can create derived tables to fetch desired columns and then to get a union of the two tables add null values in column list of derived tables which have less number of columns:
Pseudo code:
select * from
(select a,b,c,d,e,f,g
where z = 1
and 1 = case when i = 1 and xi = 0 then 1
when i = 1 and xi = 1 then 1
else 0
end) as T1
union all
(select a,c,f,h,l,n ,null -- add null value to equate number of columns
where w = var
and 1 = case when i=0 and xi = 1 then 1
when i=1 and xi = 1 then 1
else 0
end) as T2
Hope this helps!!!
If it is not a requirement not to use dynamic sql I will opt for that one.
Another idea will be to use user defined function returnin tables.
So you encapsulate there the logic...

subtract operation on two rows of temp table in SQL server

Age16_20 AgeBelow_16 Age21_30 Age31_40 Age41_50 Age51_60 QID
93 81 55 46 54 48 1
13 11 15 16 14 18 2
I want to subtract second row from first? which is best way to do inside stored procedure. performance main issue here so please provide optimal solution.
SELECT
[qid1].AgeBelow16 - [qid2].AgeBelow16 AS [AgeBelow16],
[qid1].Age16_20 - [qid2].Age16_20 AS [Age16_20],
[qid1].Age21_30 - [qid2].Age21_30 AS [Age21_30],
[qid1].Age31_40 - [qid2].Age31_40 AS [Age31_40],
[qid1].Age41_50 - [qid2].Age41_50 AS [Age41_50],
[qid1].Age51_60 - [qid2].Age51_60 AS [Age51_60]
FROM
MyTable AS [qid1]
INNER JOIN
MyTable AS [qid2]
ON [qid1].QID = [qid2].QID - 1
WHERE
[qid1].QID = 1
If possible, however, you would be much better off storing the QID2 values as negatives. That way you don't need to know which one to subtract from the other; it's just a straight SUM.
SELECT
SUM(AgeBelow16) AS [AgeBelow16], -- (93) + (-13) = 80
SUM(Age16_20) AS [Age16_20], -- (81) + (-11) = 70
SUM(Age21_30) AS [Age21_30], -- (55) + (-15) = 40
SUM(Age31_40) AS [Age31_40], -- (46) + (-16) = 30
SUM(Age41_50) AS [Age41_50], -- (54) + (-14) = 40
SUM(Age51_60) AS [Age51_60] -- (48) + (-18) = 30
FROM
MyTable
Assuming you have a unique identifier on the rows (we'll presume it is ID) you could do something like (I have only done the first column, for brevity):
SELECT SUM(Age16_20) FROM
(SELECT Age16_20
FROM table
WHERE ID = 1
UNION ALL
SELECT -Age16_20
FROM table
WHERE ID = 2) Temp
Assumes only 2 rows.
SELECT
SUM(CASE WHEN ID = 2 THEN -Age16_20 ELSE Age16_20 END),
SUM(CASE WHEN ID = 2 THEN -AgeBelow_16 ELSE AgeBelow_16 END),
SUM(CASE WHEN ID = 2 THEN -Age21_30 ELSE Age21_30 END),
...etc
FROM
Mytable
If more then 2 rows (why?) then change the CASE to
CASE ID WHEN 1 THEN Age16_20 WHEN 2 THEN -Age16_20 ELSE NULL END