How to add multiple case in sql - sql

I want to add multiple cases in single line. In other words in below example if first item is NOT NULL and second item is equal to 1 then output should be 1 else 0. How can I do it?
MatchbyCatalog= (case when ISNULL(pc.ProductID,0) and tc.RawMatch=1) then 1 else 0 end)

Is this what you want?
CASE WHEN pc.ProductID IS NULL and tc.RawMatch=1 THEN 1 ELSE 0 END
You misunderstood ISNULL() , it's a function that replaces the value if it's NULL, it's not a condition . You were looking for IS NULL

That's all you need.
MatchbyCatalog= (case when ISNULL(pc.ProductID,0)=1 and tc.RawMatch=1 then 1 else 0 end)

when you say both I think you mean pc.ProductID and tc.RawMatch...
I have not you're entire sql query so I have to guess.
You have multiple solutions :
Nearly ss you have wrote when pc.ProductId = 1 and tc.RawMatch=1 then 1 else 0 :
MatchbyCatalog= (case when ISNULL(pc.ProductID,0)=1 and tc.RawMatch=1) then 1 else 0 end)
You could also use a trick : 1 * 1 should be 1 and 1 * 0 =0 so you could write (if tc.Rawmatch is a boolean sort of with values 0 or 1) :
MatchbyCatalog= ISNULL(pc.ProductID,0) * tc.RawMatch

case when {Condition} then {result}
When {Condition} then {result}
Else {result}

Related

SQL : Count with "==" to check in SQL Server [duplicate]

This question already has answers here:
Conditional Count on a field
(8 answers)
Closed last year.
I'm working with SQL Server and I want to create a view (my_View) which has these columns :
[element_1]
[element_2]
[element_3]
[element_4]
All of them are referring to the same column, named [parent_1], in another table (AT)
[parent_1] can have 4 possible values [value_1], [value_2], [value_3] and [value_4].
What I want is that, using COUNT,
[element_1] is equal to the number of times that [parent_1] is equal to [value_1]
same for [element_2], [element_3] and [element_4] equals to [value_2], [value_3] and [value_4].
Is it possible to use "==" inside the COUNT to see if it checks the criteria?
Something like this:
COUNT (AT.parent_1 == "value_1") AS element_1
COUNT (AT.parent_1 == "value_2") AS element_2
COUNT (AT.parent_1 == "value_3") AS element_3
COUNT (AT.parent_1 == "value_4") AS element_4
Thanks guys
You can use the CASE instruction for that
https://learn.microsoft.com/fr-fr/sql/t-sql/language-elements/case-transact-sql?view=sql-server-ver15
SUM (CASE WHEN AT.parent_1 = 'value_4' THEN 1 ELSE 0 END) as element_4
You can do something like:
SELECT
sum(case when parent_1 = "value_1" then 1 else 0 end) as element_1,
sum(case when parent_1 = "value_2" then 1 else 0 end) as element_2,
sum(case when parent_1 = "value_3" then 1 else 0 end) as element_3,
sum(case when parent_1 = "value_4" then 1 else 0 end) as element_4
FROM table;
create my_view as
select
-- other list of columns,
SUM (CASE WHEN AT.parent_1 = 'value_1' THEN 1 ELSE 0 END) as element_1,
SUM (CASE WHEN AT.parent_1 = 'value_2' THEN 1 ELSE 0 END) as element_2,
SUM (CASE WHEN AT.parent_1 = 'value_3' THEN 1 ELSE 0 END) as element_3,
SUM (CASE WHEN AT.parent_1 = 'value_4' THEN 1 ELSE 0 END) as element_4
from tableName AT
There is no need to use == like that of a programming language, in SQL comparison operator is =

How to use case function in SQL where clause?

I am converting a Crystal Report to SSRS. In the CR Select Records section there are several filters including an IF statement like this:
(if BeginCompany <> 0 and EndCompany <> 999 then
Company between BeginCompany and EndCompany else 1 = 1)
How can I make an equivalent CASE statement in an SQL WHERE clause in my dataset? Thanks.
Are BeginCompany and EndCompany actually variables? What are they when you don't want to use them as a filter? NULL? Something else?
Maybe you're looking for this (assuming companies are represented by positive integers):
WHERE Company >= COALESCE(#BeginCompany, 0)
AND Company <= COALESCE(#EndCompany, 2147483647)
Which is just another way to express:
WHERE Company >= CASE
WHEN #BeginCompany IS NULL THEN 0 ELSE #BeginCompany END
AND Company <= CASE
WHEN #EndCompany IS NULL THEN 2147483647 ELSE #EndCompany END
You can't use a CASE expression for control-of-flow, e.g.
CASE ... something ... THEN Column BETWEEN x AND y
CASE is an expression that returns a single value. Much more background here:
Dirty Secrets of the CASE Expression
It sounds like the logic wants the Company field to be between the BeginCompany and EndCompany parameters if BeginCompany <> 0 and EndCompany <> 999 to show a record but if they do equal 0 or 999, then show all records. If a users enters 0 for the Begin parameter or 999 for the end, then the query would show all records.
I think in SQL it would be
WHERE (
#BeginCompany <> 0
and #EndCompany <> 999
AND Company between #BeginCompany and #EndCompany
)
OR (
#BeginCompany = 0
OR
#EndCompany = 999
)
If you really wanted to squeeze a CASE statement if there then
WHERE CASE WHEN
#BeginCompany <> 0
and #EndCompany <> 999
AND Company between #BeginCompany and #EndCompany
THEN 1
WHEN
#BeginCompany = 0
OR
#EndCompany = 999
THEN 1
ELSE 0 END = 1

Count(*) return 1 or zero

In one of the usecase I need a query which should return 1 based on condition also if not match it should return 0
In Descpriont column if the 'SAP' count is exactly 1 then the query should return 1 else it should return 0
Note : There might be a chance that SAP could be present any number of times in Description column.
Could someone help me out here !!
Thanks.
I tried below query :
SELECT 1 from TableName where Description ='SAP' having count(*)>1
It is returning 1 but not return 0 if the count is more than 1 or no match found.
Use CASE WHEN to decide whether to show 0 or 1.
select case when count(*) = 1 then 1 else 0 end as sap_count_is_1
from mytable
where description = 'SAP';
use case when
select
case when sum(case when description='SAP' then 1 else 0 end)=1 then 1 else 0 end from table_name

Division in Sql returning 0 or NULL

New to SQL and trying to figure this one out. I’m expecting a ratio of win % but am getting a 0 or null column
With previous as (select Venue, Row,
Count(case when place = 1 then 1 else null end) as firstx
Count(case when place = 2 then 1 else null end) as secondx
Count(case when place = 3 then 1 else null end) as thirdx
Count(case when place Between 1 and 15 then 1 else null end) as allplaces
From horses
Where racetitle not like ‘%stand%’
Group by venue, row)
Select case
When firstx = 0
Then Null
Else round(firstx/allplaces, 2)
End as winratio
From previous
Where allplaces > 10;
I want to do this with secondx and thirdx but am not sure why it is returning a column as 0 or Null???
You are getting NULL because firstx = 0 and 0 because the rdbms that you use does integer division when dividing firstx/allplaces, so multiply by 1.0 before you divide:
With previous as (
select Venue, Row,
Count(case when place = 1 then 1 end) as firstx
Count(case when place = 2 then 1 end) as secondx
Count(case when place = 3 then 1 end) as thirdx
Count(case when place Between 1 and 15 then 1 end) as allplaces
From horses
Where racetitle not like ‘%stand%’
Group by venue, row
)
Select case
When firstx = 0 Then Null
Else round(1.0 * firstx/allplaces, 2)
End as winratio
From previous
Where allplaces > 10;
I also simplified the CASE expressions inside COUNT() because there is no need for else NULL since this is the default behavior.
I assume that your DBMS makes integer division when / operates on integers. And since firstx is in general less than allplaces, then integer division of them is 0
You need to cast of of the values to a decimal number. The syntax depends on specific DBMS, usually it's CAST or CONVERT function
Some databases do integer division, which is probably the issue you are facing. That said, the more canonical way to prevent a divide-by-zero is to use nullif():
select round(firstx * 1.0/ nullif(allplaces, 0), 2) as winratio
nullif() is a standard SQL function and available in almost all databases.

SQL ... changing a having with a case into a where with a case

This is my code.
The problem I have is related with the having.
I had to make some changes to handle a sas code in teradata, and, therefore, I needed to eliminate the GROUP BY and I have to use a WHERE clause rather than a HAVING, keeping the same conditions of it. Unfortunately, I am stucked and that's why I am asking for your suggestions.
SELECT t1.COD_PRODT,
t1.COD_RESID_,
t1.COD_DIVISA,
t1.COD_ABI,
t1.COD_NDG,
t1.COD_KTO,
t1.COD_PAESE,
t1.DAT_SCA,
t1.DAT_ACC,
t1.DAT_EST,
(COUNT(t1.COD_ABI)) AS COUNT_of_COD_ABI
FROM WORK.A_VE_ES_DB_ANAGR_CONTO_CT_TT t1
GROUP BY t1.COD_ABI,
t1.COD_KTO
HAVING (
CASE WHEN COUNT_of_COD_ABI > 1 AND t1.DAT_EST IS NOT NULL THEN 1
WHEN COUNT_of_COD_ABI > 1 AND t1.DAT_EST IS NULL THEN 0
WHEN COUNT_of_COD_ABI = 1 THEN 1
ELSE 0 END
) = 1
ORDER BY COUNT_of_COD_ABI DESC,
t1.COD_ABI,
t1.COD_KTO,
t1.COD_NDG
I did something like that, but it turns out that it eliminates duplicates, while I need to bring them into analysis as well...
where (COUNT_of_COD_ABI =
CASE
WHEN COUNT_of_COD_ABI> 1 AND P2.DAT_EST IS NOT NULL THEN 1
WHEN COUNT_of_COD_ABI> 1 AND P2.DAT_EST IS NULL THEN 0
WHEN COUNT_of_COD_ABI = 1 THEN 1
ELSE 0
end )
Can you help me out with this?