|
Tom_Watson -> RE: Help with an SMS Report (6/9/2008 5:45:22 AM)
|
I thought I'd make this a little more complicated again, but simpler for the project managers that need these numbers to read. I combined this with the standard report 77 "Sites by hierarchy with time of last site status update", and got:- quote:
set nocount on /* order sites by hierarchy */ create table #SiteOrder ( SiteCode char(3), SiteLevel int, PreOrder int identity, PRIMARY KEY (SiteLevel,SiteCode) ) declare @NextS char(3) declare @SiteLevel int set @SiteLevel=1 set @NextS='' /* peform a depth first search of the site table, list the sites in pre-order */ while @NextS is not null begin /* find the next child node */ select @NextS=MIN(SiteCode) from v_Site where ReportingSiteCode=@NextS and SiteCode not in (select SiteCode from #SiteOrder where SiteLevel=@SiteLevel) if @NextS IS NULL begin set @SiteLevel=@SiteLevel-1 /* pop up one level and find the last parent we were working with */ select @NextS = SiteCode from #SiteOrder where PreOrder= (select MAX(PreOrder) from #SiteOrder where SiteLevel=@SiteLevel-1) end else begin insert into #SiteOrder(SiteCode,SiteLevel) values(@NextS,@SiteLevel) /* move down one level and and find the children of this site */ set @SiteLevel = @SiteLevel + 1 end end /* run the query, order by PreOrder, use SPACE with SiteLevel for indentation */ SELECT SPACE(3*(SiteLevel-1))+so.SiteCode as SiteCode, s.SiteName, cnt.ABC0001F AS [Collection ABC0001F Count], cnt.ABC00062 AS [Collection ABC00062 Count], cnt.ABC00062 - cnt.ABC0001F AS [Difference], CASE WHEN cnt.ABC00062 = 0 THEN NULL ELSE ROUND((cnt.ABC00062 - cnt.ABC0001F)*100.0/cnt.ABC00062, 2) END AS [Percentage] FROM #SiteOrder so JOIN v_Site s ON so.SiteCode=s.SiteCode LEFT JOIN (SELECT fcm.SiteCode, SUM(CASE fcm.CollectionID WHEN 'ABC0001F' THEN 1 ELSE 0 END) as [ABC0001F], SUM(CASE fcm.CollectionID WHEN 'ABC00062' THEN 1 ELSE 0 END) as [ABC00062] FROM dbo.v_FullCollectionMembership AS fcm WHERE fcm.CollectionID IN('ABC0001F','ABC00062') GROUP BY fcm.SiteCode) AS cnt ON so.SiteCode=cnt.SiteCode ORDER BY so.PreOrder drop table #SiteOrder A bit more complicated again though. Tom
|
|
|
|