我正在制作SSRS报告,其参数为“Quarter1”,“Quarter2”,“Quarter3”和“Quarter4”,我有一个需要获取用户值并获取数据的查询对于该季度,如果选择第1季,我的查询将如此。我正在使用VS 2010 BIDS for SSRS和SQL 2012 for database
Select
CASE WHEN MONTH(DateGenerated) <= 3 THEN '1Q/'
WHEN MONTH(DateGenerated) > 3 and MONTH(DateGenerated) <= 6 THEN '2Q/'
WHEN MONTH(DateGenerated) > 6 and MONTH(DateGenerated) <= 9 THEN '3Q/'
WHEN MONTH(DateGenerated) > 9 and MONTH(DateGenerated) <= 12 THEN '4Q/' END
as GenDate, Notes, Equation
from SubTask
where Year(DateGenerated) = @year and MONTH(DateGenerated) in (@Quarter)
在SSRS中执行此操作时,我无法将第(1,第2和第3)个值作为Quarter1的值。有没有办法让我失踪。感谢帮助..
答案 0 :(得分:0)
来自SSRS的参数将以逗号分隔的字符串形式出现,因此我们需要一个函数来分割值。
例如,如果您的参数是Quarter ...那么您的SQL看起来像
Create Procedure xyz
@Quarter varchar(50) = null
as BEGIN
Select
CASE WHEN MONTH(DateGenerated) <= 3 THEN '1Q/'
WHEN MONTH(DateGenerated) > 3 and MONTH(DateGenerated) <= 6 THEN '2Q/'
WHEN MONTH(DateGenerated) > 6 and MONTH(DateGenerated) <= 9 THEN '3Q/'
WHEN MONTH(DateGenerated) > 9 and MONTH(DateGenerated) <= 12 THEN '4Q/' END
as GenDate, Notes, Equation
from SubTask
where Year(DateGenerated) = @year and MONTH(DateGenerated)
in (Select Value From dbo.FNSplit(@Quarter,','))
功能是......
CREATE FUNCTION [dbo].[FnSplit]
(
@List nvarchar(max),
@SplitOn nvarchar(5)
)
RETURNS @RtnValue table
(
Id int identity(1,1),
Value varchar(max)
)
AS
BEGIN
While (Charindex(@SplitOn,@List)>0)
Begin
Insert Into @RtnValue (value)
Select
Value = ltrim(rtrim(Substring(@List,1,Charindex(@SplitOn,@List)-1)))
Set @List = Substring(@List,Charindex(@SplitOn,@List)+len(@SplitOn),len(@List))
End
Insert Into @RtnValue (Value)
Select Value = ltrim(rtrim(@List))
Return
END
GO