AND statement inside CASE postgreSQL referring to similar condiitons - sql

I am trying to use an AND statement inside a CASE statement, referring to the same thing twice:
CASE
WHEN col1 like A then col_A-(col_B/col_C)
WHEN col1 like A AND sum(col2)=0 then col_D
end as casecolumn
However I am finding that only the first WHEN statement is affecting the column, i.e only calculation1 is implemented, how can I make sure both calculations are applied to casecolumn?

Switch the order of the conditions:
CASE WHEN col1 like A AND SUM(col2) = 0 THEN calcuation2
WHEN coc1 like A THEN calculation1
END AS casecolumn
If the LIKE and SUM criteria be met first, that condition would fire. Otherwise, the CASE expression will flow down to the more lenient criteria for another chance to match.
If you want the "calculation" of both conditions to be applied should both the LIKE and SUM conditions be true, then just use it when determining the value for that condition.

Related

Adding a condition to the showcase

The task is as follows. I have a code that has a huge number of attributes. And to one of the attributes, let's say this is the card type card_type='universal', you need to add the following condition:
case when card>='129897' and card<='293965'then 'unnamed' and card>='093750' and card <='903750' then 'personal' end as parameter
The attribute itself is as follows :select case when card_sybtype in ('VISA','MS') then 'universal'
At the same time, I do not need to output this to the final script, but I need this feature to be present in the script. That is, I need it to be linked only to the card type.
I think you can use a nested case when.
And if you dont want to select parameter column, you can use a subquery to hide the columns. So, in case you want it, just add it to the outside select list.
SELECT col1, col2
FROM
(SELECT
col1, col2,
case when card_sybtype in ('VISA','MS') THEN -- universal case
case when card>='129897' and card <='293965' then 'unnamed'
when card>='093750' and card <='903750' then 'personal' end
else null
end as param_adhoc
FROM tab
) rs

Return Condition/Expression from a Case Statement

I have searched all around and cannot seem to find a solution to this problem I'm having. I have a fairly large case statement (over 100 lines) that works and returns the result I am looking for. An example of the line is below:
case
When (Description like '%job%'
or description like '%job%fail%') then 'Job'
Else 'Not Classified'
End as ATC
I have a case statement that returns the result 'Job' as expected. I would also like to create a separate case statement that returns the criteria that returns the condition that the record met, allowing me to evaluate which criteria are returning the match ( a 'job' vs. 'job failed' comparison). I'm aware that I can duplicate my case statement to output the criteria met, but I would like to repeat this analysis and am looking for a more easily replicable solution (something along the lines of reading the conditions from the above case statement). Any thoughts?
If you're just trying to avoid repeating the logic you can wrap it up in a table expression.
with matches as (
select *,
case when Description like '%job%fail%' then 1 -- most specific first
when Description like '%job%' then 2 -- least specific last
else 0
end as MatchCode
from ...
)
select *,
case when MatchCode > 0 then 'Job' Else 'Not Classified' End as ATC
from matches

SQL: Is there a way for me to call another table.field when my initial table.field = NULL?

Morning All! Essentially, I found out that a potential table.field I'd like to use may not have the information I want, so, I might have to look into a separate field. My below thought process is:
If table.field1 = NULL then pull the value from table.field2. If table.field2 = NULL then state "No Phone"
My current SQL statement in the select, cause it's in a certain format, is:
substring(view_episode_summary_current.patient_home_phone, 1, 3) || ' ' || substring(view_episode_summary_current.patient_home_phone, 5, 8)
Above is let's say table.field1. I'm assuming I'll need to create a CASE statement right? I just didn't know how long it could be?
(case when view_episode_summary_current.patient_home_phone = NULL then table.field2)
But I don't know how to get it to evaluate if table.field2 = null and display the value.
In many databases, you can use coalesce() to implement that kind of logic:
coalesce(t1.field1, t2.field2) as myfield
coalesce() returns the first non-null value across its arguments. So it gives you t1.field1 if it is not null, else it falls back on t2.field2. If both are null, you get a null value as a result.
You could also use a case expression - there is no benefit as compared to colaesce(), but this can handle more complex cases than just nullity checks
case
when t1.field1 is not null then t1.field1
when t2.field2 is not null then t2.field2
end as myfield
case stops on the first branch where the condition is fulfilled. If no branch matches, it returns null - you can put an else branch at the end of your code to return something else.
Note that both techniques requires both fields to have similar data types.
You do not show your actual query so I cannot show how to use that in your code.

Oracle CASE missing right parenthesis for a "in" limit

I have a QRY im developing in Oracle for spotfire. In the where statement, I have a decision case statement and if its True, im trying to pass a list of items to match a column, below is what I have, but its throwing a missing right parenthesis error and I cannot determine why. In short, when a variable is determined True (in this case 9>8 for the example, I need it to result those items, else, result the entire column with no limits.
Note: This works fine when its only 1 item being passed, i.e. 'BOB' but as soon as its multiple, this error occurs.
and Column1 = (CASE When 9>8 Then ('BOB','TOM') Else Column1 END)
Case expressions are best avoided in the where clause. Instead, write the logic with AND and OR:
And (
(9>8 AND Column1 IN ('BOB','TOM'))
OR 9<=8 -- You say you check a variable here, don't forget to check for NULL
)
Oracle does not have a boolean type for use in SQL queries.
Instead, just use basic logic:
and ( (9 > 8 and Column1 in ('BOB','TOM')) or
9 <= 8
)

Which one of these two SELECT statment are correct?

I am a little bit confusing and have no idea which one of these two SELECT statments are correct
SELECT Value FROM visibility WHERE site_info LIKE '%site_is_down%';
OR
SELECT Value FROM visibility WHERE site_info = 'site_is_down';
SInce I run both of these I get same result, but I am interesting which one is correct since Value column is VARCHAR data type OR both of these SELECT are incorect ?
Result set running first SELECT
Value
1. 0
Result set running second SELECT
Value
1. 0
The two statements do not do the same thing.
The first statement filters on rows whose site_infos contain string 'site_is_down'. The surrounding '%' are wildcards. So it would match on something like 'It looks like site_is_down right now'.
The second query, with the equality condition, filters on site_info whose content is exactly 'site_is_dow'.
Everything that the second query is also returned by the first query - but the opposite is not true.
Which statement is "correct" depends on your actual requirement.
If both queries are useful for you, I'd use the second query, as it is the simplest, and runs faster.