Limit the row as a sum of a column equal to a value in MySQL

I want to write a query that returns all rows until the sum of the values ​​in one column reaches a certain value.

For example, in the following table:

p>

DATE ETC Meeting
2013-02-01 00:00:00 85482 1
2013-02-01 00:00:00 47228 2
2013-02-02 00:00:00 12026 4
2013-02-03 00:00:00 78927 6
2013-02-04 00:00:00 85662 2
2013 -03-05 00:00:00 47978 1
2013-08-07 00:00:00 8582 1

If I want to get rows until the sum of the columns is equal to 7.

DATE ETC Meeting
2013-02-01 00:00:00 85482 1
2013-02-01 00:00:00 47228 2
2013-02-02 00:00:00 12026 4

If I want to get rows until the sum of the meeting columns is equal to 13.

DATE ETC Meeting 
2013-02-01 00:00:00 85482 1
2013-02-01 00:00:00 47228 2
2013-02-02 00:00:00 12026 4
2013-02-03 00:00:00 78927 6

This is a method for MySQL:

SELECT
O.Id ,
O.Type,
O.MyAmountCol,
(SELECT
sum(MyAmountCol) FROM Table1
WHERE Id <= O.Id)'RunningTotal'
FROM Table1 O
HAVING RunningTotal <= 7

It involves calculating a running total and selecting records, while the running total is less than or equal to a given number, which in this case is 7.< /p>

SQL Fiddle

I want to write a query that returns all rows until the sum of the values ​​in one of the columns reaches a certain value.

For example, in the following table:

DATE ETC Meeting
2013-02-01 00:00:00 85482 1
2013-02 -01 00:00:00 47228 2
2013-02-02 00:00:00 12026 4
2013-02-03 00:00:00 78927 6
2013-02-04 00:00:00 85662 2
2013-03-05 00:00:00 47978 1
2013-08-07 00:00:00 8582 1

If I want to get Row until the sum of the columns equals 7.

DATE ETC Meeting
2013-02-01 00:00:00 85482 1
2013-02- 01 00:00:00 47228 2
2013-02-02 00:00:00 12026 4

If I want to get rows until the sum of the meeting columns equals 13.

DATE ETC Meeting
2013-02-01 00 :00:00 85482 1
2013-02-01 00:00:00 47228 2
2013-02-02 00:00:00 12026 4
2013-02-03 00:00 :00 78927 6

This is a method suitable for MySQL:

SELECT 
O.Id,
O.Type,
O.MyAmountCol,
(SELECT
sum(MyAmountCol) FROM Table1
WHERE Id <= O. Id)'RunningTotal'
FROM Table1 O
HAVING RunningTotal <= 7

It involves calculating a running total and selecting records, and the running total is less than or equal to a given number. In this case, it is 7.

SQL Fiddle

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 3000 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.