MySQL Select records exceeding the cumulative total - mysql

Given I have following table
Id
FileSizeMB
1
100
2
100
3
100
4
100
5
100
6
100
I want to select oldest records exceeding a cumulative value, in this case say 500.
So something like this
Id
Cumulative_FileSizeMB
6
100
5
200
4
300
3
400
2
500
1
600
I want to select only records with id 2 and 1 as they are >= 500.
Goal is to delete them.
Thanks

For anyone with same problem.
I have reached this solution using Mysql window functions,
and also there is no need to declare a variable for cumulative total
SELECT * from (
SELECT
id,
FileSizeMB,
SUM(FileSizeMB) OVER (ORDER BY id DESC) AS TotalFileSizeMB
FROM table
) as t1
WHERE TotalFileSizeMB > 500

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 to sum rows and columns based on column criteria

Please help me with writing a query for the following condition. I have a table which I have listed below
ID Wt1 Wt1_Type Wt2 Wt2_Type Wt3 Wt3_Type Wt4 Wt4_Type
--------------------------------------------------------------
1 200 1 220 1 300 2 400 3
2 100 4 150 3 100 5 120 1
3 100 3 110 1 200 5 100 4
I want a query to sum all the the weights (wt1, wt2, wt3, wt4) grouped on the weight type (wt1_type, wt2_type, wt3_type, wt4_type).
The output should look like
Wt_type Total
1 650
2 300
3 650
4 200
5 300
Can someone please help me draft a mysql query to get this result ?
Thanks
You can try below - using union all and subquery
select Wt_Type,sum(Wt) as total from
(
select Wt1_Type as Wt_Type,Wt1 as Wt from tablename
union all
select Wt2_Type ,Wt2 from tablename
union all
select Wt3_Type ,Wt3 from tablename
union all
select Wt4_Type ,Wt4 from tablename
)A group by Wt_Type
Rather than giving the answer by #fa06, which should work for you, I am going to suggest using a better table design. Here is how you should be storing your data:
ID Type Wt
-------------
1 1 200
2 4 100
3 3 100
4 1 220
5 3 150
6 1 110
7 2 300
8 5 100
9 5 200
10 3 400
11 1 120
12 4 100
Note that there is a single column which stores the type and a single column for that type's weight. Now your expected output just requires a very simple query:
SELECT Type, SUM(Wt) AS Total
FROM yourTableUpdated
GROUP BY Type;
Databases are really good at performing operations across rows, much less so across columns.
Use this it should be work
select Wt_Type,sum(Wt) as total from ( select Wt1_Type as Wt_Type,Wt1 as Wt from tablename union all select Wt2_Type ,Wt2 from tablename union all select Wt3_Type ,Wt3 from tablename union all select Wt4_Type ,Wt4 from tablename )A group by Wt_Type

Average with partial data in SQL

I have data as below, which is partial and few rows are missing. I need an average with data considering the previous available value. Do we have any function available for such an average in sql? 
Needed average: 220 
Available data for 10 days:
1st day: 100
4th day: 200
7th day: 300
10th day: 400
Putting the same in a table format:
Rows Date Partial Continuous(needed)
1 01-Aug-18 100 100
2 100
3 100
4 04-Aug-18 200 200
5 200
6 200
7 07-Aug-18 300 300
8 300
9 300
10 10-Aug-18 400 400
-----------------------------------
Average 250 220
-----------------------------------
I am looking at something like select avg(partial*(nextdate-date))/(lastdate-firstdate) from mytable;
Use a user-defined variable to fill in the missing values.
SELECT AVG(normal)
FROM (SELECT IFNULL(continuous, #prev) AS normal, #prev := IF(continuous IS NULL, #prev, continuous)
FROM (SELECT continuous
FROM yourTable
ORDER BY id) AS x
CROSS JOIN (SELECT #prev := NULL) AS y
) as z
What about AVG()?
> SELECT AVG(Normal) FROM table WHERE something;

SUM two colmuns from two tables and find highest

I want to SUM two columns from two different database and output the highest value.
trying to figure it out since last 1day but no luck. can anyone please help?
Table 1
mid points
1 20
2 10
1 10
1 30
3 10
Table 2
mid points
1 20
2 10
1 10
2 20
1 10
3 10
so the total should be
mid points
1 100
2 40
3 20
output that i want highest total mid is 1 = 100
Try this untested query:
select mid , sum(points) from (
select mid,points from table1
union all
select mid,points from table2
) as table3
group by mid
order by sum(points) DESC
limit 1

How to perform SUM of rows in MySQL

I have a table called temp_reading. It has the following columns (consumption is an index key):
id consumption total
1 100
1 200
1 300
1 400
2 50
2 100
3 200
4 250
Now I want to display total as
id consumption total
1 100 100
1 200 300
1 300 600
1 300 900
2 50 50
2 100 150
3 200 200
4 250 250
Is it possible to display like the above?
I tried the following query:
SELECT id,consumption,sum(consumption) as total
FROM temp_reading
GROUP BY consumption;
Please help me solve this.
I recommend that you add a Primary Key on your temp_reading table. (Read more about Primary Keys) This key will be unique per row. Then you can try this query:
SELECT TR.id
, TR.consumption
, TR.consumption + IFNULL(SUM(TR2.consumption), 0) AS Total
FROM temp_reading TR
LEFT JOIN temp_reading TR2 ON TR.id = TR2.id AND TR.pk > TR2.pk
GROUP BY TR.pk;
I've tried it in SQL Fiddle.
select id,sum(consumption) as total
from temp_reading
group by id;
I suggest you do not have the same ID (1,1,1.2,2...)
Try this:
SELECT id, consumption, IF(#s=#s:=id, #s2:=#s2+consumption, #s2:=consumption) AS total
FROM temp_reading, (SELECT #s:=0, #s2:=0);