I've got this table:
id | payment_id | quantity | unit cost
1 633 1 750
2 633 1 750
3 632 2 750
4 632 2 750
What I need is :
payemnt_id | total
633 1500
632 3000
You can also think of this as Countries and then you are trying to find the total for each Country. I was sure there were some tutorials like this but could not find by STFW.
You can simply put the formula (expression) inside SUM:
SELECT payment_id, SUM(`unit cost` * quantity) AS total
FROM myTable
GROUP BY payment_id
SELECT
payment_id,
SUM(subtotal) AS total
FROM(
SELECT
payment_id,
(unit_cost * quantity) AS subtotal
) AS t1
GROUP BY
payment_id
SELECT payemnt_id, SUM(`unit cost` * quantity) as total
FROM Table1
GROUP BY payemnt_id
SELECT COALESCE (SUM(cost_field * quantity_field),0) AS tot FROM Table_Name
I Think This One Is Good, Why ?
Because If Their is No Result It Will Give You Zero
Related
I have a table with the following structure and sample data:
STORE_ID | INS_TIME | TOTAL_AMOUNT
2 07:46:01 20
3 19:20:05 100
4 12:40:21 87
5 09:05:08 5
6 11:30:00 12
6 14:22:07 100
I need to get the hourly sum of TOTAL_AMOUNT for each STORE_ID.
I tried the following query but i don't know if it's correct.
SELECT STORE_ID, SUM(TOTAL_AMOUNT) , HOUR(INS_TIME) as HOUR FROM VENDAS201302
WHERE MINUTE(INS_TIME) <=59
GROUP BY HOUR,STORE_ID
ORDER BY INS_TIME;
Not sure why you are not considering different days here. You could get the hourly sum using Datepart() function as below in Sql-Server:
DEMO
SELECT STORE_ID, SUM(TOTAL_AMOUNT) HOURLY_SUM
FROM t1
GROUP BY STORE_ID, datepart(hour,convert(datetime,INS_TIME))
ORDER BY STORE_ID
SELECT STORE_ID,
HOUR(INS_TIME) as HOUR_OF_TIME,
SUM(TOTAL_AMOUNT) as AMOUNT_SUM
FROM VENDAS201302
GROUP BY STORE_ID, HOUR_OF_TIME
ORDER BY INS_TIME;
I have a table that has the following fields
SalesOrderID RelatedOrderID Amount
1 0 5.00
2 1 1.00
3 0 3.00
4 0 20.00
5 4 10.00
I'm looking to write a query that will return the sales order total made up of the original total and the RelatedOrderID total.
SalesOrderID NewAmount
1 6.00
4 30.00
Hope that makes sense...please ask any questions. I'm aware it's a confusing situation!
Thanks,
Mike
You could use union all and then aggregate::
select SalesOrderID, sum(amount) as amount
from ((select SalesOrderID, amount from t) union all
(select RelatedSalesOrderID, amount from t where RelatedSalesOrderID > 0)
) tt
group by SalesOrderID;
For that effect you will have to use something like this
SELECT
least(o.SalesOrderId,r.SalesOrderId) AS SalesOrderId,
o.Amount + r.Amount AS Amount
FROM
orders o
JOIN orders r ON o.SalesOrderId = r.RelatedOrderID;
Thanks both, I've actually worked it out since posting this:
SELECT so1.SalesOrderID,
cast(SUM(so1.Amount+so2.Amount) as money) as NewAmount
FROM SalesOrder so1
JOIN SalesOrder so2
ON so1.SalesOrderID = so2.RelatedOrderID
GROUP BY so1.sorder_code
My data looks like this:
UserID Hours BillRate
1 1.50 2.25
1 2.50 3.25
1 3.50 3.25
2 5.50 4.25
2 6.50 5.25
2 7.50 5.25
In detail page, I have this query to get total spend for each UserID
SELECT UserID, ROUND(SUM(Hours*BillRate), 2) AS TotalSpend
FROM mytable
GROUP BY UserID
The result is 22.88 for UserID_1 and 96.88 for UserID_2 (the total is 119.76)
In summary page, I have to run a query with 2 SELECT statements to get the right total:
SELECT SUM(TotalSpend)
FROM (
SELECT UserID, ROUND(SUM(Hours*BillRate), 2) AS TotalSpend
FROM mytable
WHERE UserID IN (1, 2)
GROUP BY UserID
) a
Is there anyway that i can get the total in summary page with one SELECT statement?
SELECT ROUND(SUM(Hours*BillRate), 2) AS TotalSpend
FROM mytable
WHERE UserID IN (1, 2)
My data looks like this:
UserID Hours BillRate
1 1.50 2.25
1 2.50 3.25
1 3.50 3.25
2 5.50 4.25
2 6.50 5.25
2 7.50 5.25
Is there anyway that I can get total spend for each person in one SELECT statement?
This is what I have. It returns what I want, but I have to do 2 SELECT statements.
SELECT UserID, SUM(OneSpend) AS TotalSpend
FROM (
SELECT UserID, ROUND(SUM(Hours)*BillRate,2) AS OneSpend
FROM mytable
GROUP BY UserID, BillRate
) a
GROUP BY UserID
SELECT UserID, ROUND(SUM(Hours*BillRate), 2) AS TotalSpend
FROM mytable
GROUP BY UserID
According to the distributive property of multiplication over addition, SUM(Hours)*BillRate is the same as SUM(Hours * BillRate) when all the BillRate values are the same, as they are when you group by BillRate.
How to sum duplicated values in a Group*(SSRS 2005).*
eg.
My query returns values like the following:
CusID Discount Amount
1 20 1000
1 20 2000
1 5 700
2 15 1500
2 15 3000
But,when I sum Discount amount in Group Footer, I cannot get the total values like below. I get 45 for CusID 1 instead of 25. Please help me to solve this problem.Thanks.
CusID Discount Amount
1 20 1000
1 20 2000
1 5 700
------------------------
Total 25 3700
2 15 1500
2 15 3000
------------------------
Total 15 4500
well without what your actual data looks like, i can only provide you a code example based off of the data you provided.
declare #table table (CustID int, Discount int, Amount int)
insert into #table (CustID,Discount,Amount)
select 1 as CusID,20 as Discount,1000 as Amount
union all
select 1,20,2000
union all
select 1,5,700
union all
select 2,15,1500
union all
select 2,15,3000
select
CustID,
sum(Discount) as Discount,
sum(Amount) as Amount
from
(
select
CustID,
Discount,
SUM(Amount) as Amount
from #table
group by CustID, Discount
) a
group by CustID
One slight simplification to DForcek42's answer by using the sum(distinct x)
declare #table table (CustID int, Discount int, Amount int)
insert into #table (CustID,Discount,Amount)
select 1 as CusID,20 as Discount,1000 as Amount
union all
select 1,20,2000
union all
select 1,5,700
union all
select 2,15,1500
union all
select 2,15,3000
select
CustID,
sum(distinct Discount),
SUM(Amount) as Amount
from #table
group by CustID