【问题标题】:Syntax for column values for same period of last year in SSRS reportSSRS 报告中去年同期列值的语法
【发布时间】:2017-03-24 19:07:32
【问题描述】:

我正在生成一份报告,其中我总结了每月的价格和成本值。我想显示相同的定义时间段 - 1 年。我不清楚如何嵌套这个。我会在声明时使用案例吗?

我的可怕例子。

select month, cost, price from table1 where (define months here in SSRS parameter filter)

我想看看:

select month, cost, price, lastyearcost, lastyearprice FROM table1 where (define months here in SSRS parameter filter)

我知道我应该使用 GETDATE() -1 的一些变体,但这会包括运行报告时传递的数据范围参数吗?如何选择成本列应用日期过滤器,然后获取去年该期间的成本结果?

希望这是有道理的?

【问题讨论】:

  • 如果有人在 2016 年 10 月 14 日通过,你会得到 10/14/2015-10/14/2016 的数据吗?另外,是否有一个实际的日期列或只有一个月份和一个年份列?
  • 基本上我想为他们提供日期参数中的年份和月份范围。选择 2017 年和月份范围从 1 到 4。然后显示该期间的成本和价格值,另外显示2016 年 1 - 4 个月的价格和成本值。
  • 好吧,这是有道理的。您的数据源中有 dateColumn 还是只有年月?
  • 参数将作为 year = @fiscalyear 传递,即 2016 年或 2017 年。然后 month = ATmonth 将是日历年中所有十二个月的列表。
  • 我已加入我的日期维度表。所以就日期而言,我有所有可用的选项

标签: date reporting-services sql-server-2012


【解决方案1】:

一种方法是使用 DATEPART() 并进行正确的嵌套和配对。

select 
    month, 
    cost, 
    price, 
    lastyearcost, 
    lastyearprice 
FROM table1 
where 
    --this is limiting the data to the year passed in, and the previous year
    (datepart(year,DateColumn) = @YearParam or datepart(year,DateColumn) = @YearParam-1)
    and 
    --this is limiting the months to the two parameters you pass in as integers
    (datepart(month,DateColumn) >= @minMonthParam and datepart(month,DateColumn) <= @maxMonthParam)

测试数据

见 DEMO HERE

declare @table1 table (DateColumn date)
insert into @table1 (DateColumn)
values
('1/1/2016'),
('2/1/2016'),
('3/1/2016'),
('4/1/2016'),
('5/1/2016'),
('6/1/2016'),
('7/1/2016'),
('8/1/2016'),
('9/1/2016'),
('10/1/2016'),
('11/1/2016'),
('12/1/2016'),
('1/1/2017'),
('2/1/2017'),
('3/1/2017'),
('4/1/2017'),
('5/1/2017')

declare @YearParam int = 2017
declare @minMonthParam int = 2
declare @maxMonthParam int = 5

select 
    DateColumn
FROM @table1 
where 
    (datepart(year,DateColumn) = @YearParam or datepart(year,DateColumn) = @YearParam-1)
    and 
    (datepart(month,DateColumn) >= @minMonthParam and datepart(month,DateColumn) <= @maxMonthParam)

【讨论】:

  • 有没有办法在 select 中内联而不是作为 where 子句?在通过 SSRS 选择之前,我显然可以创建一个视图并暂存这些数据。那么,如果我这样做了,我该如何构造与 select 子句内联的语法呢?
  • @JoeResler,这会产生不同的结果......并且不会限制返回的数据集,它只会将不满足条件的行设为 NULL。通常我为此编写一个存储过程,它接受我的 SSRS 参数,而不是在 RDL 本身中包含 TSQL。所以,你的 RDL 中的数据集是一个存储过程,在我看来给你更大的灵活性
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多