## calculate percentage contribution of each group based on certain condition SQL

sql calculate percentage of column

sql calculate percentage of two columns

sql percentage format

sql percentage of total sum

how to calculate percentage in sql oracle

percentage in sql w3schools

sql calculate percentage to 2 decimal places

I would like to calculate the percentage of "Quantity" contribution for 2 groups, group 1 where the "Customer ID" appeared more than once and group 2 where "Customer ID" is once. For example:

|ID | OrderID |Quantity |---|-------- |----- |1 |10248 |12 |2 |10248 |10 |3 |10248 |5 |4 |10249 |9 |5 |10249 |40 |6 |10250 |10

-----Into-----

OrderID |Quantity 10248 |27 10249 |49 10250 |10

----- Ultimately Into -----

Group % tot Quantity 1 12% <--- Just OrderID 10250 since it has only 1 order 2 88%

select 100.0 * sum(case when cnt = 1 then sumQty else 0 end) / sum(sumQty) as "single Order" ,100.0 * sum(case when cnt <> 1 then sumQty else 0 end) / sum(sumQty) as "multiple Orders" from ( select OrderID ,sum(Quantity) as sumQty ,count(*) as cnt from myTable group by OrderId ) as dt

**Calculating Percentage (%) of Total Sum · Advanced SQL · SILOTA,** SQL Recipes; Calculations Per Group; Calculating Percentage (%) of Total Sum How to compute the ratio of a particular row to the sum of values? to the relative contribution of a row against the backdrop of the total sum of all the rows. i have master table by which i am getting single Bed Charges and Double Bed charges also applicable tax percentage on both single and double bed. i want to to insert these record on the basis of their percentage. if percentage for both single and double bed are 0 then total sum of single and double bed charges will be save in taxfreecolumnamount in which i want to save data. otherwise if

Maybe this will help.Otherwise any wrong please inform me.

select a.OrderID,a.grp, Cast(Cast(a.Quantity*100 as decimal(18,2)) as varchar(100)) + ' %' as Percentage from ( select OrderID as OrderID,count(OrderID) as grp,Sum(Quantity) as Quantity from myTable group by OrderID ) as a

**SQL,** We can then divide the individual values by this sum to obtain the percent to total for each row. List of SQL Complex Operations. Operation, Description. Rank SQL > Advanced SQL > Percent To Total. To display percent to total in SQL, we want to leverage the ideas we used for rank/running total plus subquery. Different from what we saw in the SQL Subquery section, here we want to use the subquery as part of the SELECT. Let's use an example to illustrate. Say we have the following table, Table Total_Sales.

The other answers put the values into a single row, but the OP is asking for two rows. If this is important:

select (case when cnt = 1 then 1 else 2 end) as grp, qty / totQty as ratio from (select OrderID, sum(Quantity) as Qty, count(*) as cnt from myTable group by OrderId ) o cross join (select sum(Quantity) as totQty from myTable) x group by (case when cnt = 1 then 1 else 2 end);

**Need separate columns that calculate percentage,** Possible with a single SELECT : SELECT name, count(*), to_char((count(*) * 100.0 / sum(count(*)) OVER ()), 'FM990.00" %"') AS percent You can see by the results that taking time to better understand your options and altering your select statement you can not only get percent of total using Selects but do it in a very concise and optimized way.

Previous answer doesn't work it replies 33% This work fine:

select 100.0 * sum(case when cnt = 1 then sumQty else 0 end) / sum(sumQty) as 'single Order' ,100.0 * sum(case when cnt <> 1 then sumQty else 0 end) / sum(sumQty) as 'multiple Order' from ( select OrderID ,sum(Quantity) as sumQty -- not needed for the final result ,count(*) as cnt from _ds_test_ac group by OrderId ) as dt

If you want in two lines you can do :

select cnt = 1 as 'IS Single', 100.0 * sum(sumQty) / tot as 'percent' from ( select OrderID ,sum(Quantity) as sumQty -- not needed for the final result ,count(*) as cnt from _ds_test_ac group by OrderId ) as dt join ( select sum(Quantity) as tot from _ds_test_ac ) dtot group by 1

**Calculating percentage of a row over total sum,** To return all agents with their commissions and commission percentages use an analytic function with no analytic clause so that the partition is calculate percentage contribution of each group based on certain condition SQL. SQL Calculate Percentage in Group By. 0. but specific to an individual?

**How to Calculate a Cumulative Percentage in SQL – Java, SQL and ,** A fun report to write is to calculate a cumulative percentage. We're interested in the cumulative revenue per date, so let's run a classic GROUP BY : But in some more complex cases, it might help to shorten your SQL syntax. And the trick is that in the absence of a frame clause but presence of an order The GROUP BY clause is used with the SQL SELECT statement. The grouping can happen after retrieves the rows from a table. When some rows are retrieved from a grouped result against some condition, that is possible with HAVING clause. The GROUP BY clause is used with the SELECT statement to make a group of rows based on the values of a specific

**SQL Calculate Percent on Total / of Column,** Calculate percent sql - SQL Server calculate percentage - sql convert percent GROUP BY YEAR(OrderDate). ORDER T-SQL applying OVER() to get summary total and percent on base How to calculate percentage with a SQL statement. Need separate columns that calculate percentage. as Percentage from Table1 group by name Sample SQL Fiddle contributions licensed under cc by-sa 4.0

**SQL GROUP BY clause,** The WHERE clause is used to retrieve rows based on a certain condition, but it can not be applied to grouped result. In an SQL statement, Is it possible to calculate the percentage of some category for each group, using proc sql? the percentage of some category for each group, using proc sql

##### Comments

- The count(*) does the number of order rather by the number of quantity right? I'm thinking more of 10 / (10+49+27) = 12%
- @AndyWang: Ok, fixed my answer