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

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?

Related

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

How to add multiple case in 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}

Even or odd in SQL

This is table structure
id
1
2
3
4
5
6
I need result like this
id even odd
1 0 1
2 1 0
3 0 1
4 1 0
5 0 1
6 1 0
I tried
select id %2=0 then 1 else 0 end or id%2 <>0 then 1 else 0 odd
from table
How about
select
id,
~id & 1,
id & 1
from t
Take a look at the CASE keyword. It works very similarly to what you're trying to do in your SELECT statement. In addition, if you want to select multiple columns, separate them with a comma. The OR keyword is used for combining logical conditions in your query, not for specifying multiple columns.
An example of how you could use CASE in your query would be as follows:
SELECT id,
CASE WHEN id %2=0 THEN 1 ELSE 0 END AS Even,
[column2]
FROM [TableName]
The table structure is just Id?
you could try this!
select *,
case when id %2=0 then 1 else 0 end as even,
case when id %2 <>0 then 1 else 0 end as odd
from table
You have the right idea, but your syntax is a bit off. I'd use a CASE statement to create the even column, and then a calculate odd accordingly:
SELECT id, even, ABS(even - 1) AS odd
FROM (SELECT id, CASE WHEN id % 2 = 0 THEN 1 ELSE 0 END AS even
FROM my_table)

best query for a complex situation

I have a table with following columns,(id,fkid, flag1,flag2,flag3,flag4). The possible value for each flag field is -1 to 3 and null is allowed. What I need is a query to check if count of any flag field with value 2 is greater than 3 for a given foreign key fkid. The way I am doing is write a query for each field. It works but very not smart to me. Anyone has better idea? Thanks.
You can do this with one query:
select fkid
from t
group by fkid
having sum(case when flag1 = 2 then 1 else 0 end) > 3 or
sum(case when flag2 = 2 then 1 else 0 end) > 3 or
sum(case when flag3 = 2 then 1 else 0 end) > 3 or
sum(case when flag4 = 2 then 1 else 0 end) > 3
I do agree strongly with the comments, though, that sample data, sample results, and a clear table structure would greatly improve the question.
Query below will also answer your question, sql fiddle example: http://sqlfiddle.com/#!3/adda9/2
SELECT
DISTINCT fkid
FROM
tblTest pvt
UNPIVOT
(
FlagValue FOR Flag IN
(flag1,flag2,flag3,flag4)
) as Unpvt
WHERE
FlagValue = 2
GROUP BY
FKID, FLAG
HAVING COUNT(*)>3

aggregations in case statement

I am trying to do aggregations in case statement. I found 2 ways to do it. Can anyone say what the difference between the 2 is?
(CASE WHEN Event = 5 THEN count(*) ELSE 0 END ) Follow_Count
GROUP BY Event;
SUM(CASE Event WHEN 5 THEN 1 ELSE 0 END) AS Follow_Count
Your case 1 will produce a row for each event in the table (from your group by). Your case 2 will just return 1 row.
Is there a reason that you wouldn't just write:
select count(*)
from my_table
where event = 5;
Better would be:
count(CASE Event WHEN 5 THEN 1 END) AS Follow_Count
Because
1) for count used own standart counter,
2) "else" not need (count don't count nulls)
Regards,
Sayan M.
There is no significant difference. You can decide for you which is better by comparing their execution plans.