Case when then conditions without else - sql

i am trying to do something like below where i dont want consider numbers which are greater then 30 in the below condition but the case condition taking else condition by default and adding them as null in value_a.
CASE WHEN a<10 and a!=0
THEN
a
WHEN a>10 and a<30
THEN
b
END AS value_a
out is as below
value_a
a
b
null
where as i want it to consider values only upto 30 and ignore other numbers
and output be like
value_a
a
b

Since you want to exclude the results where a >= 30 then use where clause to filter those results. And if you need a to be equal to 10 then you are missing the equal sign (adjusted below)
SELECT *,
CASE WHEN a<=10 and a!=0 THEN a
WHEN a>10 and a<30 THEN b
END AS value_a
code 2
select *,
case when transaction_type = 'c' and date < [enter date] THEN A
when transaction_type = 'c' and date > [enter date] THEN B
else end as final_value
FROM [table name]
WHERE transaction_type != 'd'

You will need to add '=' if you want to include 10 in your first condition.
You will also need to add '=' ifd you want to include 30 in your second condition.
At the end you will have to say you do not want to process if it is bigger than 30.
select CASE WHEN id<=10 and id>=0 THEN
'a'
WHEN id > 10 and id<=30 THEN
'b'
END value_a
from A
where id <= 30
Here is a small demonstration for SQLServer: DEMO
I believe it will help even if it is not in HIVEQL

Related

Subtract 2 case statements

I am trying to subtract the 2 case statements like this:
CASE
WHEN fct.measure IN ('A')
THEN fct.month_value
ELSE NULL
END
- CASE
WHEN fct.measure IN ('B')
THEN fct.month_value
ELSE NULL
END AS discounts
This query doesn't throw a syntax error, but it returns all NULL.
The month_value corresponding to A is 3173.100000 and the month value corresponding to B is 8043.000000.
Any suggestions on how this could return the correct result instead of all NULL?
I presume that you need some kind of conditional aggregation approach here:
SELECT
col,
MAX(CASE WHEN measure = 'A' THEN month_value END) -
MAX(CASE WHEN measure = 'B' THEN month_value END);
FROM yourTable
GROUP BY col;
This assumes that your table structure looks something like the following:
col | measure | month_value
1 | A | 3173.10
1 | B | 8043.00
We aggregate by each col value, and then use conditional aggregation to isolate the various month values based on the value of the measure column.

Need to create a case statement for multiple criteria with varchar and date data types

Need to create a case statement for multiple criteria with varchar and date data types, my code sp far:
select
least(coalesce(case when 1.other_id=10 then 1.datestr else 0 end,
case when 2.info='Yes' then 2.date else 0 end))as date1,
from
test1 as 1
left join test2 as 2 on 1.id = 2.id
I essentially want to get the lowest date between 2 conditions. Both conditions have some filters (other_id=10 in first, info="Yes" in second). For those conditions I want to compare least dates.
I think this is the logic:
select (case when 1.other_id = 10 and 2.info = 'Yes'
then least(case when 1.other_id=10 then 1.datestr end,
case when 2.info='Yes' then 2.date end
)
when 1.other_id = 10 then 1.date
else 2.date
end)
Or, you can use more appropriate default values in your expression. Something like this (it might depend on the database):
select least(case when 1.other_id = 10 then 1.datestr else '9999-01-01' end,
case when 2.info = 'Yes' then 2.date else '9999-01-01' end)
) as date1,

Impala SQL, return value if a string exists within a subset of values

I have a table where the id field (not a primary key) contains either 1 or null. Over the past several years, any given part could have been entered multiple times with one, or both of these possible options.
I'm trying to write a statement that will return some value if there is ever a 1 associated with the select statement. There are lots of semi-duplicate rows, some with 1 and some with null, but if there is ever a 1, I want to return true, and if there are only null values, I want to return false. I'm not sure how to code this though.
If this is my SELECT part,id from table where part = "ABC1234" statement
part id
ABC1234 1
ABC1234 null
ABC1234 null
ABC1234 null
ABC1234 1
I want to write a statement that returns true, because 1 exists in at least one of these rows.
The closest I've come to this is by using a CASE statement, but I'm not quite there yet:
SELECT
a1.part part,
CASE WHEN a2.id is not null
THEN
'true'
ELSE
'false'
END AS id
from table.parts a1, table.ids a2 where a1.part = "ABC1234" and a1.key = a2.key;
I also tried the following case:
CASE WHEN exists
(SELECT id from table.ids where id = 1)
THEN
but I got the error subqueries are not supported in the select list
For the above SELECT statement, how do I return 1 single line that reads:
part id
ABC1234 true
You can use conditional aggregation to check if a part has atleast one row with id=1.
SELECT part,'True' id
from parts
group by part
having count(case when id = 1 then 1 end) >= 1
To return false when the id's are all nulls use
select part, case when id_true>=1 then 'True'
when id_false>=1 and id_true=0 then 'False' end id
from (
SELECT part,
count(case when id = 1 then 1 end) id_true,
count(case when id is null then 1 end) id_false,
from parts
group by part) t

select rows from table and check if condition at column in sql

I have to check certain rows from table and check if-else condition for each rows
TABLE: report
COLUMNS :
sl.no, count, total_count, calls
Sample Data:
sl.no count total_count calls
----------- ----------- ----------- -----------
1 2 6 4
2 2 7 5
3 4 9 3
Here i have to check if condition
if total_count > 6
select 'Y
else
select 'N'
I get correct answer for single row. If there is multiple row it can't check all, it check only last row of my table.
Use CASE expression
SELECT
*,
CASE WHEN total_count > 6 THEN 'Yes' ELSE 'N' END
FROM report
You must use CASE.It work like if-else in t-SQL. MSDN
For example:
SELECT [num],[count], [total_count], [calls], CASE WHEN [total_count] > 6 THEN 'Y' ELSE 'N' END FROM t
You could use CASE. You can read documentation.
SELECT *,
CASE WHEN total_count > 6 THEN 'Y' ELSE ' N' END
FROM Report
The SQL version of "inline if" is CASE:
SELECT
*,
CASE WHEN total_count > 6 THEN 'Y'
ELSE 'N'
END AS IsTotalCountGreaterThanSix
FROM YourTable;

Ordering Select clause result in specific way

I need help with writing a select clause query.
For example, lets say I have a query like that:
select value from some_table order by value asc;
as a result I get this:
1
2
3
4
5
6
7
8
9
10
but a special query I want to write, is the one which still will give me sorted values, but will put 5 after 8.
this means I need one value to be out of regular order.
it can be described in other way. lets say I have two groups of numbers (example):
A={ a | 1<=a<=118, a!=78 } B={ b | b>118 }
I have a group C=A U B U {78}
and I need all these values sorted like "A,78,B"
Assuming value is integer, you could do this:
SELECT *
FROM tbl
ORDER BY
CASE
WHEN value = 5 THEN 8.5
ELSE value
END
Or to expand upon DCP's answer...
SELECT *
FROM tbl
ORDER BY
CASE
WHEN (Condition for first grouping) THEN 1
WHEN (Condition for second grouping) THEN 2
WHEN (Condition for third grouping) THEN 3
ELSE 4
END
You can use multiple conditions in your order by:
ORDER BY (value BETWEEN 1 AND 118) AND value != 78 DESC,
value > 118 DESC,
value
This will ensure that values which match the first predicate come first, then values matching the second predicate, and finally values matching none of the predicates. If there is a tie (two numbers matching the same predicate) then these numbers are sorted in ascending order.
Note that I haven't tested this in Oracle. It might be necessary to wrap the predicate in a CASE expression (CASE WHEN predicate THEN 1 ELSE 0 END) to get the sorting to work in Oracle.
ORDER BY
(CASE WHEN ((value BETWEEN 1 AND 118) AND value <> 78) THEN 1 ELSE 0 END) DESC,
(CASE WHEN (value > 118) THEN 1 ELSE 0 END) DESC,
value
Order by some CASE-expression to remap your values.