Returning 1 or 0 in specific SQL query - sql

I have three columns in the table MYTABLE (ID, NUM, NAMES). There is a column NAMES. I need to check on NAMES column to see if the first name is JACK or BRUCE and the corresponding NUM column = 0. If the match is found, return 1 else 0.
ID NUM NAMES
1 1 'TOM'
2 1 'MIKE'
3 0 'JACK'
4 1 'MICKY'
5 0 'BRUCE'
I've came up with the following query:
select *
case NAMES in ('JACK', 'BRUCE') and NUM=0 then 1 else 0 end as MYNAMES
from MYTABLE;
That does not work unfortunately.

This works (SQLFiddle demo):
SELECT id, num,
CASE WHEN names IN ('JACK', 'BRUCE') AND num=0
THEN 1 ELSE 0 END AS mynames
FROM mytable

select case
when exists
(
select *
from YourTable
where name in ('JACK', 'BRUCE')
and NUM = 0
)
then 1
else 0
end
from dual
Live example at SQL Fiddle.

select case when NAMES in ('JACK','BRUCE') AND NUM = 0
then 1
else 0
end
from your_table

Related

SQL AS query results in duplicate column

I have a SQL query like so
SELECT
name,
CASE WHEN (new_value=2) THEN 0 END as out,
CASE WHEN (previous_value=2) THEN 1 END as out
FROM my_table;
This results in duplicate columns:
name out out
foo 1 null
bar null 1
instead of
name out
foo 1
bar 0
How do I fix this?
You want one case expression with two conditions:
SELECT name,
(CASE WHEN new_value = 2 THEN 0
WHEN previous_value = 2 THEN 1
END) as out
FROM my_table;
Consider:
SELECT
name,
CASE
WHEN new_value = 2 THEN 0
WHEN previous_value = 2 THEN 1
END as out
FROM my_table;
In your query, each case expression generates one column in the resulset. You want only one, with two branches (denoted by when ... then ...)
You are getting null output, so you need to add else on this.
select name,
case
when new_value = 2 then 0
when previous_value = 2 then 1
else 0 end as out
from my_table;

Using Nested Case Expression

I am stuck in properly using Nested Case Expression.
The requirement is to create the calculated column that will be either 0 or 1.
If the name is 'abc' and ind = 1, then the value of calc column will be 1 . But if the name is 'abc' and salary is > 2000 . then the value will be 1 again irrespective the value of ind.
I have written the below query using OR condition in CASE Expression but want to use the Nested CASE.
select t.*, case when (name = 'abc' and ind = 1 ) or (name = 'abc' and sal > 2000)
then 1
else 0
end
from test t
Below is the result:
You don't need nested CASE:
-- multiple conditions
select t.*, case when name = 'abc' and ind = 1 then 1
when name = 'abc' and sal > 2000 then 1
else 0 end
from test t
I don't know why you need nested case expression, i would do instead of nested case expression that would be more efficient :
select t.*, (case when name = 'abc' and (ind = 1 or sal > 2000)
then 1
else 0
end)
from test t;
If you HAVE to have nested case expression then this is how you would write it:
SELECT t.*,
CASE
WHEN name = 'abc' THEN
CASE
WHEN ind = 1 THEN 1
WHEN sal > 2000 THEN 1
ELSE 0
END
ELSE 0
END
FROM test t;
If you are on SQL Server 2012 and above, you can also use an IIF statement like below:
select iif(name = 'abc' AND (salary > 2000 OR ind = 1), 1,0)

SQL Server - Get column who have specific value

I have a SQL query which returns :
id | value
1 a
1 a
1 b
2 a
2 a
I want to get only id who have only the value a. So the id 2
How to do this ?
You can use aggregation and having clause to check if all the rows have value 'a' for a given id:
Using Count:
select id
from t
group by id
having count(*) = count(case when value = 'a' then 1 end);
Or using Sum
select id
from t
group by id
having SUM(case when value = 'a' then 0 else 1 end) = 0;
Use the next code:-
Select id
from #test
group by id
having sum (case when value = 'a' then 0 else 1 end) = 0
The clue is passing 0 for 'a' and pass 1 for other, then having sum equals 0
This is slightly slower than #Gurwinder Singh's answer but can be more readable if performance is not your top priority.
CREATE TABLE tmp (id int, [value] char(1))
INSERT INTO tmp values (1,'a'),(1,'a'),(1,'b'),(2,'a'),(2,'a')
SELECT DISTINCT id
FROM tmp a
WHERE [value] = 'a'
AND id NOT IN (
SELECT id FROM tmp
WHERE [value] <> 'a')

Get the distinct count of values from a table with multiple where clauses

My table structure is this
id last_mod_dt nr is_u is_rog is_ror is_unv
1 x uuid1 1 1 1 0
2 y uuid1 1 0 1 1
3 z uuid2 1 1 1 1
I want the count of rows with:
is_ror=1 or is_rog =1
is_u=1
is_unv=1
All in a single query. Is it possible?
The problem I am facing is that there can be same values for nr as is the case in the table above.
Case statments provide mondo flexibility...
SELECT
sum(case
when is_ror = 1 or is_rog = 1 then 1
else 0
end) FirstCount
,sum(case
when is_u = 1 then 1
else 0
end) SecondCount
,sum(case
when is_unv = 1 then 1
else 0
end) ThirdCount
from MyTable
you can use union to get multiple results e.g.
select count(*) from table with is_ror=1 or is_rog =1
union
select count(*) from table with is_u=1
union
select count(*) from table with is_unv=1
Then the result set will contain three rows each with one of the counts.
Sounds pretty simple if "all in a single query" does not disqualify subselects;
SELECT
(SELECT COUNT(DISTINCT nr) FROM table1 WHERE is_ror=1 OR is_rog=1) cnt_ror_reg,
(SELECT COUNT(DISTINCT nr) FROM table1 WHERE is_u=1) cnt_u,
(SELECT COUNT(DISTINCT nr) FROM table1 WHERE is_unv=1) cnt_unv;
how about something like
SELECT
SUM(IF(is_u > 0 AND is_rog > 0, 1, 0)) AS count_something,
...
from table
group by nr
I think it will do the trick
I am of course not sure what you want exactly, but I believe you can use the logic to produce your desired result.

having issues sorting alpha numeric chars

I am trying to sort one column which have alphanumeric letters
see my query below
SELECT d.number
FROM table name d, table_name 2 a WHERE d.case_id ='11-41'
AND d.ExhibitTypeId = TypeId AND d.ComplianceNo = '0' and
active = 1 and number is not null order by case
when ISNUMERIC(d.number) = 1 then right('0000000000'+d.number+'0',10)
else right('0000000000'+d.number,10)
end
This is the output
1
2
3
11
12
2A1
I want this output instead
1
2
2A1
3
11
12
Any help regarding this is greatly appreciated.
If(ISNUMERIC(LEFT(case,2)
BEGIN
order by case
END
else
BEGIN
order by LEFT(case,1), LEFT(case,2)
END
Assuming SQL Server this may work with some tweaks
SELECT
d.number
FROM
table name d,
table_name 2 a
WHERE
d.case_id ='11-41'
AND
d.ExhibitTypeId = TypeId
AND
d.ComplianceNo = '0'
and
active = 1
and number is not null
order by
Convert(int, LEFT(number, Case
When PATINDEX('%[^0-9]%', number) > 0 Then PATINDEX('%[^0-9]%', number) - 1
Else LEN(number)
End)
),
LEN(Number)