Output getting a zero for division - division

As I am trying to calculate 'PUT', when I run the query I get 0 for that field. Any suggestions on how to fix this?
SELECT
COUNT(x_reasons) as Cancel,
week,
COUNT(case when on_plan = '1' then 1 else null end) *1.0 / count(*) AS PUT
FROM inbound.otpu_plan_raw
WHERE
week IN ('43','44','45','46','47','48', '49','50')
AND x_reasons like '%CLOSED%' and code = 'VC'
Group By 1,2
Can someone tell me why I am getting a zero in Put output?

Related

CASE STATEMENT + COUNT function not working - SQL Big Query

when i run this code it does not seem to be working in big query...
SELECT date,
COUNT(CASE WHEN product = "fan" AND product color = "white"
THEN product_code ELSE "null" END) AS number_of_whitefans,
FROM `radiant-oven-328313.customer_data.customer_purchase`
GROUP by date
Been trying to run this code many times and i wonder if it's any commas i've missed or just the order of the syntax? i'm running this using bigquery - should i have included a else statement? I've been following the below line of code logic from datacamp so could someone tell me if i've replicated the same thing for my code above with a different data set?
SELECT season,
COUNT(CASE WHEN hometeam_id = 8650
AND home_goal > away_goal
THEN match_id END) AS home_wins
COUNT(CASE WHEN awayteam_id = 8650
AND away_goal > home_goal THEN id END) AS away_wins
From match
GROUP by season;

How to display marks in Percentage in SQL?

As an example, I have this dataƩ
Total submission 170
Passed Submission 32
Failed Submission 137
Skipped Submission 1
I need to be able to show:
Pass percentage
Fail Percentage
Skip percentage
Can someone help achieve this in SQL?
since I dont know what the table looks like here is an example on how it could look
select
round(pass/total,2)*100 as passPercentage
, round(fail/total,2)*100 as failPercentage
, round(skip/total,2)*100 as skipPercentage
from table
Its Worked For Me - Answer is :
convert(float,Secondset.PassedSubmission)*100/convert(float,FirstSet.TotalSubmission) as PassPercentage,
(convert(float,FirstSet.TotalSubmission)-(Convert(float,Secondset.PassedSubmission)+Convert(float,thirdset.SkippedSubmission)))*100/convert(float,FirstSet.TotalSubmission)) as FailPercentage,
convert(float,thirdset.SkippedSubmission)*100/convert(float,FirstSet.TotalSubmission) as SkipPercentage
It depends on how your data is structured and how you want to return your results. If you have a table with one row per submission and a status field, you can do either of these:
SELECT status, (COUNT(*) * 1.00) / (SELECT COUNT(*) FROM submission) AS status_pct
FROM submission
GROUP BY status
;
SELECT
(SUM(CASE WHEN status = 'passed' THEN 1 ELSE 0 END) * 1.00) / COUNT(*) AS pass_pct,
(SUM(CASE WHEN status = 'failed' THEN 1 ELSE 0 END) * 1.00) / COUNT(*) AS fail_pct,
(SUM(CASE WHEN status = 'skipped' THEN 1 ELSE 0 END) * 1.00) / COUNT(*) AS skip_pct
FROM submission
;
SQL Fiddle

Using SUM SEC_TO_TIME in MariaDB

Reference from How to sum time using mysql
I want to SUM Field LogsFormatted.Late Every month with query :
SELECT
SUM(CASE
WHEN MONTH (LogsFormatted.DateIn) = 1
THEN SEC_TO_TIME( SUM( TIME_TO_SEC(LogsFormatted.Late)))
ELSE 0 END
) AS '1'
FROM
HrAttLogsFormatted AS LogsFormatted
But the result is
1111 - Invalid use of group function
Where is the problem with the query? resulting in an error output.. Thank you in advance
[EDIT-SOLVED] It's Solved with simply apply
Change format SUM at the beginning of the query
SEC_TO_TIME(SUM(
CASE WHEN MONTH(LogsFormatted.DateIn) = 1 THEN
TIME_TO_SEC(LogsFormatted.Late) END)
) AS '1'
You don't need to call the sum() so many times. You can also move the case condition to the WHERE clause:
SELECT SUM(TIME_TO_SEC(lf.Late))
FROM HrAttLogsFormatted lf
WHERE MONTH(lf.DateIn) = 1 ;
If you want conditional aggregation, then do:
SELECT SUM(CASE WHEN MONTH(lf.DateIn) = 1 THEN TIME_TO_SEC(lf.Late) END)
FROM HrAttLogsFormatted lf;

get a percentage of daily errors

i have 1 table with 2 columns i work with (time,status)
i select a certain day with date_trunc() in the time column and apply condition where status = '404 NOT FOUND'
and divide that with the daily count
to get a percentage of daily errors
status has 2 values 404 NOT FOUND and 200 OK
--------------------------------------------
i wanna get the daily error percentage
i tried :
select case when status = '404 NOT FOUND' then count(time) END / count(time) from log group by date_trunc('day',time);
but got an error i get error column "log.status" must appear in the GROUP BY clause or be used in an aggregate function
You can use something like this:
SELECT days, (ERROR*1.0/TOTAL)*100.0 Percentage FROM
(select date_trunc('day',time) days,
COUNT(case when status = '404 NOT FOUND'
then 1 ELSE NULL END) ERROR,
COUNT(1) TOTAL
from log
group by date_trunc('day',time)) A;
I would do this as:
select date_trunc('day', time) as dte,
avg(case when status <> '200 OK' then 1.0 else 0 end) as daily_rate
from log l
group by dte;
In Postgres, this can further be shortened to:
select date_trunc('day', time) as dte,
avg( (status <> '200 OK')::int ) as daily_rate
from log l
group by dte;

Sum by Distinct Values

I have the below code which brings though a sum of where a day 'GRLastDt' has complete or not complete however I hadn't accounted for duplicated Return IDs. Is there a way to return a sum by day for each Return ID?
For example day 24/5/15 may have 5 Lines to the day which have 'X' in Complete however 2 lines have duplicated return ids 'RtnId' and therefore the sum total would be 4 rather than 5 for 24/5/15.
SELECT
CONVERT(varchar(15), GRLastDt, 111) AS Date_,
SUM(CASE WHEN Complete = 'X' THEN 1 ELSE 0 END) AS Complete,
SUM(CASE WHEN Complete <> 'X' /*and RtnDt <>GRLastDt*/THEN 1 ELSE 0 END) AS Due
FROM [dbo].[vw_AN_Admin_VendorReturns]
WHERE (GRLastDt >= GETDATE() - 30)
GROUP BY CONVERT(varchar(15), GRLastDt, 111),GRLastDt
If I understand your problem right I think you can change this line:
SUM(CASE WHEN Complete = 'X' THEN 1 ELSE 0 END) AS Complete,
to this:
COUNT(DISTINCT CASE WHEN Complete = 'X' THEN RtnId END) AS Complete,
You probably want the same change for the Due calculation.