这个问题让我现在围着圈子走了一圈,我想知道是否有人可以指出我哪里出错了。
我试图生成一个SPROC,它生成一个数据集,由SSRS调用过去6个月的图表。
出于示例目的的数据使用了三个表(更多但不会改变手头的问题),如下所示:
tbl_ReportList:
Report Site
----------------
North abc
North def
East bbb
East ccc
East ddd
South poa
South pob
South poc
South pod
West xyz
tbl_TicketsRaisedThisMonth:
Date Site Type NoOfTickets
---------------------------------------------------------
2010-07-01 00:00:00.000 abc Support 101
2010-07-01 00:00:00.000 abc Complaint 21
2010-07-01 00:00:00.000 def Support 6
...
2010-12-01 00:00:00.000 abc Support 93
2010-12-01 00:00:00.000 xyz Support 5
tbl_FeedBackRequests:
Date Site NoOfFeedBackR
----------------------------------------------------------------
2010-07-01 00:00:00.000 abc 101
2010-07-01 00:00:00.000 def 11
...
2010-12-01 00:00:00.000 abc 63
2010-12-01 00:00:00.000 xyz 4
我正在使用CTE来简化代码,如下所示:
DECLARE @ReportName VarChar(200)
SET @ReportName = 'North';
WITH TicketsRaisedThisMonth AS
(
SELECT
[Date],
Site,
SUM(NoOfTickets) AS NoOfTickets
FROM tbl_TicketsRaisedThisMonth
WHERE [Date] >= DATEADD(mm, DATEDIFF(m,0,GETDATE())-6,0)
GROUP BY [Date], Site
),
FeedBackRequests AS
(
SELECT
[Date],
Site,
SUM(NoOfFeedBackR) AS NoOfFeedBackR
FROM tbl_FeedBackRequests
WHERE [Date] >= DATEADD(mm, DATEDIFF(m,0,GETDATE())-6,0)
GROUP BY [Date], Site
),
SELECT
trtm.[Date]
SUM(trtm.NoOfTickets) AS NoOfTickets,
SUM(fbr.NoOfFeedBackR) AS NoOfFeedBackR,
FROM tbl_ReportList rpts
LEFT OUTER JOIN TotalIncidentsDuringMonth trtm ON rpts.Site = trtm.Site
LEFT OUTER JOIN LoggedComplaints fbr ON rpts.Site = fbr.Site
WHERE rpts.report = @ReportName
GROUP BY trtm.[Date]
当sproc传递一个参数如'North'时的输出如下:
Date NoOfTickets NoOfFeedBackR
-----------------------------------------------------------------------------------
2010-07-01 00:00:00.000 128 112
2010-08-01 00:00:00.000 <data for that month> <data for that month>
2010-09-01 00:00:00.000 <data for that month> <data for that month>
2010-10-01 00:00:00.000 <data for that month> <data for that month>
2010-11-01 00:00:00.000 <data for that month> <data for that month>
2010-12-01 00:00:00.000 122 63
我遇到的问题是,当我执行查询时,我会给出每个月重复的值列表,例如128将重复6次,然后是下一个月值的另一个值重复6次,等等。哎呀!
答案 0 :(得分:1)
您需要更改返回月份的方式。在结果集中,您将返回trtm。[Date],但是左键连接到trtm,因此可能不存在。
如果始终存在,则将其转换为INNER JOIN并更改另一个LEFT JOIN以包含来自trtm的链接。[Date]至fbr。[Date]。
否则,您需要一个'月'表来返回月份列表,并从日期字段(以及网站上现有的连接)中将其从该连接添加到trtm和fbr