Wednesday, 15 May 2013

mysql - Convert dates into weeks -


How to change the continuous date in weeks, the week of Saturday should end Example: < / P>

  2/27/2015 3/6/2015 3/13/2015 3/20/2015  

Here I give more information about your requirement I'm so easy to understand.

I have a datetime field in my db that tells Date2. Me mm / dd / yyyy This date needs to be grouped in date2 as 1/1/2015 like 20/1/2015 .

  1/1/2015-3 / 1/2015 This category has been displayed as 3/1/2015, 4/1/2015- 10/1/2015 - --------------------- 10/1/2015, 11/1/2015-17 / 1/2015 ----- ------- ---------- 17/1/2015, 18/1 / 2015-24 / 1/2015 ---------------- ----- 24 / 1/2015  

It is not completely clear what you are asking , Then I would guess If I feel wrong, please forgive me.

Let us know that you have an arbitrary DATETIME column named Date2 , and you want to create a DATE on Sunday immediately before that value. In other words, you want to find out the week that contains DATETIME .

You can do this:

  FROM_DAYS (TO_DAYS (date 2) - MOD (TO_DAYS (date 2) -1, 7))  

So, if you have a sales table with customer_id , date2 , and sales , you summarize this way in the week You can: SELECT FROM_DAYS (TO_DAYS (date 2) -MOD (TO_DAYS (date -1, 7)) Week_bugging, COUNT (*) num_transactions, COUNT (DISTINCT customer_id) num_customers,

This sale from sale group by FROM_DAYS (TO_DAYS (Date 2) - MOD (TO_DAYS (Date) -1, 7) from SUM (Sales) Total_SESS Each calendar week will show on the date of the commencement. If you want to end each calendar week on Saturday, then use it instead.

  SELECT FROM_DAYS (TO_DAYS (date 2 ) - MOD (TO_DAYS (date) -1, 7)) + Interval 6 days a week, COUNT (*) num_transactions, ...  

I have written it in detail here:

You


No comments:

Post a Comment