Rewriting case when in sql - sql

Hi Im trying to rewrite the following code that uses CASE WHEN. I was thinking that I can instead use decode or something else?
The code:
create table want as select
case when (Var1<20 ) then 1
when (40>Var1>=20 ) then 2
when (Var1>=40 ) then 3
else .
end as Var1

This is more simply written as:
create table want as
select (case when Var1 < 20 then 1
when Var1 < 40 then 2
when Var1 >= 40 then 3
else NULL
end) as Var1

decode() only supports equality. One of the reasons why we welcomed case() when it was introduced back in the day was precisely because it allowed us to test greater than and less than.
However it is possible to nest decode() calls and combine them with other functions to achieve the same outcome. This ...
select id
, var1
, decode(greatest(var1,20), 20, 1,
decode(least(var1,40), 40, 3, 2 )) as trans_var
from tab
/
... implements the logic of your case() statement:
select id
, var1
, (case when Var1 < 20 then 1
when Var1 < 40 then 2
when Var1 >= 40 then 3
else NULL
end) as trans_var
from tab
/
SQL Fiddle demo.

Related

Select values above and below limit with same query?

I have a table witch hold random numbers - for example (1, 2, 4, 6, 8, 10).
Is it possible to select the sum of numbers that are below 5 and the sum of numbers that are above 5 within one query?
Something like:
SELECT sum(numbers_below_five), sum(numbers_above_five) from numbers
If it is possible then is there point in doing so or would two queries faster?
You can use a case statement for this:
SELECT
SUM (CASE WHEN n > 5 THEN n ELSE 0 END) as SumGreaterThanFive,
SUM (CASE WHEN n <= 5 THEN n ELSE 0 END) as SumLessThanFive
FROM myTable
Assuming that accessing your RDBMS requires a network roundtrip, a single query should be faster than two queries.
SELECT sum(case when num_column < 5 then num_column end) as numbers_below_five,
sum(case when num_column > 5 then num_column end) as numbers_above_five
from numbers

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)

SQL: Order by statement with start value

I have a field like this:
1月~3月
12月~1月
3月~12月
4月~12月
9月~8月
6月~7月
How can i sort that column following:
4月~12月
6月~7月
9月~8月
12月~1月
1月~3月
3月~12月
It start by 4 and end by 3 (4-5-6-7-8-9-10-11-12-1-2-3)(month)
This will do it, you need to separate out the numeric portion of your field, and also use a CASE statement:
SELECT *
FROM Table1
ORDER BY CASE WHEN CAST(LEFT(Col1, CHARINDEX('月',Col1)-1)AS INT) >= 4 THEN 1 END DESC
,CAST(LEFT(Col1, CHARINDEX('月',Col1)-1)AS INT)
Demo: SQL Fiddle
SQL Server syntax above, might vary depending on database.
order by case when col < 4 then 1 else 0 end, col
or if it's really a varchar
order by case when convert(int,substring(col,1,1)) < 4 then 1 else 0 end, col

SQL - Find the binary representation from the place of '1's

It is really hard to find a good title for this.
Here is the question: I have a SELECT query GROUP BY a field which returns me up to three values (1,2,3). These values are representing the positions of '1' in a binary number.
In other words:
Query Output | Reult
0,1,2 | 7 (111)
1,2 | 6 (110)
3 | 1 (001)
- | 0 (000)
Ok, I know it is easy. But there are two constraints. First, I want a query not a function/store procedure. Second, the result should be a string (like '010') not the number.
I found the solution for integer value, but not the string (varchar)
SELECT COALESCE(sum(power(2, field)), 0) AS test FROM (
SELECT field FROM myTable GROUP BY field) a
I am using SQL server 2008, just in case.
I also have this solution, but this one cannot be extended to bigger number of outputs:
SELECT output =
CASE TEST
WHEN 0 THEN '000'
WHEN 1 THEN '001'
WHEN 2 THEN '010'
WHEN 3 THEN '011'
WHEN 4 THEN '100'
WHEN 5 THEN '101'
WHEN 6 THEN '110'
WHEN 7 THEN '111'
END
FROM(
select COALESCE(sum(power(2, 3 - field)), 0) as test from (
select field from myTable group by field) a) b
You can use binary and and string concatenation:
select (case when test&4 > 0 then '1' else '0' end) +
(case when test&2 > 0 then '1' else '0' end) +
(case when test&1 > 0 then '1' else '0' end)
from (select 6 as test) t;
If you are allergic to case statements, you could do this:
select CHAR(ascii(0) + (test&4)/4) +
CHAR(ascii(0) + (test&2)/2) +
CHAR(ascii(0) + (test&1)/1)
from (select 6 as test) t

SQL (TSQL) - Select values in a column where another column is not null?

I will keep this simple- I would like to know if there is a good way to select all the values in a column when it never has a null in another column. For example.
A B
----- -----
1 7
2 7
NULL 7
4 9
1 9
2 9
From the above set I would just want 9 from B and not 7 because 7 has a NULL in A. Obviously I could wrap this as a subquery and USE the IN clause etc. but this is already part of a pretty unique set and am looking to keep this efficient.
I should note that for my purposes this would only be a one-way comparison... I would only be returning values in B and examining A.
I imagine there is an easy way to do this that I am missing, but being in the thick of things I don't see it right now.
You can do something like this:
select *
from t
where t.b not in (select b from t where a is null);
If you want only distinct b values, then you can do:
select b
from t
group by b
having sum(case when a is null then 1 else 0 end) = 0;
And, finally, you could use window functions:
select a, b
from (select t.*,
sum(case when a is null then 1 else 0 end) over (partition by b) as NullCnt
from t
) t
where NullCnt = 0;
The query below will only output one column in the final result. The records are grouped by column B and test if the record is null or not. When the record is null, the value for the group will increment each time by 1. The HAVING clause filters only the group which has a value of 0.
SELECT B
FROM TableName
GROUP BY B
HAVING SUM(CASE WHEN A IS NULL THEN 1 ELSE 0 END) = 0
If you want to get all the rows from the records, you can use join.
SELECT a.*
FROM TableName a
INNER JOIN
(
SELECT B
FROM TableName
GROUP BY B
HAVING SUM(CASE WHEN A IS NULL THEN 1 ELSE 0 END) = 0
) b ON a.b = b.b