Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR SQL

How to group by week (7 days) in SQL Server

SELECT
    CONVERT(VARCHAR(5), DATEADD(WK, WK, 0), 103) + ' - ' + CONVERT(VARCHAR(5), DATEADD(DD, 6, DATEADD(WK, WK, 0)), 103)
    , Cnt
FROM (
    SELECT
        DATEDIFF(WK, 0, MyDate) AS WK
        , COUNT(*) AS Cnt
    FROM (VALUES
    ('2013-01-01')
    , ('2013-01-02')
    , ('2013-01-03')
    , ('2013-01-04')
    , ('2013-01-05')
    , ('2013-01-06')
    , ('2013-01-07')
    ) AS X(MyDate)
    GROUP BY DATEDIFF(WK, 0, MyDate)
) AS X
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #How #group #week #SQL #Server
ADD COMMENT
Topic
Name
8+5 =