I have got the results in the following format
customer ; Sales
A ; 1000
B ; 1500
c ; 2500
I want to add a grand total row in the results like
customer ; Sales
A ; 1000
B ; 1500
c ; 2500
Grand Total ; 5000
how can I do this?
I have tried roll up function
Select customer, sales from `xyz`
group by customer
expecting grand total in the output
To add a new row to the query you had, you can use the WITH statement, and use a query like the following:
WITH grand_total AS (SELECT SUM(sales) as grand_total FROM TABLE_NAME)
SELECT customer, sales FROM TABLE_NAME
UNION DISTINCT
SELECT 'grand_total' as customer, grand_total.grand_total from grand_total;
which will output the following:
Row customer sales
1 B 1500
2 C 2500
3 A 1000
4 grand_total 5000
You can read more information about the WITH statement in the public documentation.
Related
suppose i have a product is and sales column
product id sales
1 1000
2 10000
3 50000
4 12000
5 8000
write an sql query to get all product ids that contribute to top 80 % of sales?
For this, you want a cumulative sum. Presumably, you want the top selling such products, so:
select p.*
from (select p.*,
sum(sales) over (order by sales desc) as running_sales,
sum(sales) over () as total_sales,
from products
) p
where running_sales - sales < 0.8 * total_sales;
This returns the product that reaches or first exceeds 80% of the total sales.
I have two tables an tblOrder and tblOrderDetail Table.
tblOrderDetail contain below rows:
OrderDetailID OrderID Product Quantity UnitPrice Discount Total
1 1 ABC 10 $240.00 10 $2,160.00
2 2 CDF 100 $200.00 10 $18,000.00
3 3 GHI 200 $150.00 0 $30,000.00
4 1 XYZ 40 $100.00 5 $3,800.00
i want sql query to get Subtotal column in tblOrder, which are sum of relate OrderID Total from tblOrderDetail like this:
OrderID Sub Total
1 $5,960.00
2 $18,000.00
3 $30,000.00
i try this sql query:
SELECT
OrderID
,(
SELECT
SUM(((tblOrderDetail.UnitPrice) - (tblOrderDetail.UnitPrice * (tblOrderDetail.Discount / 100))) * (tblOrderDetail.Quantity))
FROM
tblOrderDetail
WHERE tblOrderDetail.OrderID = tblOrder.OrderID
) AS [Sub Total]
FROM
tblOrder
but it gives this
OrderID Sub Total
1 $0.00
2 $0.00
3 $0.00
Note i want Sub Total column dynamically not by Sum of Total Column in tblOrderDetail Table.
I hope somebody can make sense of what I'm saying and hopefully help me achieve this!
Use Group by Clause to with SUM aggregate function,
Select *, sum(Total) as totalforOrder from ordertbl group by OrderID;
Demo SQLFiddle
as per your question, here is a correlated query
SELECT
tblOrder.OrderID
,(
SELECT
SUM(((tblOrderDetail.UnitPrice) - (tblOrderDetail.UnitPrice * (tblOrderDetail.Discount / 100))) * (tblOrderDetail.Quantity))
FROM
tblOrderDetail
WHERE tblOrderDetail.OrderID = tblOrder.OrderID
) AS SubTotal
FROM
tblOrder;
Its working fine on SQLFiddle
you can use Group by to do this
Select OrderID, sum(Total) as [Sub Total] from tblOrderDetail group by OrderID;
You can get more details here !
I have a sales table sale with following columns
sale_id | Department | gross_amount
I need to find the avg of sales in each department from the total sale
eg:-
Table
sale_id Department gross_amount
1 A 10
2 B 30
3 A 25
4 c 5
Desired output
Department Gross_amount avg
A 35 50 --(35/70)*100
B 30 42.86 --(30/70)*100
C 5 7.14 --(5/70) *100
ie is dept_avg = (dept_total / total) *100
eg:- dept_total of A = 35
total = A+B+C = 35+30+5 =70
I am able to find up to Gross_amount
select Department ,sum(si.GrossPrice) gross_amt
from Sale si
group by Department
order by Department
For getting avg I tried follow
select Department ,sum(si.GrossPrice) gross_amt,
AVG(sum(si.GrossPrice)) avg
from Sale si
group by Department
order by Department
It is giving me an error
Cannot perform an aggregate function on an expression containing an
aggregate or a subquery.
Also I am not sure i can get my expected avergage with the above query. How can I achieve the same.
Divide grouped sum by total sum:
SqlFiddleDemo
SELECT Department ,
[Gross_amount] = SUM(gross_amount),
[avg] = ROUND(CAST(SUM(gross_amount) AS DECIMAL(10,2))/
CAST((SELECT SUM(gross_amount) FROM tab) AS DECIMAL(10,2)) * 100
,2)
FROM tab
GROUP BY Department
ORDER BY Department
If you want the proportion of the total for each department out of the overall total, I would suggest using window functions:
SELECT Department, SUM(gross_amount) as gross_amount
SUM(gross_amount)*100.0 / SUM(SUM(gross_amount)) over () as propotion
FROM tab
GROUP BY Department
ORDER BY Department;
I have a table, sales, which is ordered by descending TotalSales
user_id | TotalSales
----------------------
4 10
2 1.5
5 0.99
3 0.5
1 0.33
What I would like to do is find the percentage of the sum of all sales that the xx% most important sales represent.
For example if I wanted to do it for top 40% sales, here I would get (10+1.5)/(10+1.5+0.99+0.5+0.33)= 86%
But right now I haven't been able to select "top xx% rows".
Edit: DB management system can be MySQL or Vertica or Hive
select Sum(a) as s from sales where a in (Select TotalSales from sales where TotalSales>=x)
GROUP BY a
select Sum(TotalSales) as b from sales group by b
your result is s/b
and x= the percentage you set each time
I have one table with following data..
saleId amount date
-------------------------
1 2000 10/10/2012
2 3000 12/10/2012
3 2000 11/12/2012
2 3000 12/10/2012
1 4000 11/10/2012
4 6000 10/10/2012
From my table I want result with max of sum amount between dates 10/10/2012 and 12/10/2012 which for the data above will be:
saleId amount
---------------
1 6000
2 6000
4 6000
Here 6000 is the max of the sums (by saleId) so I want ids 1, 2 and 4.
You have to use Sub-queries like this:
SELECT saleId , SUM(amount) AS Amount
FROM Table1
GROUP BY saleId
HAVING SUM(amount) =
(
SELECT MAX(AMOUNT) FROM
(
SELECT SUM(amount) AS AMOUNT FROM Table1
WHERE date BETWEEN '10/10/2012' AND '12/10/2012'
GROUP BY saleId
) AS A
)
See this SQLFiddle
This query goes through the table only once and is fairly optimised.
select top(1) with ties saleid, amount
from (
select saleid, sum(amount) amount
from tbl
where date between '20121010' and '20121210'
group by saleid
) x
order by amount desc;
You can produce the SUM with the WHERE clause as a derived table, then SELECT TOP(1) in the query using WITH TIES to show all the ones with the same (MAX) amount.
When presenting dates to SQL Server, try to always use the format YYYYMMDD for robustness.