GROUP BY using a SUM - mysql

This is a tough one to explain. I have this data set
Data set 1
counter | id
1 280
1 280
0 280
1 781
1 781
1 781
0 25
1 25
1 25
I want to GROUP BY the id, but with the SUM of counter so I end up with this data set
Data set 2
counted | id
2 280
3 781
2 25
I have tried a number of approaches but I always end up with the total SUM of rows for each ID like this
Data set 3
counted | id
3 280
3 781
3 25
EDIT
It might be worth noting that data set 1 comes from a sub query and is not a table in itself.
EDIT
I have used a query like this, it gives me the result of data set 3
select sum(counter) as counted, id
from t
group by id;

Did you do this?
select sum(counter) as counted, id
from t
group by id;

Related

Update the mysql field according to the sorting

a simple task, but I can't figure it out. There is a Table:
id
name
sum
place
1
Alex
210
0
2
Bob
250
0
3
Sam
190
0
4
Bill
290
0
5
Jack
210
0
I need to UPDATE the PLACE
according to the maximum SUM and then the id
Those are the request :
SELECT id, name, sum, place FROM tableORDER BYsumDESC,id ASC;
According to this request, update the semi PLACE from 1++
Those places 1, 2, 3 and further throughout the table
Like this:
id
name
sum
place
1
Alex
210
3
2
Bob
250
2
3
Sam
190
5
4
Bill
290
1
5
Jack
210
4
SET #n_place := 0;
UPDATE `table` SET `place` = #n_place := #n_place + 1 ORDER BY `sum` DESC, `id` ASC;

MySQL query using Update, Sum, Where, and Group By

I have a table below named deposit
dep_id
deposit_amount
comp_id
1
100
1
2
100
1
3
300
2
4
200
2
5
100
2
6
500
3
When I update the table with the query below I get the following table, which is not what I want
UPDATE deposit
SET deposit_amount = (SELECT SUM(deposit_amount) - 50)
WHERE comp_id =1
What the query above does is to subtract 50 from each of the corresponding comp_id
dep_id
deposit_amount
comp_id
1
50
1
2
50
1
3
300
2
4
200
2
5
100
2
6
509
3
But the table below is what I need.
Because seeing the first table and with the query I provided where comp_id =1, we have 100 + 100 = 200, and then 200 - 50 = 150. So because comp_id has 1 IDs two times, therefore we have 75 and 75 because 75 +75 is 150. So we have the table below, which is what I need.
dep_id
deposit_amount
comp_id
1
75
1
2
75
1
3
300
2
4
200
2
5
100
2
6
500
3
The amount supposed to be evenly split amongst the deposits that share a comp_id, even if they weren't before.
Please how do I write the query to suit the table I need? Help!
You can divide the 50 by the count of the records for this id:
UPDATE deposit d1,
(SELECT *, count(*) over (partition by d2.comp_id) as c FROM deposit d2) x
SET d1.deposit_amount = (SELECT SUM(d1.deposit_amount) - 50/x.c)
WHERE d1.comp_id =1
see: DBFIDDLE

SQL sum up records with similar id

I have this table and I require to sum the payments and balance of the same id.
ID BALANCE PAYMENT DATE
1 157 3 1/3/2021
1 157 4 3/3/2021
1 157 7 4/3/2021
1 157 8 9/3/2021
2 304 9 21/2/2021
3 208 3 18/5/2021
I need to get to this
ID BALANCE PAYMENT TOTAL
1 157 3 179 *(157+3+4+7+8)*
1 157 4 179
1 157 7 179
1 157 8 179
2 304 9 313
3 208 3 211
And remove duplicates and eventually hide the payment column with a nested query to reduce confusion
ID BALANCE PAYMENT TOTAL
1 157 3 179
2 304 9 313
3 208 3 211
I tried using the 'select id, balance + payment from table' however that does not take in the id, and group by does not seem to work as well.
Any help is much appreciated!
I guess the tricky bit is getting 1 balance
SELECT ID, MAX(BALANCE) BALANCE,MAX(BALANCE) + SUM(PAYMENT) TOTAL
FROM T
GROUP BY ID
Try this:
SELECT id,balance,payment,sum(payment)+balance as TOTAL FROM TABLE_NAME group by id;
I tried a similar query here: https://www.programiz.com/sql/online-compiler/
SELECT customer_id,first_name,age,sum(age)+customer_id as total FROM Customers group by first_name;
Maybe you'll need to specify all columns in group by but this is a concept that you can use for this scenario.

How to add(sum) the column data in SQL

ID pcID contractor approver claimed
-------------------------------------------
1 1 one 1000 900
2 1 two 200 100
3 1 three 1000 1000
4 1 six 100 11
5 2 six 100 22
6 3 six 120 1
7 4 three 102 10
From the above table I need to sum the approver amount and claimed amount based on the contractor. Like the below given table. All has been done by using the stored procedure.
ID pcID contractor approver claimed TotalApprover TotalClaimed
--------------------------------------------------------------------------
1 1 one 1000 900 1000 900
2 1 two 200 100 200 100
3 1 three 1000 1000 1000 1000
4 1 six 100 11 100 11
5 2 six 100 22 200 33
6 3 six 120 1 320 34
7 4 three 102 10 1120 1001
Like the above table I need an output adding(sum) in ascending order based on contractor.
Thanks in advance.
You can use window function
select t.*,
sum(approver) over( partition by contractor order by ID) TotalApprover,
sum(claimed) over( partition by contractor order by ID) TotalClaimed,
from table1 t
You want cumulative sums:
select t.*,
sum(approver) over (partition by contractor order by claimid) as totalapprover,
sum(claim) over (partition by contractor order by claimid) as totalclaim
from t;
This is just accumulated the corresponding values by the dimensions you are asking for.

Need MySQL query to sum and avrage

I have duplicate rows in table, i want sum of quantity of duplicate row and average of purchase rate of duplicate rows, let me explain you by example.
Please give me mysql query to acheive desired output.
Problem Table
===================================================
POID itemid quantity purchaserate
1 1 100 100
2 2 100 100
3 3 100 100
4 1 80 200
5 1 40 150
6 3 100 400
====================================================
Desired output
===================================================
itemid totalquantity avgpurchaserate
1 220 145.45
2 100 100
3 200 250
===================================================
Tried below query
select itemid, sum(quantity), avg(purchaserate)
from test
group by itemid
Output
itemid |sum(quantity) | avg(purchaserate)
1 | 220 | 150
2 | 100 | 100
3 | 200 | 250
You can use aggregation function as sum and avg and group by the column you need. In your case itemid
select itemid, sum(quantity) avg(purchaserate)
from my_table
group by itemid