SQL - Command to get Id's with unique flags from multiple flags - sql

ID Risk_1 Risk_2 Risk_3 Risk_4
XYZ Yes Yes Yes
ABC Yes
PQR Yes Yes
As you can see from the above table There are IDs that have multiple risks associated with them. I want to get an output of IDs where only one risk is associated with them.
In the case above I want all IDs which have only risk_1 so the result should be ABC.
How can I get this done using SQL?

Assuming that Risks are nullable.
SELECT ID
FROM tableName
WHERE CASE WHEN Risk_1 IS NOT NULL THEN 1 ELSE 0 END +
CASE WHEN Risk_2 IS NOT NULL THEN 1 ELSE 0 END +
CASE WHEN Risk_3 IS NOT NULL THEN 1 ELSE 0 END +
CASE WHEN Risk_4 IS NOT NULL THEN 1 ELSE 0 END = 1
SQLFiddle Demo
if however they are empty string,
SELECT ID
FROM tableName
WHERE CASE WHEN Risk_1 <> '' THEN 1 ELSE 0 END +
CASE WHEN Risk_2 <> '' THEN 1 ELSE 0 END +
CASE WHEN Risk_3 <> '' THEN 1 ELSE 0 END +
CASE WHEN Risk_4 <> '' THEN 1 ELSE 0 END = 1
SQLFiddle Demo

That will work with NULLs, empty strings, or values other than Yes (e.g. No)
SELECT ID
FROM Table1
WHERE COALESCE(Risk_1, 'No') = 'Yes' AND
COALESCE(Risk_2, 'No') <> 'Yes' AND
COALESCE(Risk_3, 'No') <> 'Yes' AND
COALESCE(Risk_4, 'No') <> 'Yes'
sqlfiddle

If the value of risks are only 'Yes' then below query will work
select ID from table where risk1||risk2||risk3||risk4='Yes'
If the value of risks are only 'Yes' and the empty risks have spaces instead of nulls then below query will work
select ID from table where replace(risk1||risk2||risk3||risk4,' ','')='Yes'

If you altered your schema so your table had columns ID and risk_type with a row for each risk type, you could have a query like this:
SELECT ID
FROM Table1
GROUP BY ID
HAVING COUNT(*) = 1
SQLFiddle
re-edit: modified to no longer specify which one risk type they have

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;

Create Multiple Count Columns using Case When

I have to Case statements that count the same column just with a differnt criteria. The problem i am having is that the first case creates a null entry in the Test2 column but then counts it on my second case statement and leaves the Test 1 column null. I would like to have both counts side by side instead of created a duplicate row.
select m.no,
Case when itemtype = 'S' THEN count(ITEMKEY) end as Test1,
case when ItemType='C' THEN count(ITEMKEY) END as Test2
from test m
I'm pretty sure you want conditional aggregation. The case expression is an argument to the aggregation function:
select m.no,
sum(case when itemtype = 'S' then 1 else 0 end) as test1,
sum(case when itemtype = 'C' then 1 else 0 end) as test2
from test m
group by m.no;
This assumes that itemKey is never null, so the count() is just counting rows.
Following query can display records which having Itemtype 'S' or 'C' with count of itemkey. if itemkey is null it will display 0 else count of item key
select m.no,
Case when isnull( m.itemtype = 'S',0) THEN (select count(a.ITEMKEY) from test a where a.itemtype = 'S' ) else 0 end as Test1,
case when isnull( m.itemtype='C',0) THEN (select count(b.ITEMKEY) from test b where b.itemtype = 'C') else 0 END as Test2
from test m

How to check if all rows validate a predicate

I've a table in my database for which I need to check if all rows have one field not null.
If there are no row or if there is at least 1 row with the field null => true
If there are rows and they are all with the field not null => False
Is there a way to do this in on simple query? Or I need to check if my table is empty first then if it's not check if I've a row with the field value empty ?
This will count how many NULL values you have in a field;
SELECT
SUM(CASE WHEN FieldName IS NULL THEN 1 ELSE 0 END) NullValues
FROM TableName
Will return 0 if there are no NULL values, and will return the number of NULLS if there are any present.
If you actually want to return a value as 'True' or 'False' then do this;
SELECT CASE
WHEN a.NullValues > 0
THEN 'True'
ELSE 'False'
END CheckField
FROM (
SELECT
SUM(CASE WHEN FieldName IS NULL
THEN 1
ELSE 0
END) NullValues
FROM TableName
) a
Use count(*) and count(field) and compare the two:
select
case when count(*) > 0 and count(*) = count(field) then 1 -- not empty and no nulls
else 0 end as isgood
from mytable;
Oracle SQL has no boolean data type , so I use 1 for true and 0 for false. You can replace this with whatever you like (e.g. 'true' instead of 1 and 'false' instead of 0).
As to turning this into a predicate (correlated to a main query), you'd use something along the lines of:
select ...
from main
where exists
(
select 1
from mytable
where mytable.colx = main.coly
having count(*) > 0 and count(*) = count(field)
);
You can do this with aggregation. However, it is difficult to understand what you are asking for. If you want to check that a field has no NULL values, you can do:
select (case when count(*) > 0 then 1 else 0 end) as HasNullValues
from t
where field is null;
Alternate way I found using max with putting null first:
select case when
max(field) keep (dense_rank first order by datfin desc nulls first) is null then 1
else 0 end as flag
from MYTABLE;

SQL check if numrical value is 0 for a field where id is=333

inside an SQL SELECT CASE STATEMENT how would i check if the a value for some coulmn in sometable is equal to 0 where the id is equal to something?
select
case
when (select column_name from sometable where id = #id) = 0 then
else
end
select
case when( select column from table where id=333)=0 then "your condition"
else
end
My interpretation of your question:
SQL check if numerical value is 0 for a field (Field1) [only] when id is=333
[If id is not 333, don't need to check]
SELECT CASE WHEN isnull(id,0) <> 333 OR Field1=0 THEN 1 ELSE 0 END
,other1, other2
FROM tbl

query sql returning custom value

I have a table with 3 columns (containing Integer values that assume only values from 0 to 10). I want extract, with a single query, a table with 1 column. This column must assume a value based on the following logic:
If one of these three columns has value 0 ----> the value of column of table generated by query must be 0 too.
If none of the last three columns has value 0 ----> the value of column must assume the value 1.
You are looking for CASE construct or IF function:
SELECT CASE WHEN (t.field1 = 0 OR t.field2 = 0 OR t.field3 = 0) THEN 0
ELSE 1 END AS value
FROM t;
In this specific case you might also use the fact that any member being zero will zero the product:
SELECT CASE WHEN (t.field1*t.field2*t.field3 = 0) THEN 0 ELSE 1 END AS value
FROM t;
Or
SELECT IF((t.field1*t.field2*t.field3)=0, 0, 1) AS value FROM t;
This is a simple case statement. Assuming there are no NULL values, try this:
select (case when col1 = 0 or col2 = 0 or col3 = 0 then 0 else 1 end)
Try this
SELECT
CASE
WHEN column1 = 0 THEN 0
WHEN column2 = 0 THEN 0
WHEN column3 = 0 THEN 0
ELSE 1
END
FROM urtable