1

I have read several questions like MySQL grouping by week, based on a date column?.

But my date type is int(11)(I can't change the structure) to store something like 20150101 to represent 1st Jan,2015. How should I use WEEK method to group by based on this?

Like:

select week(SOMETHING_TO(dateid)) as weeknum, game ,count(*) 
from table where game is not null group by weeknum , game order by weeknum desc;
Community
  • 1
  • 1
JaskeyLam
  • 14,373
  • 17
  • 110
  • 142

1 Answers1

1

I think you need to first change your int(11) value to date and then use the Week function ie,

select WEEK(DATE(dateid)) as weeknum, game ,count(*) 
from table

DEMO

EDIT:-

As per your comments you may try to use

select concat('last ' ,(WEEK(dateid,1) -WEEK(CURDATE(),1)),' week',' ' )

DEMO

Also check the WEEK function in detail.

The two-argument form of WEEK() enables you to specify whether the week starts on Sunday or Monday and whether the return value should be in the range from 0 to 53 or from 1 to 53. If the mode argument is omitted, the value of the default_week_format system variable is used.

Also see this list for reference:

M   F.day   Range   Week 1 is the first week …
0   Sunday  0-53    with a Sunday in this year
1   Monday  0-53    with more than 3 days this year
2   Sunday  1-53    with a Sunday in this year
3   Monday  1-53    with more than 3 days this year
4   Sunday  0-53    with more than 3 days this year
5   Monday  0-53    with a Monday in this year
6   Sunday  1-53    with more than 3 days this year
7   Monday  1-53    with a Monday in this year

M = Mode
F.day = First day of week
JaskeyLam
  • 14,373
  • 17
  • 110
  • 142
Rahul Tripathi
  • 161,154
  • 30
  • 262
  • 319