我对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(问题所针对的应用程序)是动态的,列表每个月都在增长或缩小。
我基本上需要布局
申请办公室将于5月5月4月1月1月前举行WORD LONDON 20 1 1 2 5 10 1
WORD PARIS 10 2 3 1 2 0 3
EXCEL MADRID 05 0 0 3 2 0 0
等(如果在这个布局上有意义的话!)
我已经走了6条单独的报道,但它在ssrs中看起来并不是很好。我已经考虑过#tmptables,但他们不喜欢插入不同的行。
答案 0 :(得分: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日到现在每个月的数字。