2012-07-11 33 views
0

我有一個關於SQL 2008的問題,這可能很容易,但我現在無法看到樹木的樹林。返回多個月的數據到一個選擇

我想產生一個基於SQL報告,詳細說明在過去六個月服務檯問題的統計資料,每個應用,每個辦事處,每月然後我考慮到SSRS申請可愛:O)

反正 - 我例如,有我的腳本,這個腳本每個月都很好;

SELECT  distinct t.name_1 'Application', 
      (select distinct name from location where location_ref = c.location_ref) as office, 
      Count (t.name_1) as [Call Count], 
      datename(month, dateadd(month,-2,getdate()))+' '+datename(year, dateadd(month,-2,getdate())) as [Report Month] 

FROM  call_logging C 
      Inner Join problem_type t On t.ref_composite = c.ref_composite 

AND   c.resolve_time between onvert(datetime,convert(varchar,month(dateadd(m,-2,getdate()))) + '/01/' + convert(varchar,year(dateadd(m,-2,getdate())))) 
      and convert(datetime,convert(varchar,month(dateadd(m,-1,getdate()))) + '/01/' + convert(varchar,year(getdate()))) 
      and c.resolve_group in ('48', '60') 

這將帶回所有May的問題。

問題是t.name_1(問題所針對的應用程序)是動態的,並且列表每個月都會增長或收縮。

我基本上需要

申請事務COUNT六月五月四月三月FEB的佈局JAN

WORD LONDON 20 1 1 2 5 10 1

WORD PARIS 10 2 3 1 2 0 3

EXCEL馬德里05 0 0 3 2 0 0

等(如果使在此佈局意義!)

我已經走了6條獨立報道,但它在ssrs中看起來不太好。我想過#tmptables,但他們不喜歡插入不同的行。

回答

0
SELECT [C].[name_1] AS [APPLICATION] 
     ,COUNT([name_1]) AS [CALL COUNT] 
     ,[l].[location_ref] 
     ,[dbo].[ufn_GetDateTime_CalenderYearMonth]([resolve_time]) AS [StartCalenderYearMonth] 
FROM [call_logging] [C] INNER JOIN [problem_type] [t] 
            ON [t].[ref_composite] = [c].[ref_composite] 
            AND [c].[resolve_group] IN ('48', '60') 
         INNER JOIN [location] [l] ON [c].[location_ref] = [l].[location_ref] 
WHERE [C].[resolve_time] BETWEEN '2011-01-01' AND GETDATE() 
GROUP BY [C].[name_1], [l].[location_ref], [dbo].[ufn_GetDateTime_CalenderYearMonth]([resolve_time]) 

而對於ufn_GetDateTime_CalenderYearMonth代碼:

CREATE FUNCTION [dbo].[ufn_GetDateTime_CalenderYearMonth] (@DateTime datetime)  
RETURNS varchar(20)  
AS  
BEGIN  
declare @dateString varchar(20)  
declare @yearString varchar(10)  
declare @monthString varchar(10)  

set @yearString = cast(DATEPART(year, @DateTime) as varchar(10))  

if(DATEPART(month, @DateTime) < 10)  
    set @monthString = '0' + cast(DATEPART(month, @DateTime) as varchar(5))  
else  
    set @monthString = cast(DATEPART(month, @DateTime) as varchar(5))  

set @dateString = @yearString + '-' + @monthString  

RETURN (@dateString)  

END 

你剛纔拍的結果集由[StartCalenderYearMonth]矩陣和羣體的一切,它會顯示每月人數從2011年1月1日至現在..

+0

謝謝sc0rp - 我會給那個去:) – Tricky 2012-07-12 09:35:14