How to get the cumulative sum of an aggregate column? - sql

I have this query in BigQuery that returns the representation of total contributing_factor_vehicle_1
SELECT
TBL_TOTAL.contributing_factor_vehicle_1,
TBL_TOTAL.TOTAL,
(TBL_TOTAL.TOTAL / SUM(TBL_TOTAL.TOTAL) OVER ()) * 100 AS PERCENTAGE
FROM
(SELECT
contributing_factor_vehicle_1,
COUNT(contributing_factor_vehicle_1) AS TOTAL
FROM
`bigquery-public-data.new_york_mv_collisions.nypd_mv_collisions`
WHERE
borough = 'BROOKLYN'
AND contributing_factor_vehicle_1 <> 'Unspecified'
GROUP BY
contributing_factor_vehicle_1
ORDER BY
TOTAL DESC) TBL_TOTAL
ORDER BY
TOTAL DESC
Output:
contributing_factor_vehicle_1
TOTAL
PERCENTAGE
Driver Inattention/Distraction
65427
28.913538237178777
Failure to Yield Right-of-Way
25831
11.415250679452903
Backing Unsafely
16384
7.240426895286917
Following Too Closely
12605
5.570408997503148
Passing Too Closely
10875
4.805886382217116
Now I need to get the cumulative PERCENTAGE to make a pareto analysis:
How do I achieve it please? Is it possible to use the column PERCENTAGE in a window function again?
contributing_factor_vehicle_1
TOTAL
PERCENTAGE
PERCENTAGE CUM
Driver Inattention/Distraction
65427
28.91%
28.91%
Failure to Yield Right-of-Way
25831
11.42%
40.33%
Backing Unsafely
16384
7.24%
47.57%
Following Too Closely
12605
5.57%
53.14%
Passing Too Closely
10875
4.81%
57.95%

Just add one more line into the outer SELECT as in below example
SELECT
TBL_TOTAL.contributing_factor_vehicle_1,
TBL_TOTAL.TOTAL,
ROUND((TBL_TOTAL.TOTAL/SUM(TBL_TOTAL.TOTAL) OVER ())* 100, 2) AS PERCENTAGE,
ROUND(((SUM(TBL_TOTAL.TOTAL) OVER (ORDER BY TOTAL DESC))/SUM(TBL_TOTAL.TOTAL) OVER ())* 100, 2) AS PERCENTAGE_CUM
FROM
(
SELECT
contributing_factor_vehicle_1,
COUNT(contributing_factor_vehicle_1) AS TOTAL
FROM `bigquery-public-data.new_york_mv_collisions.nypd_mv_collisions`
WHERE borough = 'BROOKLYN' AND contributing_factor_vehicle_1 <> 'Unspecified'
GROUP BY contributing_factor_vehicle_1
ORDER BY TOTAL DESC
) TBL_TOTAL
ORDER BY TOTAL DESC
with output

Related

Percentage for each row - SQL

I have a Searches table for pets. I want to see the percentage of search per Animal-name.
My query is:
Select AVG(a.search_count) OVER (ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT
ROW)*100 AS Precentage
from (select [Animal-Name], count (*) as search_count
from dbo.Searches
group by [Animal-Name]
) as a
What I get from the second select (inside the from):
so what I need now is the percentage of search_count result for each animal-name.
any idea what is wrong with my query?
I think you want a simple ratio:
select [Animal-Name], count(*) as search_count,
count(*) * 1.0 / sum(count(*)) over () as ratio
from dbo.Searches
group by [Animal-Name];
The * 1.0 is because the code looks like SQL Server and SQL Server does integer arithmetic -- so 1 / 2 is 0 rather than 0.5.

How to calculate metrics between two tables

How to calculate metrics between two tables? In addition, I noticed that when using FROM tbl1, tbl2, there are noises, the WHERE filters did not work, a total count(*) was returned
Query:
select
count(*) filter(WHERE tb_get_gap.system in ('LINUX','UNIX')) as gaps,
SUM(CAST(srvs AS INT)) filter(WHERE tb_getcountsrvs.type = 'LZ') as total,
100 - (( gaps / total ) * 100)
FROM tb_get_gap, tb_getcountsrvs
Error:
SQL Error [42703]: ERROR: column "gaps" does not exist
I need to count in the tb_get_gap table by fields = ('LINUX', 'UNIX'), then a SUM ()in thesrvs field in the
tb_getcountsrvs table by fields = 'LZ' in type, right after
making this formula 100 - ((gaps / total) * 100)
It would seem that you cannot define gaps and also use it in the same query. In SQL Server you would have to use the logic twice. Maybe a subquery would work better.
select 100 - (t.gaps / t.total) * 100)
from
(
select
count(*) filter(WHERE tb_get_gap.system in ('LINUX','UNIX')) as gaps,
SUM(CAST(srvs AS INT)) filter(WHERE tb_getcountsrvs.type = 'LZ') as total
FROM tb_get_gap, tb_getcountsrvs
) t

SQL Percentage Queries

I'm a beginner in SQL and my question is about calculating a percentage of the overall disclosed total from a table called merged. I want to calculate the number of 'SUPPORT' from committee_position (a column in the table merged)
How to calculate the percentage in that case.
I start with:
SELECT Sum (amount) *100
from merged
where merged.committee_position == 'SUPPORT';
Help me continue it, Thank you
If I followed you correctly, you can do conditional aggregation:
select
100.0 * sum(case when committee_position = 'SUPPORT' then amount else 0 end) / sum(amount)
from merged
This gives you the percentage of amount that have committee_position = 'SUPPORT' over the total amount in the table.
Here you go.
SELECT a.Support_Amount/b.Total_Amount*100
FROM (SELECT Sum (amount) as Support_Amount
from merged
where merged.committee_position = 'SUPPORT') as a
CROSS JOIN
(SELECT Sum (amount) as Total_Amount
from merged) as b

average query ORA-00936 error

SQL> SELECT consignmentNo, VoyageNo, Weight
2 (SELECT (AVG(WEIGHT) FROM consignment), AS AVERAGE,
3 WHERE Weight = 650,
4 FROM consignment;
(SELECT (AVG(WEIGHT) FROM consignment), AS AVERAGE,
*
ERROR at line 2:
ORA-00936: missing expression
average weight for a particular ship, listing consignments for the particular ship also, unable to identify the error
Are you simply looking for group by?
SELECT VoyageNo, AVG(Weight)
FROM consignment
GROUP BY VoyageNo;
If you want the average along with the detailed information, you want a window function:
SELECT c.*, AVG(Weight) OVER (PARTITION BY VoyageNo)
FROM consignment c;
This assumes that VoyageNo is what you mean by ship.
You seems want :
SELECT consignmentNo, VoyageNo, Weight, avg.AVERAGE
FROM consignment CROSS JOIN
(SELECT AVG(WEIGHT) AS AVERAGE FROM consignment) avg
WHERE Weight = 650;
You have an extra , in your query (before AS AVERAGE) and you are missing a , after Weight. Also from and where is not in the right order. Try this:
SELECT consignmentNo, VoyageNo, Weight,
(SELECT (AVG(WEIGHT) FROM consignment) AS AVERAGE,
FROM consignment
WHERE Weight = 650;

sql sum different column value descending

I have this query:
Available Total Usage
7000.0 7021.9
7000.0 -15000.00
7000.0 -7700.85
I want to create new column to sum both column in descending order.The result I wish is
Available Total Usage Total
7000.0 7021.9 -700.85+(-15000.00) = -15700.85
7000.0 -15000.00 7000+(-7700.85) = -700.85
7000.0 -7700.85 7000
How can I do this?
try this:
with cte as(select Available, [Total Usage], Available+[Total Usage] as Total from <table>)
select * from CTE order y Total