Aggregation using conditions in CASE WHEN in SQL Teradata? [duplicate] - sql

I work in SQL Teradata.
I would liek to count how many rows (clients) have rounded value in column "amount" (means for example 140.00 not 157.76 and so on). I use code like below:
select
client_id,
count(amount mod 1 = 0)
from table
group by client_id
Nevertheless, I have an error like: SELECT Failed. 3706: Syntax error: expected something between an integer and '='.
What can I do ?

Use a case expression:
sum(case when amount mod 1 = 0 then 1 else 0 end)
I'm not 100% sure if Teradata supports mod 1. I would normally write this as:
sum(case when amount = floor(amount) then 1 else 0 end)

Related

In SQL Server, how can I use the result of a comparison operator as a logical value?

I would like to compute something in an SQL Server result set that is the result of the value of comparison operators on other columns, something like
select (a > 2 AND b > 2 AND (c > 2 OR or c is null)) as "Reject"...
I just can't find any syntax that works.
SQL Server doesn't have a boolean type. Perhaps you intend:
select (case when a > 2 and b > 2 and (c > 2 or c is null)
then 1 else 0
end) as is_reject
SELECT CASE
WHEN A>2 AND B>2 AND (C>2 OR CIS NULL) THEN 'REJECT'
ELSE 'NO REJECT'
END
SQL Server has a limited concept of Booleans. They are used within logical expressions in statements like IF.. ELSE and WHILE and in WHERE clauses, but cannot be assigned to a variable or column directly. Use a CASE statement instead
SELECT
CASE WHEN a > 2 AND b > 2 AND (c > 2 OR c IS NULL)
THEN 1
ELSE 0
END AS "Reject"...
There is a BIT data type that can be either 0 or 1 and is commonly used to store truth values.
Also, SQL Server uses a three valued logic. See: Example of three valued logic in SQL Server

Turn row with 2 different value into 2 column oracle

I'm having trouble tunring a row with only 2 possible value (heads or tails for example ) into 2 column. I found Pivot but it doesn't seems to fit the problem.
Here's a little shema to help you understand what i'm trying to do :
Into this :
It's probably not very complicated yet I can't find the good words to explain my problem to google so he give's me the right solution !
You can use aggregation:
select name, date,
sum(case when type = 'Heads' then result else 0 end) as heads,
sum(case when type = 'Tails' then result else 0 end) as tails
from t
group by name, date;

SQL Server query to return 1 if value exist in a column else return 0

I am trying to query the database for checking if a specific column has a value or not. If there is a value in that column, the query should return 1, else it should return 0.
But my query is returning the total count of the columns for (ex:10).
Note: query is done in Dell Boomi integration platform, SQL Server.
select count (*)
from ApplicationRequest
where EmpID = '993685' and ApplID = '1';
Do you just want case?
select (case when count(*) > 0 then 1 else 0 end)
from ApplicationRequest
where EmpID = 993685 and ApplID = 1;
I removed the single quotes around the comparisons. If they are really numbers then single quotes are not appropriate. If they are indeed strings, then use the single quotes.
If this is what you want, a more efficient method would use exists:
select (case when exists (select 1
from ApplicationRequest
where EmpID = 993685 and ApplID = 1
)
then 1 else 0
end)
The aggregation query needs to find all matching rows. This version can stop at the first one.

Divide by zero error encountered even when divider is set as NOT NULL

I've come across the error "Divide by zero error encountered" when running this query.
> SUM(CASE WHEN EML_DateSent IS NOT NULL THEN 1 ELSE 0 END) AS [Sends],
(SUM(CASE WHEN EML_DateViewed IS NOT NULL OR EML_DateClicked IS NOT NULL THEN 1 ELSE 0 END)) * 100 / SUM((CASE WHEN EML_Datesent IS NOT NULL THEN 1 ELSE 0 END)) AS [Views %],
(SUM(CASE WHEN EML_DateClicked IS NOT NULL THEN 1 ELSE 0 END)) * 100 / SUM((CASE WHEN EML_DateViewed IS NOT NULL OR EML_DateClicked IS NOT NULL THEN 1 ELSE 0 END)) AS [Clicks %]
Its an edited existing stored procedure that now calculates percentages , any quick fix ?
Try using a max/maxium statement depending on what provider you are using.
/ MAX(SUM((CASE WHEN EML_DateViewed IS NOT NULL OR EML_DateClicked IS NOT NULL THEN 1 ELSE 0 END)), 1)
This will use your sum if it has a value, if it is zero the division will use 1 instead.
You don't show the grouping criteria but it's obvious at least one of the groups has one of the dates set to NULL over the entire group. First, you don't have to put all that logic in a sum function. The count function does that, counting all the not null values, ignoring the null values. Where that doesn't work is where you're checking both dates, but that is solved by a simple coalesce. You want a count of where one date or the other or both are not null. There you can play a little trick:
select count(EMS_DateSent) AS Sends,
count(coalesce(EMS_DateViewed, EMS_DateClicked)) * 100
/ case count(EMS_Datesent)
when 0 then 1000000
else count(EMS_Datesent)
end as "Views %",
count(EMS_DateClicked) * 100
/ case count(coalesce(EMS_DateViewed, EMS_DateClicked))
when 0 then 1000000
else count(coalesce(EMS_DateViewed, EMS_DateClicked))
end AS "Clicks %"
from EML
group by whatever;
If the divisor is 0 (all nulls over the group), I have set to a large number so you get a very small answer. But this must be large relative to actual counts in your application so adjust as needed.

How to count two fields by using Select statement in SQL Server 2005?

Total records in table are 10.
Select count(ID) from table1 where col1 = 1 (Result is 8)
Select count(ID) from table1 where col1 = 0 (Result is 2)
So its a same table but count is based on different condition. How am i gonna get two results (counts) using one select statement?
Also Performance is a big concern here.
PS: I am using Stored procedure...
EDIT:
I wanna clear the above query is just a part of a big SP logic (for me at least). Since i got these following answers, it gave another idea to achieve it in different way. My above question is a bit changed now.....Please help here? Its a same col (bool type) with true or false state.
Use CASE:
SELECT
SUM(CASE col1 WHEN 1 THEN 1 ELSE 0 END) AS Count1,
SUM(CASE col1 WHEN 0 THEN 1 ELSE 0 END) AS Count0
FROM table1
You should use subselects or UNIONS, I don't see the other way...