Conditional select in sql query - sql

I am trying to access a field from a table and give different output based on the field content.
If the field has 0 I want to fetch No else if 1 or NULL I want to fetch yes.
It is working well for 0 or 1 but not for NULL.
Select distinct(convert(varchar(255),( CASE Field1
WHEN 0
THEN 'No'
WHEN 1
THEN 'Yes'
WHEN NULL
THEN 'Yes'
END ))) AS Field1 FROM Table1

CASE Field1
WHEN 0 THEN 'No'
ELSE 'Yes'
END
or this:
CASE ISNULL(Field1, 1)
WHEN 0 THEN 'No'
WHEN 1 THEN 'Yes'
END

Select distinct(convert(varchar(255),
(CASE Field1 WHEN 0 THEN 'No'
WHEN 1 THEN 'Yes'
WHEN IS NULL THEN 'Yes'
END))) as Field1 from Table1

SELECT ISNULL(CASE Field1 WHEN 0 THEN 'No' WHEN 1 THEN 'Yes','Yes') AS ..

Select distinct(convert(varchar(255),( CASE Field1
WHEN 0 THEN
'No'
else 'Yes' END )))as Field1 from Table1

Select distinct(convert(varchar(255),
CASE ISNULL(FIELD1, 1)
WHEN 0 THEN 'No'
WHEN 1 THEN 'Yes'
END
))as FIELD1
from TABLE1

May be you want this:
Select distinct(convert(varchar(255),( CASE
WHEN Field1 =0 THEN
'No'
WHEN Field1=1 THEN
'Yes'
WHEN Field1 is null THEN 'Yes'
END )))as Field1 from Table1

Related

SQL check link between two records (CASE, WHEN, THEN)

I Have in table some records:
ID Services
2 A
2 C
2 C1
2 D2
I`m trying make query that will be select a link between services.
For example: If for ID 2 exists Services C then check if exist Service C1, result Yes or No.
SELECT a. ID, a.service,
CASE
WHEN (a.service ='C') = (a.service = 'C1') THEN 'Yes'
ELSE 'No'
END
FROM t1 a
Try this query:
SELECT *
FROM yourTable t1
WHERE NOT EXISTS (SELECT 1 FROM yourTable t2
WHERE (t2.Services LIKE t1.Services + '%' OR
t1.Services LIKE t2.Services + '%') AND
t1.ID = t2.ID AND t1.Services <> t2.Services);
This returns A and D2 only.
Demo
Hmm... what about this? But I now have problem with checking relationship for each ID independently...
SELECT a. ID, a.service,
CASE
WHEN a.service IN ('C','C1') THEN 'Yes'
ELSE 'No'
END
FROM t1 a
If I understand correctly, you can use aggregation:
SELECT ID,
(CASE WHEN SUM(CASE WHEN service = 'C' THEN 1 ELSE 0 END) > 0 AND
SUM(CASE WHEN service = 'C1' THEN 1 ELSE 0 END) > 0
THEN 'Yes' ELSE 'No'
END) as c_c1_flag
FROM t1
GROUP BY ID;
The SUM(CASE . . . ) counts the number of rows that match the conditions. The > 0 simply says that at least one row exists.

Case SQL If there is no record between dates

I'm new to SQL and am struggling with a case.
I would like to make the case where if an account (account_ID) doesn't have a record (ON billing_id) between current_date-302 and current_date-62 THEN MARK WITH A "1"
Query below:
Thanks in advance
SELECT
billing_date_local_time
,account_id
,contract_owner_name
,date_first_feature_partner
,deal_starts_at
,contract_id
,new_partner_type
,sum(voucher_sold) AS Vouchers
,sum(gross_bookings_local) AS GB
,sum(gross_revenue_local) AS GR
,is_G2
,Case when billing_date_local_time between current_date-302 and current_date-62 = 0 THEN 'YES' ELSE 'NO' End
FROM EMEA_ANALYTICS.eu_deal_flat
WHERE
country_id = 206
and billing_date_local_time between current_date-400
and current_date-2
GROUP BY 1,2,3,4,5,6,10,11
You'll need to do a correlated subquery; something like this:
select
a.billing_date_local_time
,a.account_id
,...
, CASE WHEN EXISTS (SELECT * FROM EMEA_ANALYTICS.eu_deal_flat b WHERE a.account_id = b.account_id AND b.billing_date_local_time between current_date-302 and current_date-62 ) THEN 'YES' ELSE 'NO' END
from
FROM EMEA_ANALYTICS.eu_deal_flat a
WHERE ...
You need to apply an aggregate function like this:
min(case when billing_date_local_time
between current_date-302 and current_date-62
then 0
else 1
end)

SQL SELECT CASE throws an error with no parenthesis

SELECT CASE s.Country WHEN 1 THEN 'One' WHEN 2 THEN 'Two'
WHEN 3 THEN 'Three' ELSE 'Your message.' END
,(SELECT CASE DoYouWishToP When 0 Then 'Yes' When 1 Then 'No' END)
,(SELECT CASE Housingoptions When 'rb0' Then 'Lease' When 'rb1' then 'im lazy' when 'rb2' Then 'Rental' END)
from tblSurvey s
Above script does work. My question is why the 2nd and 3rd SELECT statments need to be inside the parenthesis. ( ). And do you see any issues in my script?
If I Use below I get Error:
SELECT CASE s.Country WHEN 1 THEN 'One' WHEN 2 THEN 'Two'
WHEN 3 THEN 'Three' ELSE 'Your message.' END
,SELECT CASE DoYouWishToP When 0 Then 'Yes' When 1 Then 'No' END
They don't. Write the query as:
SELECT (CASE s.Country
WHEN 1 THEN 'One'
WHEN 2 THEN 'Two'
WHEN 3 THEN 'Three'
ELSE 'Your message.'
END),
(CASE DoYouWishToP When 0 Then 'Yes' When 1 Then 'No' END),
(CASE Housingoptions
When 'rb0' Then 'Lease'
When 'rb1' then 'im lazy'
when 'rb2' Then 'Rental'
END)
from tblSurvey s;

return a column name depending upon another column field value in oracle

I have written a code like this:
SELECT
C.*,
CASE
WHEN C.COST_CENTRE_NAME IS NOT NULL THEN 'YES'
WHEN C.GL_CODE IS NOT NULL THEN 'YES'
WHEN C.MATERIAL_CODE IS NOT NULL THEN 'YES'
ELSE
'NO' END AS DEPENDS_OTHER
FROM
OYSTER3.CAT_RULE_MV C
I want an another column to be added with this named DEPENDS_ON where column value of this column will depend upon the 'yes' or 'no' value of DEPENDS_OTHER. If DEPEND_OTHER column value is YES then it should return the column name for which it comes YES under DEPENDS_OTHER column. I am giving an example below:
id COST_CENTRE_NAME GL_CODE MATERIAL_CODE depends_other depend_on
1 a yes COST_CENTRE_NAME
2 no
3 123 yes GL_CODE
4 a2 yes MATERIAL_CODE
5 no
Just repeat what you have started:
select c.*
,case
when c.cost_centre_name is not null then
'YES'
when c.gl_code is not null then
'YES'
when c.material_code is not null then
'YES'
else
'NO'
end as depends_other
,case
when c.cost_centre_name is not null then
'COST_CENTRE_NAME'
when c.gl_code is not null then
'GL_CODE'
when c.material_code is not null then
'MATERIAL_CODE'
else
null
end as depends_on
from oyster3.cat_rule_mv c
TRY THIS
SELECT
C.*,
CASE
WHEN C.COST_CENTRE_NAME IS NOT NULL THEN 'YES'
WHEN C.GL_CODE IS NOT NULL THEN 'YES'
WHEN C.MATERIAL_CODE IS NOT NULL THEN 'YES'
ELSE
'NO' END AS DEPENDS_OTHER,
CASE
WHEN C.COST_CENTRE_NAME IS NOT NULL THEN 'COST_CENTRE_NAME'
WHEN C.GL_CODE IS NOT NULL THEN 'GL_CODE'
WHEN C.MATERIAL_CODE IS NOT NULL THEN 'MATERIAL_CODE'
ELSE
' ' END AS DEPENDS_ON
FROM
OYSTER3.CAT_RULE_MV C

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

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