sql - MySQL query that gets all rows UNTIL the SUM(column) is bigger than X -
i have following data
user_id days date 88 2 2013-08-25 88 4 2013-08-23 88 18 2013-08-5 88 1 2013-08-4 88 2 2013-08-2 73 11 2013-08-2 299 4 2013-08-2 12 983 2013-08-2
i'm trying recent rows (order date desc
) specific user_id , until sum of days
column bigger x
. example in case if x=7 3 first rows sum(days)=24.
try this. here use local variable count sums in subquery.
select user_id, days, date ( select user_id, days, date, @sum_days := @sum_days + days sum_days mytable order date desc ) t cross join (select @sum_days := 0) const -- resetting @sum_days var. sum_days < x -- fill number in x here.
Comments
Post a Comment