Adding relative week number column to MySQl results
        Posted  
        
            by Anthony
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Anthony
        
        
        
        Published on 2010-05-27T16:01:01Z
        Indexed on 
            2010/05/27
            16:31 UTC
        
        
        Read the original article
        Hit count: 289
        
I have a table with 3 columns: user, value, and date. The main query returns the values for a specific user based on a date range:
SELECT date, value FROM values
WHERE user = '$user' AND
date BETWEEN $start AND $end
What I would like is for the results to also have a column indicating the week number relative to the date range. So if the date range is 1/1/2010 - 1/20/2010, then any results from the first Sun - Sat of that range are week 1, the next Sun - Sat are week 2, etc. If the date range starts on a Saturday, then only results from that one day would be week 1. If the date range starts on Thursday but the first result is on the following Monday, it would be week 2, and there are no week 1 results.
Is this something fairly simple to add to the query? The only ideas I can come up with would be based on the week number for the year or the week number based on the results themselves (where in that second example above, the first result always gets week 1).
© Stack Overflow or respective owner