Can multiple 'OR' statements be combined if they have the same condition? - sql

I have the below OR statement with each column = 1 as the condition. Is there a shorter way of doing this? I have tried HAVING and IN but neither worked.
(count(distinct case when (fiscal_mth_idnt_june21 = 1 or fiscal_mth_idnt_july21 = 1 or fiscal_mth_idnt_august21 = 1) then contact_key end)) / sum(fiscal_mth_idnt_may21)*100 as within_three_months
is the main code I am interested in.

The boolean expression:
fiscal_mth_idnt_june21 = 1 or fiscal_mth_idnt_july21 = 1 or fiscal_mth_idnt_august21 = 1
is equivalent to:
1 IN (fiscal_mth_idnt_june21, fiscal_mth_idnt_july21, fiscal_mth_idnt_august21)

Related

SQL How to use IF query to create a new column from others

I have tables where I have a result of 0-1 0 is gone and 1 is. I need to write a query, and I totally don't know how to. If New_Def = 0 a Default = 1 then 'NEW' If New_Def = 1 a Default = 1 then = OLD.
I think you are looking for case :
select * , case when New_Def = 0 and Default = 1 then 'NEW'
when New_Def = 1 and Default = 1 then 'OLD'
else 'Unknown'
end as Newcolumn
from table

Simple way to check % of total if I add one criteria

I have the following script:
select count (*)
from somewhere
where 1=1
and criteria 1 = 'xxx'
and criteria 2 = 'xx'
and criteria 3 = 'x'
Is there a nice way to calculate the % of the count with the first 2 criterias vs the count with all 3 criterias ?
So far I'm using something that is not very elegant:
Select x.nb / y.nb
from
(select count (*) as nb
from somewhere
where 1=1
and criteria 1 = 'xxx'
and criteria 2 = 'xx'
and criteria 3 = 'x' ) x
JOIN
(select count (*) as nb
from somewhere
where 1=1
and criteria 1 = 'xxx'
and criteria 2 = 'xx' ) y on 1 = 1
Thanks!
You can use avg():
select avg(case when criteria3 = 'x' then 1.0 else 0 end) as ratio
from somewhere
where criteria 1 = 'xxx' and criteria 2 = 'xx' ;
Or more concisely as:
select avg( (criteria3 = 'x')::int ) as ratio
from somewhere
where criteria 1 = 'xxx' and criteria 2 = 'xx' ;

where clause work only when case is satisfied

I have made a view that has a where clause :
WHERE M.Deleted = 0 AND I.Deleted = 0 AND L.Deleted = 0
is there a possible way to make L.Deleted = 0 only work on a specific case? My plan is when the M.TYPE_ID != 4 then add this L.Delete = 0 to where clause, and without it otherwise
I tried to make something like:
AND CASE WHEN M.TYPE_ID != 4 THEN L.DELETED = 0 ELSE --(i want here to make this entire part of where clause to be invalid if the case is not valid)
or there are other ways to do that like intersect the same select above but without that condition ?
any help would be appreciated, thanks.
Yes, you can do:
WHERE M.Deleted = 0 AND I.Deleted = 0 AND
(M.TYPE_ID = 4 OR L.Deleted = 0)
You don't need a case expression.
You might find the logic simpler to follow as:
WHERE M.Deleted = 0 AND I.Deleted = 0 AND
( NOT (M.TYPE_ID <> 4 AND L.Deleted <> 0) )
The simplest way would be to include the valid condition in two checks and or them together.
e.g.
Select * from x where (x.isvalid = 1 and x.deleted = 0) or (x.isvalid <> 1 and x.othercheck = 1)

Changing when to if statement in SQL server

How do I change the following code to an if statement that returns a boolean 0 or 1 value? My end results I would like to have, is one column listing the interest rate of 2, and my results column with a 0 or 1 if the condition is true.
(Case when new_interestratevariability = 2
and (new_interestrateindex = 1 or new_interestrateindex = 2 or new_interestrateindex = 3 or new_interestrateindex = 4 or new_interestrateindex = 6)
and new_crms_dt = #Curr_Date
then 0 else 1 end) as CIEDIT_VAL_96,
Currently, I am getting something like below:
Results Table
To filter rows, use a Where clause. The Case statement in the Select clause will modify the value shown on the row.
Select *
from table
Where new_interestratevariability = 2
and new_interestrateindex IN (1,2,3,4,6)
and new_crms_dt = #Curr_Date
Found my answer, it was as simple as adding "not in" instead of just "in". Thanks everyone
(Case when new_interestratevariability = 2
and (new_interestrateindex not in(1,2,3,4,6))
and new_crms_dt = #Curr_Date
then 1 else 0 end) as CIEDIT_VAL_96,

SQL - "IF" in Where Clause

I'm trying to write a stored procedure that will have 6 bit value flags as parameters and a couple of other values.
The pseudo sql I want to write is something like:
SELECT *
FROM theTable
WHERE
IF #flagA = 1 THEN theTable.A = 1
IF #flagB = 1 THEN theTable.B = 1
IF #flagC = 1 THEN theTable.CValue = #cValue
etc
Any ideas how I can do this in SQL or am I best reverting to building the SQL in C# (where this SP will be called from)?
SELECT *
FROM theTable
WHERE
(#flagA = 0 or (#flagA = 1 AND theTable.A = 1 ))
and (#flagB = 0 or (#flagB = 1 AND theTable.B = 1 ))
and (#flagC = 0 or (#flagC = 1 AND theTable.CValue = #cValue ))
Note: I am assuming your bit flags are non-nullable. If that is not the case, you will need to use ISNULL.