How to display marks in Percentage in SQL? - 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

Related

SQL - Dividing aggregated fields, very new to SQL

I have list of line items from invoices with a field that indicates if a line was delivered or picked up. I need to find a percentage of delivered items from the total number of lines.
SALES_NBR | Total | Deliveryrate
1 = Delivered 0 = picked up from FULFILLMENT.
SELECT SALES_NBR,
COUNT (ITEMS) as Total,
SUM (case when FULFILLMENT = '1' then 1 else 0 end) as delivered,
(SELECT delivered/total) as Deliveryrate
FROM Invoice_table
WHERE STORE IN '0123'
And SALE_DATE >='2020-02-01'
And SALE_DATE <='2020-02-07'
Group By SALES_NBR, Deliveryrate;
My query executes but never finishes for some reason. Is there any easier way to do this? Fulfillment field does not contain any NULL values.
Any help would be appreciated.
I need to find a percentage of delivered items from the total number of lines.
The simplest method is to use avg():
select SALES_NBR,
avg(fulfillment) as delivered_ratio
from Invoice_table
where STORE = '0123' and
SALE_DATE >='2020-02-01' and
SALE_DATE <='2020-02-07'
group by SALES_NBR;
I'm not sure if the group by sales_nbr is needed.
If you want to get a "nice" query, you can use subqueries like this:
select
qry.*,
qry.delivered/qry.total as Deliveryrate
from (
select
SALES_NBR,
count(ITEMS) as Total,
sum(case when FULFILLMENT = '1' then 1 else 0 end) as delivered
from Invoice_table
where STORE IN '0123'
and SALE_DATE >='2020-02-01'
and SALE_DATE <='2020-02-07'
group by SALES_NBR
) qry;
But I think this one, even being ugglier, could perform faster:
select
SALES_NBR,
count(ITEMS) as Total,
sum(case when FULFILLMENT = '1' then 1 else 0 end) as delivered,
sum(case when FULFILLMENT = '1' then 1 else 0 end)/count(ITEMS) as Deliveryrate
from Invoice_table
where STORE IN '0123'
and SALE_DATE >='2020-02-01'
and SALE_DATE <='2020-02-07'
group by SALES_NBR

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;

SQL statement selecting a count of a column

I have this SQL query:
SELECT
COUNT(SDDOCO) AS Total
FROM
KAIPRDDTA.F4211LA, KAIPRDDTA.Dates
WHERE
SDDRQJ = Day10
which returns a count of all the orders for today.
I am trying to get a second column so that I have this:
To get orders that are not completed would be: SDNXTR < '562'. How would I add this to my sql query so that I can accomplish this goal? Let me know if you need anymore information and thanks in advance for your responses.
You have two options here:
SELECT
COUNT(SDDOCO) AS Total,
SUM(CASE WHEN SDNXTR < '562' THEN 1 ELSE 0 END) AS Not_Finished_Yet_With_SUM,
COUNT(CASE WHEN SDNXTR < '562' THEN 1 ELSE NULL END) AS Not_Finished_Yet_With_COUNT,
FROM
KAIPRDDTA.F4211LA, KAIPRDDTA.Dates
WHERE
SDDRQJ = Day10
You can use case statement to count that SDNXTR < '562' values like below:
SELECT count(SDDOCO) as Total,
sum(case when SDNXTR < '562' then 1 else 0 end) as not_yet_finished
FROM KAIPRDDTA.F4211LA, KAIPRDDTA.Dates
WHERE SDDRQJ = Day10

SQL select grouping and subtract

i have table named source table with data like this :
And i want to do query that subtract row with status plus and minus to be like this group by product name :
How to do that in SQL query? thanks!
Group by the product and then use a conditional SUM()
select product,
sum(case when status = 'plus' then total else 0 end) -
sum(case when status = 'minus' then total else 0 end) as total,
sum(case when status = 'plus' then amount else 0 end) -
sum(case when status = 'minus' then amount else 0 end) as amount
from your_table
group by product
There is another method using join, which works for the particular data you have provided (which has one "plus" and one "minus" row per product):
select tplus.product, (tplus.total - tminus.total) as total,
(tplus.amount - tminus.amount) as amount
from t tplus join
t tminus
on tplus.product = tminus.product and
tplus.status = 'plus' and
tplus.status = 'minus';
Both this and the aggregation query work well for the data you have provided. In other words, there are multiple ways to solve this problem (each has its strengths).
you can query as below:
select product , sum (case when [status] = 'minus' then -Total else Total end) as Total
, sum (case when [status] = 'minus' then -Amount else Amount end) as SumAmount
from yourproduct
group by product

Work out percentage count using SQL CASE statement

I have the following code which tells me which line items are in and out of SLA.
How can I turn that into a %, so for example when I add them together it will show 98% SLA Met.
,CASE
WHEN m.COMPLETED_DT is NULL THEN ''
WHEN m.COMPLETED_DT <= m.SLA_ADJUSTED_DT THEN 'SLA Met'
WHEN m.SLA_ADJUSTED_DT IS NULL THEN 'SLA Met'
ELSE 'SLA Missed' END AS "SLA Desc"
If I had the result already, I think it would look something like...
SELECT (count(*) * 100 / (select count(*) FROM testtable)) AS YesSLA
FROM testtable where SLA='Yes';
I am not sure how to integrate that with my current statement, I don't believe I can reference the AS SLA Desc in a new statement.
Does this do what you want?
select 100 * avg(case when m.completed_dt <= m.SLA_ADJUSTED_DT or m.SLA_ADJUSTED_DT is null
then 1.0 else 0
end)
from testtable
where SLA = 'Yes';
The code below calculates the % met SLA out of 100 by counting only values that met SLA and then dividing by the total opportunities.
DECLARE #Data TABLE (COMPLETED_DT DATETIME, SLA_ADJUSTED_DT DATETIME)
INSERT #Data VALUES ('5/5/2014', '5/6/2014'), ('5/6/2014', '5/6/2014'), ('5/7/2014', '5/6/2014')
SELECT
CONVERT(FLOAT, SUM(CASE WHEN COMPLETED_DT <= SLA_ADJUSTED_DT THEN 1 ELSE 0 END)) * 100 / COUNT(1) AS [% Met SLA]
FROM #Data
Output
% Met SLA
----------------------
66.6666666666667