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.
Related
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.
I have a table as follows:
ID PID TID CID DID IsTrue
1 43 1 2 621 0
2 43 1 2 621 1
3 45 2 3 621 1
4 46 2 3 621 0
and I need to find the count of PID,CID and with the where condition DID=621 along with the other fields.
how can I do that?
Thanks.
Try this
Select count(pid) from table where did = 121 group by pid.
I have a table that holds timestamped messages from a sender to a receiver, and it looks like:
mes timestamp sender receiver
10 2014-04-13 12:22:25.000 1 72
10 2014-04-13 12:22:25.000 1 91
10 2014-04-13 12:22:25.000 1 58
16 2014-02-20 20:09:06.000 3 35
16 2014-02-20 20:09:06.000 3 54
17 2014-03-05 14:55:28.000 1 65
18 2014-03-07 14:55:28.000 2 97
19 2014-03-09 14:55:28.000 2 97
My table holds 3 millions rows like these, and I am trying to group results according to timestamp intervals, counting the number of messages in each month between each pair of sender-receiver. Something like:
timestamp sender receiver count
2014-04 1 72 1
2014-04 1 91 1
2014-04 1 58 1
2014-02 3 35 1
2014-02 3 54 1
2014-03 1 65 1
2014-03 2 97 2
I really have no clue how to specify my clause in mysql, so I apologize for not providing a snippet of my not-working code... should I use something as a switch control and manually specify the time interval? or is there something more specific in mysql to manage tasks like this?
This is fairly straight-forward GROUP BY with multiple levels:
SELECT CONCAT(YEAR(tstamp),'-',MONTH(tstamp)) as tstamp, sender, receiver, COUNT(*) AS cnt
FROM yourtable
GROUP BY YEAR(tstamp), MONTH(tstamp),sender,receiver;
I'm using tstamp as field name for "timestamp" to avoid conflicting with reserved words (timestamp).
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;
Trying to sort the following TEAM_TOTAL Column Desc
MATCHID TEAM_TOTAL
---------- -----------------
573 Total 112
573 Total 2 for 115
574 Total 9 for 97
574 Total 2 for 100
575 Total 9 for 129
575 Total 9 for 101
576 Total 4 for 191
576 Total 9 for 160
577 Total 8 for 157
577 Total 7 for 137
578 Total 6 for 193
578 Total 119
But the problem is TEAM_TOTAL is varchar, is there a way with query alone I can get the results in the sorted desc order.
More over there is a text as well in that column. I am running out of ideas to get this up
Result should have beeen like the below Result Set
MATCHID TEAM_TOTAL
---------- -----------------
578 Total 6 for 193
576 Total 4 for 191
576 Total 9 for 160
577 Total 8 for 157
577 Total 7 for 137
575 Total 9 for 129
578 Total 119
573 Total 2 for 115
573 Total 112
575 Total 9 for 101
574 Total 2 for 100
574 Total 9 for 97
Give this a try:
select * from t
order by substring(
team_total, locate('for', team_total) +
if(locate('for', team_total) > 0, 4, 7))+0 desc
Fiddle here.
Try to extract the integer (string after the last space):
-- 'Total 112' - extracts 112
SELECT SUBSTRING('Total 112', LENGTH('Total 112') - LOCATE(' ', REVERSE('Total 112')));
-- 'Total 6 for 193' - extracts 193
SELECT SUBSTRING('Total 6 for 193', LENGTH('Total 6 for 193') - LOCATE(' ', REVERSE('Total 6 for 193')));
Now, you can convert that string to a number and then order by it.
SELECT * FROM teams
ORDER BY
CAST(SUBSTRING(TEAM_TOTAL, LENGTH(TEAM_TOTAL) - LOCATE(' ', REVERSE(TEAM_TOTAL))) AS INT) DESC