所以我需要的是创建一个嵌入代码的假/模仿表。它将以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
目的是创建一组动态日期,以便在SSRS的订阅报告中进行选择。我将使用此模拟表作为报告服务器上的存储数据集,以链接到一组To日期和From日期。 IE日期从[昨天]到日期到[今天]。等
我找到了这个参考网站 https://www.mssqltips.com/sqlservertip/3421/add-a-date-range-dataset-in-sql-server-reporting-services/ 这是一个很好的开始,但我希望通过使这个表像数据集更进一步。许多报告要求订阅日期各不相同。 IE某些人每天都想要它,日期范围是[昨天]到[今天]。有些人想要它每周,[上周一]到[上周六],其他人想要每月一次[上一个物料清单]到[上一个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]`
答案 0 :(得分:0)
感谢您提供的代码。
基于此,我刚刚做了一些修改来创建这个数据集。希望这对你有用。
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
如果您需要任何进一步的澄清/信息,请与我们联系。
答案 1 :(得分:0)
修改<!/强> 仅供参考,以下修复程序毕竟不能满足我的需求。 SSRS将[昨天]的“订阅电子邮件日期”存储为不作为LABEL而是作为值。所以昨天不像我想的那样是动态日期,而是像5/30/2017那样的设定价值。从理论上讲,它会很好用,但这绝对不是我想要的修复。回到绘图板。
我将代码保存为我失败的试用和错误日志。哈哈
好的,我在其他论坛帖子上得到了一些建议,这使我将工会合并为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]`
我以10位数的增量离开了SortOrder值,以便我可以在以后插入其他日期参数。我用它运行了我的第一个订阅报告,它就像一个魅力。 1报告现在可以有多个日期参数订阅。