我需要您按日期订购查询,然后如果可以简化它:-)
此查询将返回从当天开始每周的条目数。
这是查询:
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