mysql show 0 if selected value is null? - mysql

I have a long sql query that calculates a few things about payments and shipments. In some cases the value is null. I think that's because there's being divided by 0.
Here's a small part of my query:
ROUND(sum(case when shipping_method = 'c' AND (paid_amount - shipping_costs) < 70 then 1 end) * 100 / sum(case when shipping_method = 'c' then 1 end),2) as co_less_70,
I think that when this part is 0: sum(case when shipping_method = 'colissimo' then 1 end),2) my query shows null. Is there any way to assign a default value for this co_less_70 column?

Yes. You can use the COALESCE() function:
http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#function_coalesce
Returns the first non-NULL value in the list, or NULL if there are no non-NULL values.
In your code:
COALESCE(
ROUND(sum(case when shipping_method = 'c' AND (paid_amount - shipping_costs) < 70 then 1 end) * 100 / sum(case when shipping_method = 'c' then 1 end),2),
0
) AS co_less_70

IFNULL(
ROUND(sum(case when shipping_method = 'c' AND (paid_amount - shipping_costs) < 70 then 1 end) * 100 / sum(case when shipping_method = 'c' then 1 end),2)
,<default>) AS co_less_70,

You can set a default case, but anything except zero :-
sum(case when shipping_method = 'colissimo' then 1 else some_value end case),2)

Related

NULL values populating when trying to calculate a percentage in MYSQL

SELECT
SUM(CASE WHEN Reason = 'Forced' then 1 Else 0 end) Total_Number_Forced_Outage_Events,
COUNT(*) - 'Total_Number_Forced_Outage_Events' AS Total_Number_Outage_Events,
ROUND(('Total_Number_Forced_Outage_Events' / 'Total_Number_Outage_Events') * 100) AS Forced_Outage_Percentage,
YEAR(Start_Time) as Year
FROM AEMR
WHERE Status = 'Approved'
GROUP BY
Reason,
YEAR(Start_Time)
ORDER BY
YEAR(Start_Time),
Reason
OUTPUT
Total_Number_Forced_Outage_Events
Total_Number_Outage_Events
Forced_Outage_Percentage
Year
0
181
NULL
2016
1264
1264
NULL
2016
0
106
NULL
2016
0
380
NULL
2016
0
127
NULL
2017
1622
1622
NULL
2017
0
102
NULL
2017
0
320
NULL
2017
You can use coalesce() to replace NULL values with 0:
SELECT
SUM(CASE WHEN Reason = 'Forced' then 1 Else 0 end) Total_Number_Forced_Outage_Events,
COUNT(*) - Coalesce('Total_Number_Forced_Outage_Events',0) AS Total_Number_Outage_Events,
ROUND((Coalesce('Total_Number_Forced_Outage_Events',0) / Coalesce('Total_Number_Outage_Events',0)) * 100) AS Forced_Outage_Percentage,YEAR(Start_Time) as Year
FROM AEMR
WHERE Status = 'Approved'
GROUP BY
Reason,
YEAR(Start_Time)
ORDER BY
YEAR(Start_Time), Reason
You used column aliases in same select list I don't know which database allow it so I am changing the query:
SELECT
SUM(CASE WHEN Reason = 'Forced' then 1 Else 0 end) Total_Number_Forced_Outage_Events,
COUNT(*) - SUM(CASE WHEN Reason = 'Forced' then 1 Else 0 end) AS Total_Number_Outage_Events,
ROUND((SUM(CASE WHEN Reason = 'Forced' then 1 Else 0 end) / (COUNT(*) - SUM(CASE WHEN Reason = 'Forced' then 1 Else 0 end))) * 100,0) AS Forced_Outage_Percentage,YEAR(Start_Time) as Year
FROM AEMR
WHERE Status = 'Approved'
GROUP BY
Reason,
YEAR(Start_Time)
ORDER BY
YEAR(Start_Time), Reason
SELECT
SUM(CASE WHEN Reason = 'Forced' THEN 1 ELSE 0 END) Total_Number_Forced_Outage_Events
,Count(*) as Total_Number_Outage_Events
,CAST((CAST(SUM(CASE WHEN Reason = 'Forced' THEN 1 ELSE 0 END)AS DECIMAL(18,2))/CAST(Count(*)
AS DECIMAL(18,2)))*100 AS DECIMAL(18,2)) as Forced_Outage_Percentage
,Year(Start_Time) as Year
FROM
AEMR
WHERE Status = 'Approved'
GROUP BY
Year(Start_Time)

SSRS calculate % of desired colums in column group in matrix

SELECT PatchPhase.PhaseName, MAX(PatchPhase.Sequence) AS seq,
PatchState.Application, PatchState.Objectname, PatchState.Jobname,
PatchState.Streamname, COUNT(*) AS Total,
PatchState.Jobdescription,
PatchState.Status, PatchState.Timestamp
FROM PatchState
INNER JOIN PatchPhase ON PatchState.Phase = PatchPhase.PhaseTech
WHERE (PatchPhase.PhaseName IN (#Phase))
AND (PatchState.Application IN (#Application)) AND (PatchState.Timestamp >= #StartDate)
AND (PatchState.Timestamp <= #EndDate)
GROUP BY PatchPhase.PhaseName, PatchState.Application, PatchState.Objectname,
PatchState.Jobname, PatchState.Streamname, PatchState.Jobdescription, PatchState.Status,
PatchState.Timestamp
ORDER BY PatchState.Application
I am working on matrix and I have column group of status which contains 3 columns (planned, running,completed). I want to sum planned+completed and divide by total column.
Total column is outside the column group.
I do find some answers but I did not get how should I use in my code
can someone please help?
Try using this to aggregate, and then use a table instead of a matrix. You will not need a column group now. As I cannot run the sql I can't promise it is perfect but perhaps you could have a little play with it if not. It should offer a guide if nothing else.
SELECT
PatchPhase.PhaseName,
MAX(PatchPhase.Sequence) AS seq,
PatchState.Application,
PatchState.Objectname,
PatchState.Jobname,
PatchState.Streamname,
PatchState.Jobdescription,
SUM(case when PatchState.Status = 'Planned' then 1 else 0 end) as 'Planned',
SUM(case when PatchState.Status = 'Running' then 1 else 0 end) as 'Running',
SUM(case when PatchState.Status = 'Completed' then 1 else 0 end) as 'Completed',
(SUM(case when PatchState.Status = 'Planned' then 1 else 0 end) + SUM(case when PatchState.Status = 'Running' then 1 else 0 end)) /
(SUM(case when PatchState.Status = 'Planned' then 1 else 0 end) + SUM(case when PatchState.Status = 'Running' then 1 else 0 end) + SUM(case when PatchState.Status = 'Completed' then 1 else 0 end)) as totalPercentage
PatchState.Timestamp
FROM
PatchState
INNER JOIN PatchPhase
ON PatchState.Phase = PatchPhase.PhaseTech
WHERE
(PatchPhase.PhaseName IN (#Phase))
AND (PatchState.Application IN (#Application))
AND (PatchState.Timestamp >= #StartDate)
AND (PatchState.Timestamp <= #EndDate)
GROUP BY
PatchPhase.PhaseName,
PatchState.Application,
PatchState.Objectname,
PatchState.Jobname,
PatchState.Streamname,
PatchState.Jobdescription,
PatchState.Status,
PatchState.Timestamp
ORDER BY
PatchState.Application
You can get a value displayed in a text box from expression =Reportitems!Textbox133.Value
instead of textbox133 in the above expression you have to give the textbox name of the textbox from which you want the value. In your case the total column

COUNT data using case when with mysql

SELECT NAMA_DUN,
COUNT(case when ((RIGHT(noMyKid, 1))% 2) = 0 then 1 else 0 end) AS FEMALE,
COUNT(case when ((RIGHT(noMyKid, 1))% 2) = 1 then 1 else 0 end) AS MALE,
COUNT(DISTINCT(noMyKid)) as jumlah
FROM mohon
LEFT JOIN dun ON dun.KOD_DUN=mohon.dun_nama
WHERE status_proses = 'diproses'
AND concat('20', substr(noMyKid, 1, 2)) = '2008'
AND status_mohon = 'Layak'
AND status_semak = '1'
AND (
status_bayar = ''
OR status_bayar = 'Belum'
OR status_bayar = 'Sudah')
AND (
status_terima = ''
OR status_terima = 'Terima'
) GROUP BY dun_nama
ORDER BY NAMA_DUN
This is my mysql code. why is my 'COUNT CASE WHEN' give the same output for female and male column.
Usually COUNT() is used to count rows and is therefore very often used in the form COUNT(*). When you use a field (or anything else) as a parameter into the COUNT() it counts 1 for every no NULL value.
In your case all your values are not NULL (they are either 1 or 0) and consequently you end up with the same results.
So Abhik Chakraborty is right, use SUM() and everything should be fine.
The COUNT() function does only recognize non-null values, so when using case expressions such as you have here, you can explicitly return NULL where needed
SELECT NAMA_DUN,
COUNT(case when ((RIGHT(noMyKid, 1))% 2) = 0 then 1 else NULL end) AS FEMALE,
COUNT(case when ((RIGHT(noMyKid, 1))% 2) = 1 then 1 else NULL end) AS MALE,
COUNT(DISTINCT(noMyKid)) as jumlah
OR, implicitly return NULL by ignoring the else condition
SELECT NAMA_DUN,
COUNT(case when ((RIGHT(noMyKid, 1))% 2) = 0 then 1 end) AS FEMALE,
COUNT(case when ((RIGHT(noMyKid, 1))% 2) = 1 then 1 end) AS MALE,
COUNT(DISTINCT(noMyKid)) as jumlah
OR, mimic the effect of count by using SUM() provided you do use 1 and 0 (or 1 and NULL)
SELECT NAMA_DUN,
SUM(case when ((RIGHT(noMyKid, 1))% 2) = 0 then 1 else 0 end) AS FEMALE,
SUM(case when ((RIGHT(noMyKid, 1))% 2) = 1 then 1 else 0 end) AS MALE,
COUNT(DISTINCT(noMyKid)) as jumlah

Is it possible to use SUM - CASE - IF together?

When I try to use this:
SUM(CASE WHEN IF(SUM(CASE WHEN "b.count_students_status" = 1 THEN 1 ELSE 0 END) >= 1, 1, 0) = 1 THEN 1 ELSE 0 END)
It says Query Error: Improper usage of Group Function
Below is the complete code:
SELECT
"a.batch" AS Batch,
SUM(
CASE
WHEN IF(
SUM(
CASE
WHEN "b.count_students_status" = 1
THEN 1
ELSE 0
END
) >= 1,
1,
0
) = 1
THEN 1
ELSE 0
END
) AS Payments_Not_Received
FROM
"DBU - Complete"
WHERE "a.suspended" = 'no'
GROUP BY "a.batch"
I wanted to convert the status to 1, if there are multiple occurrences and then sum the total occurrences.
Any help please - I am using ZOHO to build the query?
You're trying to reference the result of the GROUP BY before is has run. Try something like this:
select t.BATCH,
sum(case when t.pnr >=1 then 1 else 0 end) as Payments_Not_Received
from
(
select
a.batch as BATCH,
SUM(CASE WHEN "b.count_students_status" = 1 THEN 1
ELSE 0
END) as pnr
from yourTable a
WHERE a.suspended = 'no'
group by a.batch
) t
group by t.BATCH;

MySQL using Sum and Case

I'm trying to create a GridView with ASP.NET connecting to a MySQL database. The data appears like below.
BusinessUnit OrderDate Canceled
UnitA 1/15/2013 N
UnitA 10/1/2013 N
UnitB 10/15/2013 N
UnitB 10/22/2013 N
UnitB 10/22/2013 N
Based on the records above, I'd like the result to appear like below
BusinessUnit TodaysOrders ThisMonthsOrders ThisYearsOrders
UnitA 0 1 2
UnitB 2 3 3
My current code is below. It's giving me error (something about DatabaseName.sum does not exist. Check the
Function Name Parsing and Resolution' section... )
Select
SUM (CASE WHEN (OrderDate)=DATE(NOW()) THEN 1 ELSE 0 END) AS TodaysOrders,
SUM (CASE WHEN YEAR(OrderDate) = YEAR(CURDATE()) AND MONTH(OrderDate) = MONTH(CURDATE()) THEN 1 ELSE 0 END) AS ThisMonthsOrders,
SUM (CASE WHEN YEAR(main_order_managers.creation_date) = YEAR(CURDATE()) THEN 1 ELSE 0 END) AS ThisYearsOrders
code continues
FROM OrderTable WHERE OrderTable.Canceled. <> 'Y';
Is Sum Case the best use here?
The error is caused by the space between function name and parenthesis
SUM (CASE WHEN ...
^^
Read more Function Name Parsing and Resolution
Try
SELECT BusinessUnit,
SUM(CASE WHEN OrderDate = CURDATE() THEN 1 ELSE 0 END) TodaysOrders,
SUM(CASE WHEN DATE_FORMAT(OrderDate, '%Y%m') = DATE_FORMAT(CURDATE(), '%Y%m') THEN 1 ELSE 0 END) ThisMonthsOrders,
SUM(CASE WHEN YEAR(OrderDate) = YEAR(CURDATE()) THEN 1 ELSE 0 END) ThisYearsOrders
FROM OrderTable
WHERE Canceled <> 'Y'
GROUP BY BusinessUnit
Here is SQLFiddle demo