I have two SQL queries that I want to divide. On their own each query works, and I want to divide their results and obtain into third variable, but I am unsure how.
This query calculate no of cancelled members
(select count(*) as No_of_Member_Cancelled, M.HomeBranch,M.LocationName from
AX.Memberships M
where M.ActiveEnd between DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) - 1,
0) and DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) ,-1)
group by M.HomeBranch,M.LocationName) as g1
This query calculate the no of live members
(select count(*)as No_of_Live_Member , M.HomeBranch,M.LocationName
from AX.Memberships M
where M.ActiveStart between DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) -
1, 0) and DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) ,-1)
group by M.HomeBranch,M.LocationName) as g2
We obtain results like this
if x = g1/g2
x , LocationName , HomeBranch in one table
Best to change those to temp tables then you can do it like this:
select No_of_Member_Cancelled / No_of_Live_Member as x, HomeBranch, LocationName
from G1
join G2 on g1.HomeBranch = g2.HomeBranch and g1.LocationName = g2.LocationName
group by HomeBranch, LocationName
Try this...
SELECT CAST (g1.no_of_member_cancelled AS DECIMAL(10, 2)) /
CAST (g2.no_of_live_member AS DECIMAL(10, 2)) AS x,
g1.homebranch,
g1.locationname
FROM (SELECT Count(*) AS No_of_Member_Cancelled,
M.homebranch,
M.locationname
FROM ax.memberships M
WHERE M.activeend BETWEEN Dateadd(month, Datediff(month, 0, Getdate())
- 1, 0)
AND
Dateadd(month, Datediff(month, 0,
Getdate())
, -1)
GROUP BY M.homebranch,
M.locationname) AS g1
INNER JOIN (SELECT Count(*)AS No_of_Live_Member,
M.homebranch,
M.locationname
FROM ax.memberships M
WHERE M.activestart BETWEEN Dateadd(month, Datediff(month, 0
,
Getdate()) - 1,
0) AND
Dateadd(month, Datediff(month,
0, Getdate(
)), -1)
GROUP BY M.homebranch,
M.locationname) AS g2
ON g1.homebranch = g2.homebranch
AND g1.locationname = g2.locationname
You will need to cast (or convert) your number of cancelled and live members to the datatype that fits your situation given the size of your potential values. You probably should take precaution so you don't get a division by zero error.
SELECT distinct CAST (g1.no_of_member_cancelled AS DECIMAL(10, 2)) /
CAST (g2.no_of_live_member AS DECIMAL(10, 2)) AS x,
g1.homebranch,
g1.locationname,
g1.month,
g1.year
FROM (SELECT Count(*) AS No_of_Member_Cancelled,
M.homebranch,
M.locationname,
month(M.ActiveStart) month,
YEAR(M.ActiveStart) year
FROM ax.memberships M
WHERE M.activeend BETWEEN Dateadd(year, Datediff(year, 0, Getdate()) - 2, 0)
AND
Dateadd(year, Datediff(year, 0, Getdate()), -1)
GROUP BY M.homebranch,
M.locationname,month(M.ActiveStart),
year(M.ActiveStart)) AS g1
INNER JOIN (SELECT Count(*)AS No_of_Live_Member,
M.homebranch,
M.locationname,
month(M.ActiveStart) month,
YEAR(M.ActiveStart) year
FROM ax.memberships M
WHERE M.activestart BETWEEN Dateadd(year, Datediff(year, 0, Getdate()) - 2, 0)
AND
Dateadd(year, Datediff(year, 0, Getdate()), -1)
GROUP BY M.homebranch,
M.locationname,
month(M.ActiveStart),
year(M.ActiveStart)
) AS g2
ON g1.homebranch = g2.homebranch
AND g1.locationname = g2.locationname
AND g1.month = g2.month
AND g1.year = g2.year
Order by g1.year,g1.month
Can you please check my query is right or not. Because I wanted to get from last 2 yrs.
I have a data in which I have to display a record by month or week or day according to the date selected.
If the date selected is for a whole month for like january its 01-01-2015 to 31-01-2015
then it should fetch for the record for the whole month.
If the date selected is such that for example like 01-01-2015 to 15-02-2015. Now here there are more than a month but Feburary is not in whole month so the january will record will come in one and next first week record for Feburary will be fetched in one and next week record will be fetched in another`.
Also he day left (as there will be 14 days in week two) will have a another record.`
So, in all 4 record will be fetched. Here instead of fecthing whole 46 days record I am just fecthing 4 records. Hope I gave a proper example if you can't just comment I will explain with another exapmle. I am now just worrying about how to split month, week and day. Can anyone help me with that. Thanks in advance
I am only worried about how to split the dates in month week or a day. The formula is simple if e have whole month the entry should be shown in a single record. If we date between of more than week it should shown week wise and less than week it should be on day level
Input
Here Time_type_id is a foreign key from other 3 tables. This time_type_id will help to get data day, week or month wise. The data is shown according to date passed by the user. Time_id is the primary key of the table and it points to the id of other 3 fact table.
Dimesion Table
Time_type_id Time_id metric_id
1 1 100
1 2 400
1 3 300
1 4 100
1 5 250
2 6 1000
2 7 2000
2 8 3000
2 9 2000
2 10 5000
3 11 11000
3 12 20000
3 13 40000
3 14 50000
3 15 60000
Fact table
Fact1- This shows the day basis data.
Dim_type Id day_date month year
1 1 01-09-2015 September 2015
1 2 02-09-2015 September 2015
1 3 03-09-2015 September 2015
1 4 04-09-2015 September 2015
1 5 05-09-2015 September 2015
Fact2- This shows the weekly basis data.
Dim_type Id week_number month year
2 6 week1 September 2015
2 7 week2 September 2015
2 8 week3 September 2015
2 9 week4 September 2015
2 10 week1 October 2015
Fact3- This shows the monthly basis data.
Dim_type Id month year
3 11 September 2015
3 12 September 2015
3 13 September 2015
3 14 September 2015
3 15 October 2015
Now the user just passed the dates between which he wants to see the data and according to dates passed we have to sho the ouput.
If it is month level we have to show Fact3 table where Dim_type is 3 same for other two table. I just want how to split the the date
Output-
For second example-
Sr no id data (from the date range)
1 1 data from whole January
2 2 data from first week of Feburary
3 3 data from second week of Feburary
4 4 data from 15 Febuaray
Note: Don't make that a week will always have 7 days maybe if week start from Thrusday than it would not have 7 days
you can use two parameter, so it's dynamic to choose dates like:
Declare #FromDate DATETIME,
#ToDate DATETIME
SELECT ...
FROM ...
WHERE DATE BETWEEN #FromDate AND #ToDate
There can be two solution available in your case:
Solution # 1
You need a table/temptable which contains data for all dates with columns like Date, DaySrNo, WeekSrNo, MonthSrNo and Moreover YearSrNo. Use this table in Join and get your data among three tables (which are also in JOIN)
Solution # 2
You need extra column for date range in your existing table structure:
like In Fact1 table contains day wise data, So you need Date Range Columns Like StartDate and EndDate
Same way Same Two column in other tables Fact2 and Fact3
So using this column in Join and where filter you can get your desired output.
IF OBJECT_ID('IAS_TEST_SCHEMA.DATESPLIT', 'P') IS NOT NULL
DROP PROCEDURE IAS_TEST_SCHEMA.DATESPLIT;
GO
CREATE PROCEDURE IAS_TEST_SCHEMA.DATESPLIT (#STARTDATE date, #ENDDATE date)
AS
BEGIN
DECLARE #DATEDIFF integer,
#MONTHEND integer = 0,
#MONTHSTART integer = 0,
#STARTWEEK integer = 0,
#ENDWEEK integer = 0;
IF #STARTDATE = CONVERT(date, DATEADD(M, DATEDIFF(M, 0, #STARTDATE), 0))
BEGIN
SET #MONTHSTART = 1;
END
IF MONTH(#ENDDATE) != MONTH(DATEADD(DAY, 1, #ENDDATE))
BEGIN
SET #MONTHEND = 1;
END
IF DATEPART(DW, #STARTDATE) = 2
BEGIN
SET #STARTWEEK = 1;
END
IF DATEPART(DW, #ENDDATE) = 1
BEGIN
SET #ENDWEEK = 1;
END
SET #DATEDIFF = DATEDIFF(DAY, #STARTDATE, #ENDDATE);
SELECT
CASE
WHEN #DATEDIFF <= 30 THEN CONVERT(varchar, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #STARTDATE), 0)))
WHEN #DATEDIFF > 30 AND
#DATEDIFF < 90 THEN CASE
WHEN #STARTWEEK = 1 THEN 'WEEK ' + CAST(DATEPART(WEEK, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #STARTDATE), 0)))
- DATEPART(WEEK, DATENAME(YEAR, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #STARTDATE), 0))) +
'/' + DATENAME(MONTH, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #STARTDATE), 0))) + '/' + '1') AS nvarchar) +
' ' + DATENAME(MONTH, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #STARTDATE), 0)))
ELSE CONVERT(varchar, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #STARTDATE), 0)))
END
WHEN #DATEDIFF >= 90 THEN CASE
WHEN #MONTHSTART = 1 THEN DATENAME(MONTH, #STARTDATE) + ' ' + DATENAME(YEAR, #STARTDATE)
ELSE CONVERT(varchar, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #STARTDATE), 0)))
END
END AS START_DATE,
CASE
WHEN #DATEDIFF <= 30 THEN CONVERT(varchar, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #ENDDATE), 0)))
WHEN #DATEDIFF > 30 AND
#DATEDIFF < 90 THEN CASE
WHEN #STARTWEEK = 1 THEN CASE
WHEN #ENDWEEK = 1 THEN 'WEEK ' + CAST(DATEPART(WEEK, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #ENDDATE), 0)))
- DATEPART(WEEK, DATENAME(YEAR, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #ENDDATE), 0))) +
'/' + DATENAME(MONTH, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #ENDDATE), 0))) + '/' + '1') AS nvarchar) +
' ' + DATENAME(MONTH, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #ENDDATE), 0)))
ELSE 'WEEK ' + CAST(DATEPART(WEEK, CONVERT(date, DATEADD(WEEK, DATEDIFF(WEEK, 0, #ENDDATE), -1)))
- DATEPART(WEEK, DATENAME(YEAR, CONVERT(date, DATEADD(WEEK, DATEDIFF(WEEK, 0, #ENDDATE), -1))) + '/'
+ DATENAME(MONTH, CONVERT(date, DATEADD(WEEK, DATEDIFF(WEEK, 0, #ENDDATE), -1))) + '/' + '1') AS nvarchar) +
' ' + DATENAME(MONTH, CONVERT(date, DATEADD(WEEK, DATEDIFF(WEEK, 0, #ENDDATE), -1)))
END
ELSE CONVERT(varchar, CONVERT(date, DATEADD(WEEK, DATEDIFF(WEEK, 0, #STARTDATE), 6)))
END
WHEN #DATEDIFF >= 90 THEN CASE
WHEN #MONTHSTART = 1 THEN CASE
WHEN #MONTHEND = 0 THEN DATENAME(MONTH, DATEADD(MONTH, DATEDIFF(MONTH, -1, #ENDDATE) - 1, -1)) + ' ' + DATENAME(YEAR, DATEADD(MONTH, DATEDIFF(MONTH, -1, #ENDDATE) - 1, -1))
ELSE DATENAME(MONTH, #ENDDATE) + ' ' + DATENAME(YEAR, #ENDDATE)
END
ELSE CONVERT(varchar, CONVERT(date, DATEADD(S, -1, DATEADD(MM, DATEDIFF(M, 0, #STARTDATE) + 1, 0))))
END
END AS END_DATE
UNION
SELECT
CASE
WHEN #DATEDIFF <= 30 THEN CONVERT(varchar, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #STARTDATE), 0)))
WHEN #DATEDIFF > 30 AND
#DATEDIFF < 90 THEN CASE
WHEN #STARTWEEK = 1 THEN 'WEEK ' + CAST(DATEPART(WEEK, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #STARTDATE), 0)))
- DATEPART(WEEK, DATENAME(YEAR, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #STARTDATE), 0))) +
'/' + DATENAME(MONTH, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #STARTDATE), 0))) + '/' + '1') AS nvarchar) +
' ' + DATENAME(MONTH, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #STARTDATE), 0)))
ELSE 'WEEK ' + CAST(DATEPART(WEEK, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #STARTDATE), 7)))
- DATEPART(WEEK, DATENAME(YEAR, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #STARTDATE), 7))) +
'/' + DATENAME(MONTH, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #STARTDATE), 7))) + '/' + '1') AS nvarchar) +
' ' + DATENAME(MONTH, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #STARTDATE), 7)))
END
WHEN #DATEDIFF >= 90 THEN CASE
WHEN #MONTHSTART = 1 THEN DATENAME(MONTH, #STARTDATE) + ' ' + DATENAME(YEAR, #STARTDATE)
ELSE DATENAME(MONTH, CONVERT(date, DATEADD(M, DATEDIFF(M, -1, #STARTDATE), 0))) + ' '
+ DATENAME(YEAR, CONVERT(date, DATEADD(M, DATEDIFF(M, -1, #STARTDATE), 0)))
END
END,
CASE
WHEN #DATEDIFF <= 30 THEN CONVERT(varchar, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #ENDDATE), 0)))
WHEN #DATEDIFF > 30 AND
#DATEDIFF < 90 THEN CASE
WHEN #ENDWEEK = 1 THEN 'WEEK ' + CAST(DATEPART(WEEK, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #ENDDATE), 0)))
- DATEPART(WEEK, DATENAME(YEAR, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #ENDDATE), 0))) +
'/' + DATENAME(MONTH, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #ENDDATE), 0))) + '/' + '1') AS nvarchar) +
' ' + DATENAME(MONTH, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #ENDDATE), 0)))
ELSE 'WEEK ' + CAST(DATEPART(WEEK, CONVERT(date, DATEADD(WEEK, DATEDIFF(WEEK, 0, #ENDDATE), -1)))
- DATEPART(WEEK, DATENAME(YEAR, CONVERT(date, DATEADD(WEEK, DATEDIFF(WEEK, 0, #ENDDATE), -1))) + '/'
+ DATENAME(MONTH, CONVERT(date, DATEADD(WEEK, DATEDIFF(WEEK, 0, #ENDDATE), -1))) + '/' + '1') AS nvarchar) +
' ' + DATENAME(MONTH, CONVERT(date, DATEADD(WEEK, DATEDIFF(WEEK, 0, #ENDDATE), -1)))
END
WHEN #DATEDIFF > 90 THEN CASE
WHEN #MONTHEND = 0 THEN DATENAME(MONTH, CONVERT(date, DATEADD(MONTH, DATEDIFF(MONTH, -1, #ENDDATE) - 1, -1))) + ' '
+ DATENAME(YEAR, CONVERT(date, DATEADD(MONTH, DATEDIFF(MONTH, -1, #ENDDATE) - 1, -1)))
ELSE DATENAME(MONTH, #ENDDATE) + ' ' + DATENAME(YEAR, #ENDDATE)
END
END
UNION
SELECT
CASE
WHEN #DATEDIFF <= 30 THEN CONVERT(varchar, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #STARTDATE), 0)))
WHEN #DATEDIFF > 30 AND
#DATEDIFF < 90 THEN CASE
WHEN #ENDWEEK = 1 THEN CASE
WHEN #STARTWEEK = 1 THEN 'WEEK ' + CAST(DATEPART(WEEK, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #STARTDATE), 0)))
- DATEPART(WEEK, DATENAME(YEAR, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #STARTDATE), 0))) +
'/' + DATENAME(MONTH, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #STARTDATE), 0))) + '/' + '1') AS nvarchar) +
' ' + DATENAME(MONTH, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #STARTDATE), 0)))
ELSE 'WEEK ' + CAST(DATEPART(WEEK, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #STARTDATE), 7)))
- DATEPART(WEEK, DATENAME(YEAR, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #STARTDATE), 7))) +
'/' + DATENAME(MONTH, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #STARTDATE), 7))) + '/' + '1') AS nvarchar) +
' ' + DATENAME(MONTH, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #STARTDATE), 7)))
END
ELSE CONVERT(varchar, CONVERT(date, DATEADD(WEEK, DATEDIFF(WEEK, 0, #ENDDATE), -0)))
END
WHEN #DATEDIFF >= 90 THEN CASE
WHEN #MONTHEND = 1 THEN DATENAME(MONTH, #STARTDATE) + ' ' + DATENAME(YEAR, #STARTDATE)
ELSE CONVERT(varchar, CONVERT(date, DATEADD(DD, -(DAY(#ENDDATE) - 1), #ENDDATE)))
END
END,
CASE
WHEN #DATEDIFF <= 30 THEN CONVERT(varchar, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #ENDDATE), 0)))
WHEN #DATEDIFF > 30 AND
#DATEDIFF < 90 THEN CASE
WHEN #ENDWEEK = 1 THEN 'WEEK ' + CAST(DATEPART(WEEK, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #ENDDATE), 0)))
- DATEPART(WEEK, DATENAME(YEAR, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #ENDDATE), 0))) +
'/' + DATENAME(MONTH, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #ENDDATE), 0))) + '/' + '1') AS nvarchar) +
' ' + DATENAME(MONTH, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #ENDDATE), 0)))
ELSE CONVERT(varchar, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #ENDDATE), 0)))
END
WHEN #DATEDIFF >= 90 THEN CASE
WHEN #MONTHEND = 1 THEN DATENAME(MONTH, #ENDDATE) + ' ' + DATENAME(YEAR, #ENDDATE)
ELSE CONVERT(varchar, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, #ENDDATE), 0)))
END
END
END;
GO
I need you for order a query by the date and then if it's possible to simplify it :-)
This query will return the number of entries per week from the current day.
Here is the query :
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
Here is the result :
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
Thanks a lot :-D
if you have SQL Server 2005 and above, you can try something like this:
;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 FIDDLE EXAMPLE
To split data by weeks we have to take difference in days between getdate() and date from table - datediff(dd, u.usr_joined_dt, getdate()), then we have to get number of whole weeks - / 7, then we have to substract whole number of weeks + 1.
So, for example, if date = '2013/06/30' then number of days = 9, whole number of weeks = 1 and we have to substract 1 + 1 weeks so we get '2013/06/25' and so on