OK, I have this query
which gives me
However, what I would like it to look like is weekstart, weekend, weeknum as the 'row names', study_site_id as the 'column names' and then countforsite as the values.
And I don't get how to use PIVOT to get this to work. It seems like ti should be fairly simple, but I don't see it. So, any pointers out there?
wb
Code:
SELECT cast(DATEADD(dd, -(DATEPART(dw, STUDY_CONSENT_DATE)-1), STUDY_CONSENT_DATE)as DATE) as [WeekStart],
cast(DATEADD(dd, 7-(DATEPART(dw, STUDY_CONSENT_DATE)), STUDY_CONSENT_DATE) as DATE) as [WeekEnd],
datepart(ww,STUDY_CONSENT_DATE) as weeknum,
STUDY_SITE_ID,
COUNT(datepart(ww,STUDY_CONSENT_DATE)) as countforsite
FROM [db].[dbo].[TBLSTUDYDOC]
where STUDY_PROTOCOL_VISIT_CODE=1
and DELETEFLAG=0 and ENTRYFLAG=1
group by datepart(ww,STUDY_CONSENT_DATE), study_site_id,DATEADD(dd, -(DATEPART(dw, STUDY_CONSENT_DATE)-1), STUDY_CONSENT_DATE),
DATEADD(dd, 7-(DATEPART(dw, STUDY_CONSENT_DATE)), STUDY_CONSENT_DATE)
order by DATEADD(dd, -(DATEPART(dw, STUDY_CONSENT_DATE)-1), STUDY_CONSENT_DATE)
which gives me
Code:
WeekStart WeekEnd weeknum STUDY_SITE_ID countforsite
2011-09-25 2011-10-01 40 661 1
2011-10-02 2011-10-08 41 661 2
2011-10-09 2011-10-15 42 661 2
2011-10-09 2011-10-15 42 677 2
2011-10-16 2011-10-22 43 661 5
2011-10-16 2011-10-22 43 676 2
2011-10-16 2011-10-22 43 677 4
2011-10-23 2011-10-29 44 661 2
2011-10-23 2011-10-29 44 676 1
2011-10-23 2011-10-29 44 677 1
2011-10-30 2011-11-05 45 661 4
2011-10-30 2011-11-05 45 676 2
2011-10-30 2011-11-05 45 677 3
2011-11-06 2011-11-12 46 661 6
2011-11-06 2011-11-12 46 676 1
2011-11-06 2011-11-12 46 677 2
2011-11-13 2011-11-19 47 661 8
However, what I would like it to look like is weekstart, weekend, weeknum as the 'row names', study_site_id as the 'column names' and then countforsite as the values.
Code:
weekstart weekend weeknum 661 676 677
9/25/2011 10/1/2011 40 1
10/2/2011 10/8/2011 41 2
10/9/2011 10/15/2011 42 2 2
10/16/2011 10/22/2011 43 5 2 4
10/23/2011 10/29/2011 44 2 1 1
10/30/2011 11/5/2011 45 4 2 3
11/6/2011 11/12/2011 46 6 1 2
11/13/2011 11/19/2011 47 8
And I don't get how to use PIVOT to get this to work. It seems like ti should be fairly simple, but I don't see it. So, any pointers out there?
wb