SSRS报告中的月份和年份选项参数

时间:2016-08-20 05:04:29

标签: ssrs-2012

- 你好,

我想知道是否有用户可以选择的方式 选择月份(mm / dd / yyyy)或年份(yyyy)的日期范围......(例如使用下拉列表)

例如......

如果用户在预览模式下从下拉列表中选择“DateByMonth”,则用户可以按月输入日期范围。

开始日期: 2014年1月1日结束日期: 2016年1月1日

输出应该是这样的。

    1/2014  | 2/2014 | 3/2014 | .... | 10/2015 | .... | 1/2016|
qty   1         6        9                3               6
rev   2         3        2                12              0 

如果用户在预览模式下从下拉列表中选择“DateByYear”,则用户可以输入年份中的日期范围。

开始日期: 2014 结束日期: 2016

输出应该是这样的。

    2014 | 2015 | 2016
qty 16     3      6
rev 7      12     0

您可能有更好的解决方案。

感谢。

1 个答案:

答案 0 :(得分:2)

我曾经用参数制作报告选择显示类型(每日,每周,每月,每年或每季度)

我在DataSet中使用Expression来进行动态查询。

这是我的DataSet中的一些表达式

="DECLARE @paramSdate AS DATE = @Strdate "&
"DECLARE @paramNdate AS DATE = @endDate "&
Switch(
Parameters!displayType.Value = "D",
    .... Generate Daily into temp table .....
Parameters!displayType.Value = "W" ,
    .... Generate weekly into temp table ......
Parameters!displayType.Value = "M" ,
    .... Generate monthly into temp table ......
Parameters!displayType.Value = "Y",
    .... Generate yearly into temp table ......
Parameters!displayType.Value = "Q",
    .... Generate quarter into temp table .......
)

我使用displayType参数来确定使用什么查询。

它只是指南而非完整代码。

更新1

好的..感谢脚本创建表和数据

您希望结果必须使用UNPIVOT

我每月和每年进行样本2查询

<强>每月

DECLARE @paramSdate AS DATE = CAST('2015-02-01' AS DATE)
DECLARE @paramNdate AS DATE = CAST('2015-09-30' AS DATE)
DECLARE @temp AS DATE = @paramSDate

CREATE TABLE #tmp (label VARCHAR(10),m INT,yy INT)
 -- Monthly
WHILE @temp < @paramNdate
BEGIN
    INSERT INTO #tmp
        SELECT FORMAT(@temp,'MM/yyyy'),MONTH(@temp),YEAR(@temp)
    SET @temp = DATEADD(MONTH,1,@temp)
END

;WITH CTE 
AS
(
    SELECT upv.col
        ,upv.yy
        ,upv.m
        ,upv.value
    FROM (
        SELECT t.label
            ,t.m
            ,t.yy
            ,ISNULL(val.Qty,0) AS [Qty]
            ,ISNULL(val.Rev,0) AS [Rev]
        FROM #tmp t
        LEFT JOIN (
            SELECT CAST(Rev AS INT) AS [Rev]
                ,CAST(Qty AS INT) AS [Qty]
                ,MONTH(DateDA) AS [mm]
                ,YEAR(DateDA) AS [yyy]
            FROM Main_Table m with(nolock)
            WHERE m.DateDA BETWEEN  CAST('2/13/2015' AS DATE) AND  CAST('9/23/2015' AS DATE)
        ) val ON val.mm = t.m AND val.yyy = t.yy
    ) src
    UNPIVOT
    (
        value
        FOR col IN ([Rev],[Qty])
    ) upv
)

SELECT CTE.col
    ,CAST(CTE.m AS varchar(10)) + '/' + CAST(CTE.yy AS varchar(10)) AS [label]
    ,CTE.value
FROM CTE
ORDER BY CTE.yy,CTE.m

DROP TABLE #tmp

说明:首先,我创建临时表以生成日期范围记录然后使用UNPIVOT得到如下图所示的结果

unpivotResult

最后,我在SSRS中使用分组

design_layout

运行

时会得到这个结果

Result

并且每年我查询是相同的每月

<强>每年

DECLARE @paramSdate AS DATE = CAST('2015-02-01' AS DATE)
DECLARE @paramNdate AS DATE = CAST('2017-09-30' AS DATE)
DECLARE @temp AS DATE = @paramSDate

CREATE TABLE #tmp (yy INT)
 -- Yearly
WHILE YEAR(@temp) <= YEAR(@paramNdate)
BEGIN
    INSERT INTO #tmp
        SELECT YEAR(@temp)
    SET @temp = DATEADD(YEAR,1,@temp)
END

;WITH CTE 
AS
(
    SELECT upv.col
        ,upv.value
        ,upv.yy
    FROM (
        SELECT t.yy
            ,ISNULL(val.Qty,0) AS [Qty]
            ,ISNULL(val.Rev,0) AS [Rev]
        FROM #tmp t
        LEFT JOIN (
            SELECT CAST(Rev AS INT) AS [Rev]
                ,CAST(Qty AS INT) AS [Qty]
                ,YEAR(DateDA) AS [yy]
            FROM Main_Table m with(nolock)
            WHERE m.DateDA BETWEEN  CAST('2/13/2015' AS DATE) AND  CAST('9/23/2017' AS DATE)
        ) val ON val.yy = t.yy
    ) src
    UNPIVOT
    (
        value
        FOR col IN ([Rev],[Qty])
    ) upv
)


SELECT CTE.col
    ,CTE.yy AS [label]
    ,CTE.value
FROM CTE 

DROP TABLE #tmp

然后,如果您想使用参数作为用户选择显示类型,您可以使用这样的查询

DECLARE @displayType NVARCHAR(100) = @param

IF @displayType = 'DateByMonth' 
BEGIN
    ..... Code for Monthly ......
END
ELSE
BEGIN
    ..... Code for Yearly ......
END

希望有所帮助。