How to SUM minus INT Currency - mysql

I have a transaction table with positive and negative numbers and a budget table. A negative number will increase if being minus with budget.
bud_tab
id
budget
1
90
2
80
3
50
4
30
trans_tab
id
trans
1
-50
2
80
3
-70
4
60
What query should I use to get an output like this:
budget
trans
total
90
-50
140
80
80
0
50
-70
120
30
60
-30

From the look of output, this seems like a basic math calculations. Am I missing something ?
select budget, trans, budget - trans as total from bud_tab natural join trans_tab ;

Related

cumulative sum till target reached in mysql

I have following tables,
Workout Data:
Date User Distance Calories
1614944833 1 100 32
1614944232 2 100 43
1624944831 1 150 23
1615944832 3 250 63
1614644836 1 500 234
1614954835 2 100 55
1614344834 3 100 34
1614964831 1 260 23
1614944238 1 200 44
user_subdomain Data:
User sub_domain
1 3
2 3
3 3
4 2
Subdomain data:
subdomain name
3 test1
4 test2
I would like to get sum value of distance,calories,count of records once they user reached sum of distance >= 1000.we should not count remaining records if user crossed 1000 distance.( if user crossed 1000,then 1000 else max distance value).
Expected Output:
Date record_count Distance Calories
1614964831 4 1000 312
1614954835 2 200 98
1614344834 3 350 97
So This result shows each users total effort they used to reach distance 1000 by record_count,then if they reached 1000 above then calculated as 1000,else max reached distance value,then total sum of that calories till 1000 cumulative sum reached.This is the output i need to retrieve.I tried with below query,but not works
Can anyone suggest with cumulative sum inner join method or any other solution for this?
Since MySQL 8.0 you can use window functions in next way:
with cumulative as (
-- calculate cumulative Distance & Calories
select
User,
Distance,
Calories,
sum(Distance) over (partition by User order by Date) SumDistance,
sum(Calories) over (partition by User order by Date) SumCalories
from Workout
order by User, Date
) select
User, max(SumDistance), max(SumCalories)
from cumulative
where SumDistance - Distance < 1000 -- filter
group by User;
MySQL window functions

MySQL Query SUM Total Based on Column Value

I have table like this
Order D.O Cost Maintenance Total
ORD-0005 1 100 50 150
ORD-0005 2 50 120 170
ORD-0006 3 200 100 300
ORD-0006 4 150 50 200
Now I want to have total SUM based on Column 'ORDER'
So the result will look like this
Order D.O Cost Maintenance Total
ORD-0005 1 100 50 320
ORD-0005 2 50 120 320
ORD-0006 3 200 100 500
ORD-0006 4 150 50 500
The total value is sum from all cost+maintenance refer to Order Column
Thank you..really appreciated it
You could use a sub-query that calculates the SUM and then join this to the output:
SELECT a.`Order`,
a.`D.O`,
a.`Cost`,
a.`Maintenance`,
b.`Total`
FROM `myTable` a
INNER JOIN (
SELECT `Order`,
SUM(`Total`) AS `Total`
FROM `myTable`
GROUP BY `Order`
) b ON a.`Order` = b.`Order`
ORDER BY a.`Order`
This will calculate the total value for each order in the sub-query, and then include that total value in the output for each Order.
Output:
Order
D.O
Cost
Maintenance
Total
ORD-0005
1
100
50
320
ORD-0005
2
50
120
320
ORD-0006
3
200
100
500
ORD-0006
4
150
50
500
Seen here in this working fiddle.

How to find the smallest value in a row

i need help to create a sql query that can find the smallest value in 1 row , and display it in the last column, like this table.
id
out
mid
in
Smallest
1
200
100
50
50
2
100
150
50
50
3
200
100
250
100
4
50
100
150
50
5
50
100
100
50
6
20
200
100
20
7
-
-
100
100
8
150
-
100
100
this is my query :
On MySQL you may use the scalar LEAST() function:
SELECT id, `out`, mid, `in`, LEAST(`out`, mid, `in`) AS Smallest
FROM yourTable;
If your database doesn't have a LEAST function, we can use a CASE expression as an alternative:
SELECT id, `out`, mid, `in`,
CASE WHEN `out` < mid AND `out` < `in` THEN `out`
WHEN mid < `in` THEN mid
ELSE `in` END AS Smallest
FROM yourTable;
Side note: Both IN and OUT are reserved MySQL keywords, and you should avoid naming your columns with them.

MySQL cumulative sum order by date

I am executing a cumulative sum order by date and I do not get the expected result. Some records are presented in a different order compared to the order used for the sum.
Please have a look at SQL Fiddle
I would expect either the following result:
2015-05-05T00:00:00Z 50 30 20 90 120
2015-05-05T00:00:00Z 60 30 30 120 100
2015-05-04T00:00:00Z 70 50 20 30 70
2015-05-04T00:00:00Z 80 40 40 70 50
2015-05-03T00:00:00Z 30 20 10 10
or the following order:
2015-05-05T00:00:00Z 60 30 30 120
2015-05-05T00:00:00Z 50 30 20 90
2015-05-05T00:00:00Z 60 30 30 120
2015-05-04T00:00:00Z 80 40 40 70
2015-05-04T00:00:00Z 70 50 20 30
2015-05-04T00:00:00Z 80 40 40 70
2015-05-03T00:00:00Z 30 20 10 10
(Added) please note that negative values are also possible. This is the reason why I have mentioned on answers below that an order on the cumulative sum would not solve the problem. As an example I will modify slightly the result:
2015-05-05T00:00:00Z 30 60 -30 60
2015-05-05T00:00:00Z 50 30 20 90
2015-05-04T00:00:00Z 80 40 40 70
2015-05-04T00:00:00Z 70 50 20 30
2015-05-03T00:00:00Z 30 20 10 10
Thanks for the help.
http://sqlfiddle.com/#!9/7204d4/2
gives your expceted output.. I have added #cum := #cum + tot_earn_pts - tot_redeem_pts asc after your query.
Add extra fields in order by "cum_liability_pts" desc:
SQL Fiddle
SELECT *
FROM (
SELECT date,
tot_earn_pts,
tot_redeem_pts,
tot_earn_pts - tot_redeem_pts AS tot_liability_pts,
#cum := #cum + tot_earn_pts - tot_redeem_pts AS cum_liability_pts
FROM (
SELECT date,
earn_points AS tot_earn_pts,
redeem_points AS tot_redeem_pts
FROM i_report_total_order
/* WHERE website_id = 36 */
) tots
JOIN (SELECT #cum := 0) init
ORDER BY date asc
) res_asc
ORDER BY date desc, cum_liability_pts desc;

Find repeated battles

I have one table with the following fields:
battle_id, winner, looser
1 200 44
2 55 366
3 44 200
4 123 200
5 200 44
6 55 366
7 177 205
8 188 211
9 366 55
10 55 366
right now it has about 1300 records (its small), and there are about 400 players, in each battle there can only be a winner and a looser (there are no draws)
how can i find all the repeated battles? i do not want to find all the repeated battles of one player, i do want to know all the repeated battles of all the players...i know that i cam make a recursive function in php that iterates over all the battles and assign them to a matrix, but just for fun...is there a way to do it only on mysql?
And how can i optimize the table to find the repeated battles more quickly?
regards
EDIT:
For example i want the query to show:
battle_id, winner, looser
1 200 44
2 55 366
3 44 200
5 200 44
6 55 366
9 366 55
10 55 366
This should work, using a self-join could result in many duplicated entries
SQLFIDDLE
SELECT
t1.battle_id,
t1.winner,
t1.loser
FROM
your_table t1
WHERE
EXISTS (
SELECT
1
FROM
your_table t2
WHERE
( ( t1.winner = t2.winner
AND t1.loser = t2.loser )
OR ( t1.loser = t2.winner
AND t1.winner = t2.loser ) )
AND t1.battle_id <> t2.battle_id
)
try this:
SELECT b1.battle_id,
b1.winner,
b1.looser
FROM battles as b1
group by b1.battle_id, b1.winner, b1.looser
having count(*)>=2