mysql - GROUP BY grouping multiple values as the same group -
i have table similar this:
id week 1 1 2 1 3 1 4 2 5 2 6 3 7 3 8 3
this current query:
select count(*), `week` data group `week`
here's sqlfiddle http://sqlfiddle.com/#!2/bfdb6/2/0
what need group rows every 2 weeks same count.
so instead of this:
count(*) week 3 1 2 2 3 3 3 4
i'd get:
count(*) week 5 1 5 2 6 3 3 4
where every week has next weeks count added it.
question amended:
i should have been clear needed.
rather grouping weeks 1 , 3, need grouping every 2 weeks.
so group weeks 1 , 2, 2 , 3, 3 , 4, 4 , 5, etc
automatically if possible, sql generated outside query week groupings.
thanks.
i think have you...
first, distinct weeks available data... then, join data on either week or week +1
i applied existing sqlfiddle , appeared work.
select justweeks.`week`, count(*) twoweeksum ( select distinct `week` data ) justweeks join data on justweeks.`week` = data.`week` or justweeks.`week` +1 = data.`week` group justweeks.`week`
Comments
Post a Comment