日付でクエリを注文して、それを簡素化できるかどうかを確認する必要があります:-)
このクエリは、当日からの 1 週間あたりのエントリ数を返します。
クエリは次のとおりです。
SELECT CONVERT(VarChar(50), DATEADD(day, DATEDIFF(day, 0, GETDATE()), -7), 103) as periode_join, COUNT(u.usr_ID) as cptu
FROM [USR_USER] u
INNER JOIN [USI_USER_SITE] s ON u.USR_ID = s.USR_ID
WHERE u.[USR_JOINED_DT] >= DATEADD(day, DATEDIFF(day, 0, GETDATE()), -7) and u.[USR_JOINED_DT] <= DATEADD(day, DATEDIFF(day, 0, GETDATE()), 0)
UNION
SELECT CONVERT(VarChar(50), DATEADD(day, DATEDIFF(day, 0, GETDATE()), -14), 103) as periode_join, COUNT(u.usr_ID) as cptu
FROM [USR_USER] u
INNER JOIN [USI_USER_SITE] s ON u.USR_ID = s.USR_ID
WHERE u.[USR_JOINED_DT] >= DATEADD(day, DATEDIFF(day, 0, GETDATE()), -14) and u.[USR_JOINED_DT] <= DATEADD(day, DATEDIFF(day, 0, GETDATE()), -7)
UNION
SELECT CONVERT(VarChar(50), DATEADD(day, DATEDIFF(day, 0, GETDATE()), -21), 103) as periode_join, COUNT(u.usr_ID) as cptu
FROM [USR_USER] u
INNER JOIN [USI_USER_SITE] s ON u.USR_ID = s.USR_ID
WHERE u.[USR_JOINED_DT] >= DATEADD(day, DATEDIFF(day, 0, GETDATE()), -21) and u.[USR_JOINED_DT] <= DATEADD(day, DATEDIFF(day, 0, GETDATE()), -14)
UNION
SELECT CONVERT(VarChar(50), DATEADD(day, DATEDIFF(day, 0, GETDATE()), -28), 103) as periode_join, COUNT(u.usr_ID) as cptu
FROM [USR_USER] u
INNER JOIN [USI_USER_SITE] s ON u.USR_ID = s.USR_ID
WHERE u.[USR_JOINED_DT] >= DATEADD(day, DATEDIFF(day, 0, GETDATE()), -28) and u.[USR_JOINED_DT] <= DATEADD(day, DATEDIFF(day, 0, GETDATE()), -21)
UNION
SELECT CONVERT(VarChar(50), DATEADD(day, DATEDIFF(day, 0, GETDATE()), -35), 103) as periode_join, COUNT(u.usr_ID) as cptu
FROM [USR_USER] u
INNER JOIN [USI_USER_SITE] s ON u.USR_ID = s.USR_ID
WHERE u.[USR_JOINED_DT] >= DATEADD(day, DATEDIFF(day, 0, GETDATE()), -35) and u.[USR_JOINED_DT] <= DATEADD(day, DATEDIFF(day, 0, GETDATE()), -28)
UNION
SELECT CONVERT(VarChar(50), DATEADD(day, DATEDIFF(day, 0, GETDATE()), -42), 103) as periode_join, COUNT(u.usr_ID) as cptu
FROM [USR_USER] u
INNER JOIN [USI_USER_SITE] s ON u.USR_ID = s.USR_ID
WHERE u.[USR_JOINED_DT] >= DATEADD(day, DATEDIFF(day, 0, GETDATE()), -42) and u.[USR_JOINED_DT] <= DATEADD(day, DATEDIFF(day, 0, GETDATE()), -35)
ORDER BY periode_join desc
結果は次のとおりです。
periode_join cptu
28/05/2013 8740
25/06/2013 9773
18/06/2013 8212
11/06/2013 6644
04/06/2013 9420
02/07/2013 7868
どうもありがとう:-D