trying to figure out how to incorporate an OR in a case statement in T-SQL
I'm essentially checking for 2 things, and if either one is true, just look at them as 0 for the count.
COUNT(Case When (car[Weight] IS null) then 0 else car.CarKey
OR When (car.BinNumber is null) then 0 else car.CarKey
End) as Carkey
also tried this but syntax is wrong
COUNT(Case When (car[Weight] IS null) then 0
else When (car.BinNumber is null) then 0
else car.CarKey
End) as Carkey
You can use an OR this way:
COUNT(Case
When (car.[Weight] IS null) or (car.BinNumber is null)
then 0
else car.CarKey End) as Carkey
You were close with your second attempt, just remove one else
COUNT(Case When (car[Weight] IS null) then 0
When (car.BinNumber is null) then 0
else car.CarKey
End) as Carkey
Note: If you want to count the items without null values, that won't do it. A zero value is still a value, so it will also be counted. Use null for items that you don't want to count:
COUNT(Case When (car[Weight] IS null) then null
When (car.BinNumber is null) then null
else car.CarKey
End) as Carkey
Or use sum instead:
SUM(Case When (car[Weight] IS null) then 0
When (car.BinNumber is null) then 0
else 1
End) as Carkey
Related
select product_name ,0 price1,0 price2,0 price3,
(CASE when sum(price)>100 then 1 else 0 end) as price4,0 price5
from sales_1
group by product_name,price
union
select product_name ,0 price1,0 price2,0 price3, 0 price4,
(CASE when sum(price)<100 then 'yes' else 'no' end) as price5
from sales_1
group by product_name,price
I want values which are less then 100 to turn into 'no' and others to 'yes' but it is throwing an error which is'UNION types integer and text cannot be matched' .i have tried different type of casting to solve it but it didn't. and i am doing it in postgresql
This is the code which got me to my required result:
SELECT product_name,
0 price1, 0 price2, 0 price3,
(CASE WHEN SUM(price)>100 THEN 'yes' ELSE 'no' END) AS price4,
'' price5
FROM sales_1
GROUP BY product_name,price
UNION ALL
SELECT product_name,
0 price1, 0 price2, 0 price3, '' price4,
(CASE WHEN SUM(price)<100 THEN 'yes' ELSE 'no' END) AS price5
FROM sales_1
GROUP BY product_name, price
And this is the result I got from upper query:
I have a problem, I have a query in postgresql and I need to do a SUM, however, it returns me a total of 4 lines because the ids of my routes are different, and I need to add the "totalChecklists" and return the amount of all in one line.
select "users"."id",
count((cycles.adherence >= 100 and cycles.justified = false) or null) as "routesDone",
count((cycles.adherence < 100 and cycles.opened = false) or null) as "routesNotDone",
count((cycles.opened = true and cycles.adherence < 100) or null) as "routesOpened",
count(cycles.justified or null) as "routesJustified",
routes."totalSpots" AS "totalCheck",
count(cycles.id) as "routesOnPeriod",
(
count((cycles.adherence >= 100 and cycles.justified = false) or null) +
count((cycles.adherence < 100 and cycles.opened = false) or null) +
count((cycles.opened = true and cycles.adherence < 100) or null) +
count(cycles.justified or null)
) as "routesTotal",
0 as "routesDoneLate",
0 as "routesLate",
SUM(case when cycles.adherence >= 100 then routes."totalAssets" else 0 end) AS "coveredAssets",
SUM(case when cycles.adherence < 100 then routes."totalAssets" else 0 end) AS "uncoveredAssets"
from "routes"
inner join "cycles" on "routes"."identifier" = "cycles"."routeIdentifier"
inner join "workspaces" on "cycles"."workspaceId" = "workspaces"."id"
inner join "users" on "users"."id" = "cycles"."inspectorId"
where "routes"."deleted_at" is null
and "cycles"."deletedAt" is null
and "workspaces"."deleted" = false
and "cycleStartAt" <= '2022-07-11T02:59:59Z'
and "cycleEndAt" >= '2022-04-12T03:00:00Z'
and "users"."id" = 'b67830a7-39fc-4ad5-bf26-07a43dcd3676'
and "routes"."contextPath" like '/malicious-interaction-murder-32%'
and "routes"."deleted_at" is null
group by
users.id,
routes.id
My return:
Sorry for my bad english, I hope I managed to be very clear.
Thank you very much in advance
I'm trying to count the distinct number of ids in a column and this works fine.
COUNT(DISTINCT messages.id) AS link_created
But when I try to count with a conditional, I get a syntax error, what's the proper syntax to add a case when or some other condition to only count the distinct message ids where the messages.link_label is present?
COUNT(DISTINCT messages.id CASE WHEN messages.link_label IS NOT NULL 1 END) AS link_created
My full query looks like this.
#customers = Customer.select("customers.*,
COUNT(DISTINCT recipient_lists.id) messages_sent,
COUNT(DISTINCT messages.id CASE WHEN messages.link_label IS NOT NULL 1 END) AS link_created,
COALESCE(SUM(video_activities.video_watched_count),0) AS watched_count,
COALESCE(SUM(video_activities.response_count),0) AS response_count,
COALESCE(SUM(video_activities.email_opened_count),0) AS email_opened_count,
COALESCE(SUM(CASE WHEN video_activities.video_watched_at IS NOT NULL THEN 1 ELSE 0 END),0) AS unique_watches,
COALESCE(SUM(CASE WHEN video_activities.email_opened_at IS NOT NULL THEN 1 ELSE 0 END),0) AS unique_opens,
COALESCE(SUM(CASE WHEN video_activities.response_count > 0 THEN 1 ELSE 0 END),0) AS unique_responses,
customers.updated_at AS last_login,
SUBSTRING( email from POSITION( '#' in email) + 1 for length(email)) AS company")
.joins("LEFT JOIN messages ON customers.id = messages.customer_id
LEFT JOIN recipient_lists ON messages.id = recipient_lists.message_id AND messages.link_label is NULL
LEFT JOIN video_activities ON messages.id = video_activities.message_id")
.group("customers.id")
Try this:
COUNT(DISTINCT CASE
WHEN messages.link_label IS NOT NULL
THEN messages.id
ELSE NULL END)
AS link_created
I am running this update query, and I was hoping it would update the table to hold a value of 0 instead of displaying NULL when the count is 0. How can this be updated to force the table so show a 0 when the count returned is 0?
set #sql =
'UPDATE cra
SET mastercounts = te.LC
FROM #tbl_test As cra
JOIN (
select
,COUNT(CASE WHEN recordID < 1 THEN 0 ELSE recordID end) As LC
,employeename
from productioninformaiton
where salestatus = (''Approved'', ''Shipped'')
Group By employeename
) As te
ON cra.[StoreName] = te.[StoreName]
AND cra.[employeename] = te.[employeename]'
Exec (#sql)
Maybe the field "recordID" is returning NULL in some cases, which makes COUNT(NULL) return NULL as well. Try replacing
COUNT(CASE WHEN recordID < 1 THEN 0 ELSE recordID end) As LC
with
COUNT(CASE WHEN ISNULL(recordID, 0) < 1 THEN 0 ELSE ISNULL(recordID, 0) end) As LC
My results are showing both counts the same but there should be some that have different counts as CarCode is sometimes null.
SELECT distinct car.carKey,
car.Weight,
car.CarCode,
COUNT(car.carKey)OVER(PARTITION BY car.carKey) AS TotalCarKeyCount,
COUNT(Case When (car.[Weight] IS not null) and (car.CarCode is null) as CarCountWithoutCode
then 0
else car.carKey End) OVER(PARTITION BY car.carKey) AS CarCount
from car
results show TotalCarKeyCount and CarCountWithoutCode always with the same counts like the case statement isn't working or something.
It sounds like you might want to use SUM() instead:
SELECT distinct car.carKey,
car.Weight,
car.CarCode,
COUNT(car.carKey)OVER(PARTITION BY car.carKey) AS TotalCarKeyCount,
SUM(Case When (car.[Weight] IS not null) and (car.CarCode is null) as CarCountWithoutCode
then 0 else 1 End) OVER(PARTITION BY car.carKey) AS CarCount
from car
SQL Fiddle demo showing the difference between using COUNT() and SUM():
create table test
(
id int
);
insert into test values
(1), (null), (23), (4), (2);
select
count(case when id is null then 0 else id end) [count],
sum(case when id is null then 0 else 1 end) [sum]
from test;
Count returns 5 and Sum returns 4. Or you can change the COUNT() to use null and the null values will be excluded in the final count()
select
count(case when id is null then null else id end) [count],
sum(case when id is null then 0 else 1 end) [sum]
from test;
Your query would be:
SELECT distinct car.carKey,
car.Weight,
car.CarCode,
COUNT(car.carKey)OVER(PARTITION BY car.carKey) AS TotalCarKeyCount,
COUNT(Case When (car.[Weight] IS not null) and (car.CarCode is null) as CarCountWithoutCode
then null else 1 End) OVER(PARTITION BY car.carKey) AS CarCount
from car
Change the then 0 to then null. Zero values are counted, nulls are not.