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

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

Related

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

Count(*) return 1 or zero

In one of the usecase I need a query which should return 1 based on condition also if not match it should return 0
In Descpriont column if the 'SAP' count is exactly 1 then the query should return 1 else it should return 0
Note : There might be a chance that SAP could be present any number of times in Description column.
Could someone help me out here !!
Thanks.
I tried below query :
SELECT 1 from TableName where Description ='SAP' having count(*)>1
It is returning 1 but not return 0 if the count is more than 1 or no match found.
Use CASE WHEN to decide whether to show 0 or 1.
select case when count(*) = 1 then 1 else 0 end as sap_count_is_1
from mytable
where description = 'SAP';
use case when
select
case when sum(case when description='SAP' then 1 else 0 end)=1 then 1 else 0 end from table_name

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,

Even or odd in SQL

This is table structure
id
1
2
3
4
5
6
I need result like this
id even odd
1 0 1
2 1 0
3 0 1
4 1 0
5 0 1
6 1 0
I tried
select id %2=0 then 1 else 0 end or id%2 <>0 then 1 else 0 odd
from table
How about
select
id,
~id & 1,
id & 1
from t
Take a look at the CASE keyword. It works very similarly to what you're trying to do in your SELECT statement. In addition, if you want to select multiple columns, separate them with a comma. The OR keyword is used for combining logical conditions in your query, not for specifying multiple columns.
An example of how you could use CASE in your query would be as follows:
SELECT id,
CASE WHEN id %2=0 THEN 1 ELSE 0 END AS Even,
[column2]
FROM [TableName]
The table structure is just Id?
you could try this!
select *,
case when id %2=0 then 1 else 0 end as even,
case when id %2 <>0 then 1 else 0 end as odd
from table
You have the right idea, but your syntax is a bit off. I'd use a CASE statement to create the even column, and then a calculate odd accordingly:
SELECT id, even, ABS(even - 1) AS odd
FROM (SELECT id, CASE WHEN id % 2 = 0 THEN 1 ELSE 0 END AS even
FROM my_table)

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