1

I have a problem and I have no idea how to do that. Please help me with an idea. So I have a table : test, with columns :

id        begin                    end     
1       2016-06-06 15:30:30     2016-06-08 16:40:40

Now I need to get the count of id by week The result need to be : For example if this week is the week number 6 I need to get :

Week           count
 4               10
 5               10
 6               12
 7               19
 8               27

Thx four you help

2
  • you should better explain the expected result. Commented Jun 7, 2016 at 14:57
  • If the current week is 6 I need to get the count from table test for the weeks 4,5,6,7,8 Commented Jun 7, 2016 at 14:58

1 Answer 1

1

Well, what happens if the begin is week 4, and end is week 5? which week is calculated?

Anyway, if I understood you, you want two previous weeks and 2 future weeks :

SELECT s.* FROM (
    SELECT YEAR(`start`) as year_col,WEEK(`start`) as week_col,count(*)
    FROM YourTable t
    GROUP BY year_col,week_col) s
WHERE s.week_col between WEEK(now())-2 and WEEK(now())+2

I've also added year into the consideration, since week 4 from 2015 will be group together with week 4 from 2016. If its not possible, exclude it from the query.

6
  • how to show week 7 count 0 if for this week no data ? Commented Jun 7, 2016 at 15:52
  • That's a whole different question mate.. @hareacosticla
    – sagi
    Commented Jun 7, 2016 at 16:03
  • can you help me please ? Commented Jun 7, 2016 at 17:52
  • stackoverflow.com/questions/3538858/… . This is the same as your problem, follow the steps. @hareacpsticla
    – sagi
    Commented Jun 7, 2016 at 17:55
  • I have a problem with this sql..because if are data in multiple years...for example for week = 23 are data in database, then it's get data for week=23 for year 2015,2014,... Commented Jun 8, 2016 at 8:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.