-1

日付でクエリを注文して、それを簡素化できるかどうかを確認する必要があります:-)

このクエリは、当日からの 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

4

1 に答える 1

2

SQL Server 2005 以降を使用している場合は、次のようなことを試すことができます。

;with CTE_Data as
(
  select
        u.usr_id,
        cast(dateadd(dd, -(datediff(dd, u.usr_joined_dt, getdate()) / 7 + 1) * 7, getdate()) as date) as periode_join
    from usr_user as u 
        inner join usi_user_site as s on u.usr_id = s.usr_id
)
select periode_join, count(usr_id) as cptu
from CTE_Data
group by periode_join
order by periode_join asc

SQL フィドルの例

データを週ごとに分割するには、 getdate() とテーブルからの日付の差を取得する必要があります - datediff(dd, u.usr_joined_dt, getdate())、次に全体の週数を取得する必要があります - 、次に全体/ 7の週数 + 1 を減算する必要があります。

たとえば、日付 ='2013/06/30'日数 = 9、週数 = 1 の場合、1 + 1 週を引く必要があるため、'2013/06/25' などを取得します。

于 2013-07-09T12:40:58.873 に答える