尝试为 s-s-rS 报告创建动态日期值数据集(用户选择给定日期)

Posted

技术标签:

【中文标题】尝试为 s-s-rS 报告创建动态日期值数据集(用户选择给定日期)【英文标题】:Trying to create a Dynamic Date value Dataset for s-s-rS reporting (User select given dates) 【发布时间】:2017-05-25 16:47:28 【问题描述】:

所以我需要创建一个嵌入代码中的假/仿表。它将有一个设置为 VALUE/LABEL 样式的日期列表。将值上的动态日期和描述(今天的日期、昨天的日期、上周一、每月的第一天等)作为标签。

这就是我想要的样子

Value      Label
5/25/2017  Today
5/24/2017  Yesterday
5/15/2017  Prev Monday
5/01/2017  First of Month

这是它目前的显示方式(我不想要)

Today   Yesterday   2_Days_ago
2017-05-25 00:00:00.000 2017-05-24 00:00:00.000 2017-05-23 00:00:00.000

目的是在 s-s-rS 的订阅报告中创建一组动态日期以供选择。我将使用这个模拟表作为报告服务器上的存储数据集,以链接到一组 To 日期和 From 日期。 IE 日期从 [昨天] 到日期到 [今天]。等等

我找到了这个参考网站 https://www.mssqltips.com/sqlservertip/3421/add-a-date-range-dataset-in-sql-server-reporting-services/ 这是一个很好的开始,但我想更进一步,把它变成一个像数据集一样的表格。许多报告需要不同的订阅日期。 IE 某些人每天都想要它,日期范围是 [昨天] 到 [今天]。有些人需要每周一次,[上一个星期一] 到 [上一个星期六],而另一些人则希望每月一次 [上一个 BOM] 到 [上一个 EOM] 等等。 拥有一个可以为订阅报告动态更改的日期数据集将有助于避免创建具有不同默认日期的多个版本的报告。

这是我所知道的。我不知道如何让标签以正确的顺序出现在日期旁边。

SELECT convert(datetime,convert(varchar(8),getdate(),112)) as [Today] UNION
SELECT convert(datetime,convert(varchar(8),DATEADD(d,-1,GETDATE()),112)) as [Yesterday] UNION
SELECT convert(datetime,convert(varchar(8),DATEADD(d,-2,GETDATE()),112)) AS [2_Days_ago] UNION
SELECT DATEADD(wk,DATEDIFF(wk,0,GETDATE()),0) AS [ThisWeek_Mon] UNION
SELECT DATEADD(wk,DATEDIFF(wk,0,GETDATE()),4) AS [ThisWeek_Fri] UNION
SELECT DATEADD(wk,DATEDIFF(wk,0,GETDATE()),5) AS [ThisWeek_Sat] UNION
SELECT DATEADD(wk,DATEDIFF(wk,0,GETDATE()),6) AS [ThisWeek_Sun] UNION
SELECT DATEADD(wk,DATEDIFF(wk,7,GETDATE()),0) AS [PreviousWeek_Mon] UNION
SELECT DATEADD(wk,DATEDIFF(wk,7,GETDATE()),4) AS [PreviousWeek_Fri] UNION
SELECT DATEADD(wk,DATEDIFF(wk,7,GETDATE()),0) AS [PreviousWeek_Sat] UNION
SELECT DATEADD(wk,DATEDIFF(wk,7,GETDATE()),6) AS [PreviousWeek_Sun] UNION
SELECT DATEADD(mm,DATEDIFF(mm,0,GETDATE()),0) AS [BOM FirstDayOfThisMonth] UNION
SELECT DATEADD(ms,-3,DATEADD(mm,0,DATEADD(mm,DATEDIFF(mm,0,GETDATE())+1,0))) AS [EOM_LastDayOfThisMonth] UNION
SELECT DATEADD(mm,-1,DATEADD(mm,DATEDIFF(mm,0,GETDATE()),0)) AS [Prev_BOM_FirstDayOfLastMonth] UNION
SELECT DATEADD(ms,-3,DATEADD(mm,0,DATEADD(mm,DATEDIFF(mm,0,GETDATE()),0))) AS [Prev_EOM_LastDayOfLastMonth] UNION
SELECT DATEADD(q,DATEDIFF(q,0,GETDATE())-1,0) AS [BoPQ FirstDayOfLastFullQuarter] UNION
SELECT DATEADD(D, 0, DATEDIFF(D, 0, DATEADD(s,-1,DATEADD(q,1,DATEADD(q,DATEDIFF(q,0,GETDATE())-1,0))))) AS [EoPQ_LastDayOfLastFullQuarter] UNION
SELECT DATEADD(q,DATEDIFF(q,0,GETDATE()),0) AS [BoCQ_FirstDayOfThisQuarter] UNION
SELECT DATEADD(D, 0, DATEDIFF(D, 0, DATEADD(s,-1,DATEADD(q,2,DATEADD(q,DATEDIFF(q,0,GETDATE())-1,0))))) AS [BoCQ_LastDayOfThisQuarter] UNION
SELECT DATEADD(yy,DATEDIFF(yy,0,GETDATE()),0) AS [BoY_FirstDayOfThisYear] UNION
SELECT DATEADD(ms,-3,DATEADD(yy,0,DATEADD(yy,DATEDIFF(yy,0,GETDATE())+1,0))) AS [EoY_LastDayOfThisYear] UNION
SELECT DATEADD(yy,-1,DATEADD(yy,DATEDIFF(yy,0,GETDATE()),0)) AS [Prev_BoY_FirstDayOfLastYear] UNION
SELECT DATEADD(ms,-3,DATEADD(yy,0,DATEADD(yy,DATEDIFF(yy,0,GETDATE()),0))) AS [Prev_EoY_LastDayOfLastYear]`

【问题讨论】:

看看这个作为您尝试的替代方法:Answer 就我所见,这是一个不错的选择。不过我想看看有没有更好的方法。 好的,所以我能够在一些巧妙的帮助下解决这个问题。 SELECT DateTable.Value ,DateTable.Label FROM ( SELECT convert(datetime,convert(varchar(8),getdate(),112)) AS [Value], 'Today' AS [Label], 10 AS [SortOrder] UNION SELECT convert (datetime,convert(varchar(8),DATEADD(d,-1,GETDATE()),112)) AS [Value], 'Yesterday' AS [Label], 20 AS [SortOrder] UNION SELECT convert(datetime,convert (varchar(8),DATEADD(d,-2,GETDATE()),112)) AS [Value], '2 Days ago' AS [Label], 30 AS [SortOrder] ) AS [DateTable] ORDER BY [SortOrder ] 【参考方案1】:

感谢您提供的代码。

基于此,我只是做了一些修改来创建这个数据集。希望这对你有用。

SELECT convert(datetime,convert(varchar(8),getdate(),112)) as Date, 'Today' 

as Label, 0 AS Sort UNION
SELECT convert(datetime,convert(varchar(8),DATEADD(d,-1,GETDATE()),112)) , 'Yesterday', 1 UNION
SELECT convert(datetime,convert(varchar(8),DATEADD(d,-2,GETDATE()),112)) , '2_Days_ago', 2 UNION
SELECT DATEADD(wk,DATEDIFF(wk,0,GETDATE()),0), 'ThisWeek_Mon', 3 UNION
SELECT DATEADD(wk,DATEDIFF(wk,0,GETDATE()),4) , 'ThisWeek_Fri', 4 UNION
SELECT DATEADD(wk,DATEDIFF(wk,0,GETDATE()),5) , 'ThisWeek_Sat', 5 UNION
SELECT DATEADD(wk,DATEDIFF(wk,0,GETDATE()),6) , 'ThisWeek_Sun', 6 UNION
SELECT DATEADD(wk,DATEDIFF(wk,7,GETDATE()),0) , 'PreviousWeek_Mon', 7 UNION
SELECT DATEADD(wk,DATEDIFF(wk,7,GETDATE()),4) , 'PreviousWeek_Fri', 8 UNION
SELECT DATEADD(wk,DATEDIFF(wk,7,GETDATE()),0) , 'PreviousWeek_Sat', 9 UNION
SELECT DATEADD(wk,DATEDIFF(wk,7,GETDATE()),6) , 'PreviousWeek_Sun', 10 UNION
SELECT DATEADD(mm,DATEDIFF(mm,0,GETDATE()),0) , 'BOM FirstDayOfThisMonth', 11 UNION
SELECT DATEADD(ms,-3,DATEADD(mm,0,DATEADD(mm,DATEDIFF(mm,0,GETDATE())+1,0))), 'EOM_LastDayOfThisMonth', 12 UNION
SELECT DATEADD(mm,-1,DATEADD(mm,DATEDIFF(mm,0,GETDATE()),0)), 'Prev_BOM_FirstDayOfLastMonth', 13 UNION
SELECT DATEADD(ms,-3,DATEADD(mm,0,DATEADD(mm,DATEDIFF(mm,0,GETDATE()),0))), 'Prev_EOM_LastDayOfLastMonth', 14 UNION
SELECT DATEADD(q,DATEDIFF(q,0,GETDATE())-1,0), 'BoPQ FirstDayOfLastFullQuarter', 15 UNION
SELECT DATEADD(D, 0, DATEDIFF(D, 0, DATEADD(s,-1,DATEADD(q,1,DATEADD(q,DATEDIFF(q,0,GETDATE())-1,0))))), 'EoPQ_LastDayOfLastFullQuarter' , 16 UNION
SELECT DATEADD(q,DATEDIFF(q,0,GETDATE()),0) , 'BoCQ_FirstDayOfThisQuarter' , 17 UNION
SELECT DATEADD(D, 0, DATEDIFF(D, 0, DATEADD(s,-1,DATEADD(q,2,DATEADD(q,DATEDIFF(q,0,GETDATE())-1,0))))), 'BoCQ_LastDayOfThisQuarter', 18 UNION
SELECT DATEADD(yy,DATEDIFF(yy,0,GETDATE()),0) , 'BoY_FirstDayOfThisYear', 19 UNION
SELECT DATEADD(ms,-3,DATEADD(yy,0,DATEADD(yy,DATEDIFF(yy,0,GETDATE())+1,0))) , 'EoY_LastDayOfThisYear', 20 UNION
SELECT DATEADD(yy,-1,DATEADD(yy,DATEDIFF(yy,0,GETDATE()),0)), 'Prev_BoY_FirstDayOfLastYear', 22 UNION
SELECT DATEADD(ms,-3,DATEADD(yy,0,DATEADD(yy,DATEDIFF(yy,0,GETDATE()),0))), 'Prev_EoY_LastDayOfLastYear', 23

ORDER BY Sort

如果您需要任何进一步的说明/信息,请告诉我。

【讨论】:

【参考方案2】:

编辑! 仅供参考,毕竟以下修复无法满足我的需求。 s-s-rS 将像 [Yesterday] 这样的“订阅电子邮件日期”存储为 VALUE,而不是 LABEL。所以昨天不是我想的动态日期,而是像 2017 年 5 月 30 日这样的设定值。从理论上讲,它会很好用,但这绝对不是我想要的解决方法。回到绘图板。

我将代码保存为我失败的试用和错误日志。哈哈

好的,我在其他论坛帖子上得到了一些建议,这些建议使我将工会合并为 3 个专栏。这解决了我的问题。我将以下代码放在共享数据集中。

所以有 Value、Label 和 SortOrder

SELECT
    DateTable.Value
    ,DateTable.Label
FROM
(
SELECT convert(datetime,convert(varchar(8),getdate(),112)) AS [Value], 'Today' AS [Label], 10 AS [SortOrder] UNION
SELECT convert(datetime,convert(varchar(8),DATEADD(d,-1,GETDATE()),112)) AS [Value], 'Yesterday' AS [Label], 20 AS [SortOrder] UNION
SELECT convert(datetime,convert(varchar(8),DATEADD(d,-2,GETDATE()),112)) AS [Value], '2 Days ago' AS [Label], 30 AS [SortOrder] UNION
SELECT convert(datetime,convert(varchar(8),DATEADD(d,-3,GETDATE()),112)) AS [Value], '3 Days ago' AS [Label], 40 AS [SortOrder] UNION
SELECT DATEADD(wk,DATEDIFF(wk,0,GETDATE()),0) AS [Value], 'ThisWeek Mon' AS [Label], 50 AS [SortOrder] UNION
SELECT DATEADD(wk,DATEDIFF(wk,0,GETDATE()),1) AS [Value], 'ThisWeek Tue' AS [Label], 51 AS [SortOrder] UNION
SELECT DATEADD(wk,DATEDIFF(wk,0,GETDATE()),2) AS [Value], 'ThisWeek Wed' AS [Label], 52 AS [SortOrder] UNION
SELECT DATEADD(wk,DATEDIFF(wk,0,GETDATE()),3) AS [Value], 'ThisWeek Thur' AS [Label], 53 AS [SortOrder] UNION
SELECT DATEADD(wk,DATEDIFF(wk,0,GETDATE()),4) AS [Value], 'ThisWeek Fri' AS [Label], 54 AS [SortOrder] UNION
SELECT DATEADD(wk,DATEDIFF(wk,0,GETDATE()),5) AS [Value], 'ThisWeek Sat' AS [Label], 55 AS [SortOrder] UNION
SELECT DATEADD(wk,DATEDIFF(wk,0,GETDATE()),6) AS [Value], 'ThisWeek Sun' AS [Label], 56 AS [SortOrder] UNION
SELECT DATEADD(wk,DATEDIFF(wk,7,GETDATE()),0) AS [Value], 'PreviousWeek Mon' AS [Label], 60 AS [SortOrder] UNION
SELECT DATEADD(wk,DATEDIFF(wk,7,GETDATE()),1) AS [Value], 'PreviousWeek Tue' AS [Label], 61 AS [SortOrder] UNION
SELECT DATEADD(wk,DATEDIFF(wk,7,GETDATE()),2) AS [Value], 'PreviousWeek Wed' AS [Label], 62 AS [SortOrder] UNION
SELECT DATEADD(wk,DATEDIFF(wk,7,GETDATE()),3) AS [Value], 'PreviousWeek Thur' AS [Label], 63 AS [SortOrder] UNION
SELECT DATEADD(wk,DATEDIFF(wk,7,GETDATE()),4) AS [Value], 'PreviousWeek Fri' AS [Label], 64 AS [SortOrder] UNION
SELECT DATEADD(wk,DATEDIFF(wk,7,GETDATE()),5) AS [Value], 'PreviousWeek Sat' AS [Label], 65 AS [SortOrder] UNION
SELECT DATEADD(wk,DATEDIFF(wk,7,GETDATE()),6) AS [Value], 'PreviousWeek Sun' AS [Label], 66 AS [SortOrder] UNION
SELECT DATEADD(mm,DATEDIFF(mm,0,GETDATE()),0) AS [Value], 'Month First Day Of This Month' AS [Label], 70 AS [SortOrder] UNION
SELECT DATEADD(ms,-3,DATEADD(mm,0,DATEADD(mm,DATEDIFF(mm,0,GETDATE())+1,0))) AS [Value], 'Month Last Day Of This Month' AS [Label], 80 AS [SortOrder] UNION
SELECT DATEADD(mm,-1,DATEADD(mm,DATEDIFF(mm,0,GETDATE()),0)) AS [Value], 'Month-Prev First Day Of Last Month' AS [Label], 90 AS [SortOrder] UNION
SELECT DATEADD(ms,-3,DATEADD(mm,0,DATEADD(mm,DATEDIFF(mm,0,GETDATE()),0))) AS [Value], 'Month-Prev Last Day Of Last Month' AS [Label], 100 AS [SortOrder] UNION
SELECT DATEADD(q,DATEDIFF(q,0,GETDATE()),0) AS [Value], 'Quarter FirstDayOfThisQuarter' AS [Label], 110 AS [SortOrder] UNION
SELECT DATEADD(D, 0, DATEDIFF(D, 0, DATEADD(s,-1,DATEADD(q,2,DATEADD(q,DATEDIFF(q,0,GETDATE())-1,0))))) AS [Value], 'Quarter LastDayOfThisQuarter' AS [Label], 120 AS [SortOrder] UNION
SELECT DATEADD(q,DATEDIFF(q,0,GETDATE())-1,0) AS [Value], 'Quarter-Prev FirstDayOfLastFullQuarter' AS [Label], 130 AS [SortOrder] UNION
SELECT DATEADD(D, 0, DATEDIFF(D, 0, DATEADD(s,-1,DATEADD(q,1,DATEADD(q,DATEDIFF(q,0,GETDATE())-1,0))))) AS [Value], 'Quarter-Prev LastDayOfLastFullQuarter' AS [Label], 140 AS [SortOrder] UNION
SELECT DATEADD(yy,DATEDIFF(yy,0,GETDATE()),0) AS [Value], 'Year FirstDayOfThisYear' AS [Label], 150 AS [SortOrder] UNION
SELECT DATEADD(ms,-3,DATEADD(yy,0,DATEADD(yy,DATEDIFF(yy,0,GETDATE())+1,0))) AS [Value], 'Year LastDayOfThisYear' AS [Label], 160 AS [SortOrder] UNION
SELECT DATEADD(yy,-1,DATEADD(yy,DATEDIFF(yy,0,GETDATE()),0)) AS [Value], 'Year-Prev FirstDayOfLastYear' AS [Label], 170 AS [SortOrder] UNION
SELECT DATEADD(ms,-3,DATEADD(yy,0,DATEADD(yy,DATEDIFF(yy,0,GETDATE()),0))) AS [Value], 'Year-Prev LastDayOfLastYear' AS [Label], 180 AS [SortOrder]
) AS [DateTable]
ORDER BY
[SortOrder]`

我将 SortOrder 值保留为 10 位增量,以便以后可以插入其他日期参数。我用它运行了我的第一份订阅报告,它就像一个魅力。 1 份报告现在可以有多个日期参数订阅。

【讨论】:

以上是关于尝试为 s-s-rS 报告创建动态日期值数据集(用户选择给定日期)的主要内容,如果未能解决你的问题,请参考以下文章

Report Builder 3.0 - 如何使用大型数据集运行此报告?

如何在 s-s-rS 报告中设置动态轴单位?

s-s-rS 报告为 Join 表达式提供错误

从 s-s-rS 中的表达式将参数传递给数据集

s-s-rS 报告错误:处理数据集的命令文本表达式时出错

s-s-rS 13 (SQL 2016) 报告缓存问题