If this query
gives me this
and I want to get totals by week (the number on the far left) and by site (those are the 661,662 etc.) and a running cumulative total, how can I modify my query without disturbing the delicate balance at which I have arrived? I suppose I could do the totals in code, but would much prefer to handle them in the SQL. I have the above in a stored procedure if that is of any help.
Thanks for your time!
Willie
Code:
DECLARE @cols AS NVARCHAR(MAX),
@query AS NVARCHAR(MAX)
select @cols = STUFF((SELECT distinct ',' + QUOTENAME(study_site_id)
from TBLSTUDYDOC
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
SET @query =
'SELECT weeknum,c.weekstart,c.weekend,'+@cols+' FROM vwCalendar C left join
(
SELECT cast(DATEADD(dd, -(DATEPART(dw, STUDY_CONSENT_DATE)-1), STUDY_CONSENT_DATE) as DATE) [WeekStart],
cast(DATEADD(dd, 7-(DATEPART(dw, STUDY_CONSENT_DATE)), STUDY_CONSENT_DATE) as DATE) [WeekEnd],
STUDY_SITE_ID
FROM [dbEDRN320].[dbo].[TBLSTUDYDOC]
where STUDY_PROTOCOL_VISIT_CODE=1
and DELETEFLAG=0 and ENTRYFLAG=1
)t
PIVOT (count(study_site_id) FOR study_site_id
IN ('+@cols+') ) pvt on C.weekstart=pvt.weekstart
where C.weekstart<getdate()
order by C.weeknum'
EXECUTE (@query)
gives me this
Code:
1 2011-09-25 2011-10-01 3 1 0 0 2 0 0 0
2 2011-10-02 2011-10-08 NULL NULL NULL NULL NULL NULL NULL NULL
3 2011-10-09 2011-10-15 0 2 0 0 0 0 0 0
4 2011-10-16 2011-10-22 0 1 0 0 0 0 0 0
5 2011-10-23 2011-10-29 NULL NULL NULL NULL NULL NULL NULL NULL
6 2011-10-30 2011-11-05 2 3 0 0 0 0 0 0
7 2011-11-06 2011-11-12 NULL NULL NULL NULL NULL NULL NULL NULL
8 2011-11-13 2011-11-19 NULL NULL NULL NULL NULL NULL NULL NULL
9 2011-11-20 2011-11-26 NULL NULL NULL NULL NULL NULL NULL NULL
10 2011-11-27 2011-12-03 NULL NULL NULL NULL NULL NULL NULL NULL
11 2011-12-04 2011-12-10 NULL NULL NULL NULL NULL NULL NULL NULL
12 2011-12-11 2011-12-17 NULL NULL NULL NULL NULL NULL NULL NULL
13 2011-12-18 2011-12-24 1 1 0 0 0 0 0 0
14 2011-12-25 2011-12-31 NULL NULL NULL NULL NULL NULL NULL NULL
15 2012-01-01 2012-01-07 0 1 0 0 0 0 0 0
16 2012-01-08 2012-01-14 0 1 0 0 0 0 0 0
17 2012-01-15 2012-01-21 0 1 0 0 0 0 0 0
18 2012-01-22 2012-01-28 NULL NULL NULL NULL NULL NULL NULL NULL
19 2012-01-29 2012-02-04 1 2 0 0 0 0 0 0
20 2012-02-05 2012-02-11 0 1 0 0 0 0 0 0
21 2012-02-12 2012-02-18 NULL NULL NULL NULL NULL NULL NULL NULL
22 2012-02-19 2012-02-25 NULL NULL NULL NULL NULL NULL NULL NULL
23 2012-02-26 2012-03-03 0 1 0 0 0 0 0 0
24 2012-03-04 2012-03-10 NULL NULL NULL NULL NULL NULL NULL NULL
25 2012-03-11 2012-03-17 NULL NULL NULL NULL NULL NULL NULL NULL
26 2012-03-18 2012-03-24 0 1 0 0 0 0 0 0
27 2012-03-25 2012-03-31 NULL NULL NULL NULL NULL NULL NULL NULL
28 2012-04-01 2012-04-07 NULL NULL NULL NULL NULL NULL NULL NULL
29 2012-04-08 2012-04-14 NULL NULL NULL NULL NULL NULL NULL NULL
30 2012-04-15 2012-04-21 NULL NULL NULL NULL NULL NULL NULL NULL
31 2012-04-22 2012-04-28 NULL NULL NULL NULL NULL NULL NULL NULL
32 2012-04-29 2012-05-05 NULL NULL NULL NULL NULL NULL NULL NULL
33 2012-05-06 2012-05-12 NULL NULL NULL NULL NULL NULL NULL NULL
34 2012-05-13 2012-05-19 NULL NULL NULL NULL NULL NULL NULL NULL
35 2012-05-20 2012-05-26 NULL NULL NULL NULL NULL NULL NULL NULL
36 2012-05-27 2012-06-02 NULL NULL NULL NULL NULL NULL NULL NULL
37 2012-06-03 2012-06-09 NULL NULL NULL NULL NULL NULL NULL NULL
38 2012-06-10 2012-06-16 NULL NULL NULL NULL NULL NULL NULL NULL
39 2012-06-17 2012-06-23 NULL NULL NULL NULL NULL NULL NULL NULL
40 2012-06-24 2012-06-30 NULL NULL NULL NULL NULL NULL NULL NULL
41 2012-07-01 2012-07-07 1 0 0 0 0 0 0 0
42 2012-07-08 2012-07-14 NULL NULL NULL NULL NULL NULL NULL NULL
43 2012-07-15 2012-07-21 NULL NULL NULL NULL NULL NULL NULL NULL
44 2012-07-22 2012-07-28 NULL NULL NULL NULL NULL NULL NULL NULL
45 2012-07-29 2012-08-04 NULL NULL NULL NULL NULL NULL NULL NULL
46 2012-08-05 2012-08-11 0 0 0 0 0 0 1 0
47 2012-08-12 2012-08-18 NULL NULL NULL NULL NULL NULL NULL NULL
48 2012-08-19 2012-08-25 NULL NULL NULL NULL NULL NULL NULL NULL
49 2012-08-26 2012-09-01 1 6 0 0 0 0 1 0
and I want to get totals by week (the number on the far left) and by site (those are the 661,662 etc.) and a running cumulative total, how can I modify my query without disturbing the delicate balance at which I have arrived? I suppose I could do the totals in code, but would much prefer to handle them in the SQL. I have the above in a stored procedure if that is of any help.
Thanks for your time!
Willie