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)
Related
I have my data base like this
id project_id client_id price
1 1 1 200
2 2 1 123
3 2 1 100
4 1 1 87
5 1 1 143
6 1 1 100
7 3 3 123
8 3 3 99
9 4 3 86
10 4 3 43
11 4 3 145
12 4 3 155
Now here I want that it will sum the price columns with the same client_id.
For that I just made my query like this
Select `project_id`, SUM(`price`) FROM `table-name` GROUP BY `client_id`
This one is doing sum the price but I am getting only two project_id in the result. I want the result should be all the distinct project for the client id and the price will be summed for the group clients.
So can someone tell me how to do this? Any help and suggestions will be really appreciable. Thanks
You should not have "bare" column in a group by query that are not in the group by statement.
If you want the list of projects, you can get them in a list like this:
SELECT client_id, GROUP_CONCAT(project_id), SUM(price)
FROM table-name
GROUP BY client_id;
you only have two client that why you are getting only two record , you can group by two column,
Select `project_id`, SUM(`price`) FROM `table-name` GROUP BY `client_id`, `project_id`
Consider the following table:
id, invoice_number, item_number, qty, price
1 5000 200 1 4.50
2 5000 201 2 5.50
3 5000 201 1 5.75
2 5000 202 1 5.50
3 5000 202 1 5.75
4 5000 203 1 6.00
How can I get the following result:
invoice, item, count
5000, 200, 1
5000, 201, 2
5000, 202, 2
5000, 203, 1
I know I can group by the invoice number, but then I cannot simply count the invoices, or I will lose their name. Additionally I would love to know if they have a different price, even if it was just a flag, however I am thinking to get that level of detail I might as well put this in a script and loop through the items, its just that my data set is very large, and id love to do it with pure SQL :)
Thanks in advance!
To get your exact desired output:
select invoice_number, item_number, count(*)
from tbl
group by invoice_number, item_number
To get your desired output along with the number of unique prices within the group, and what those prices are:
select invoice_number,
item_number,
count(*) as num_rows,
count(distinct price) as num_prices,
group_concat(distinct price) as prices
from tbl
group by invoice_number, item_number
I have to do some reporting, involving various tables, and having couple of SUMs, COUNTs, etc and everything is OK. But the last thing I have to resolve is SUM by another which is not in the grouped columns.
I'll give you an example (stripped down from what I have) so you can understand the tongue-twister in the previous paragraph.
Suppose I have a query with a couple of joins that get me this result, or a temporary table, or whatever:
(this is a trimmed down version, in the original I have much more columns and groupbys)
APP_ID CAT_ID CAT_DESCRIP APP_START APP_END DETAIL_ID DET_QTY DETAIL_PRICE
1 1 Categ One 900 960 1 10 150.00
1 1 Categ One 900 960 2 8 20.00
1 1 Categ One 900 960 3 12 30.00
1 1 Categ One 900 960 4 5 100.00
2 2 Categ Two 600 720 5 12 150.00
2 2 Categ Two 600 720 6 10 50.00
3 2 Categ Two 1200 1260 7 5 20.00
I need to get something like this: (the bolded column is the important)
SELECT
CAT_ID,
CAT_DESCRIP,
SUM(DET_QTY) as TotalQTY,
SUM(DETAIL_PRICE) as TotalPrice,
COUNT(DISTINCT APP_ID) as CountOfApps,
(GET THE SUM OF (APP_END - APP_START) ONLY ONE TIME BY APP_ID INTO THIS CATEG) as TimeInMinutesByCategory
FROM
MyTable
GROUP BY
CAT_ID
And the result has to give me this:
CAT_ID CAT_DESCRIP TotalQTY TotalPrice CountOfApps TimeInMinutesByCategory
1 Categ One 35 300.00 1 60
2 Categ Two 27 220.00 2 180
Thanks for your help!
I think this will do the job... or if not, a little tweaking on the sytnax for max(app_start) - max(app_end) should do the job
The idea is, summarize the data in a subquery by app_id and cat_id. Select the max value of start and end, grouped by app_id and cat_id. Since there will only be one value per each distinct pair of app_id and cat_id, we're essentially just deduping.
Then, join the subquery to the main query and summarize by category id.
SELECT
a.CAT_ID,
a.CAT_DESCRIP,
SUM(a.DET_QTY) as TotalQTY,
SUM(a.DETAIL_PRICE) as TotalPrice,
COUNT(DISTINCT a.APP_ID) as CountOfApps,
SUM(b.TimeInMinutesByCategory) AS TimeInMinutesByCategory
FROM
MyTable AS a
INNER JOIN (
SELECT APP_ID, CAT_ID, max(app_start) - max(app_end) AS TimeInMinutesByCategory
FROM MyTable
GROUP BY APP_ID, CAT_ID) AS b
ON a.cat_id = b.cat_id
AND a.app_id = b.app_id
GROUP BY
a.CAT_ID
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.
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