简体   繁体   English

SSRS图:在SQL查询中检索不同日期的值

[英]SSRS graph: Retrieve values from different dates in SQL query

I am using the following query to retrieve snapshot values from 4 different dates: -1 day (latest), -2 days, -3 days and -40 days (not yet implemented). 我正在使用以下查询从4个不同的日期检索快照值:-1天(最新),-2天,-3天和-40天(尚未实施)。

SELECT [SnapshotDate]
      ,[SnapshotKey]
      ,[info1]
      ,[info2]
      ,[info3]
FROM [Database].[dbo].[Values]
WHERE [SnapshotDate] >= DATEADD(day,-3, GETDATE())
AND [SnapshotKey] = 'Some text here'

This results in the following graph: 结果如下图:

在此处输入图片说明

The query is not quite right firstly since it is showing 4 values and should only be showing 3 at this point. 首先查询不是很正确,因为它显示4个值,此时只应显示3个值。 Secondly I would like to show the last snapshot from 40 days ago as shown in the graph. 其次,我想显示40天前的最后一个快照,如图所示。

I have tried a few different queries but have not managed to figure out how to do this properly. 我尝试了一些不同的查询,但没有设法弄清楚如何正确执行此操作。

[SnapshotKey] = SELECT DATEADD(day,-40,getdate())

The above query gives me the correct answer in theory. 以上查询从理论上给了我正确答案。 However, when I use this in my query there is no result. 但是,当我在查询中使用此方法时,没有结果。 I believe this might be due to not having a date conversion or the fact that I'm using "day" in my query. 我相信这可能是由于没有日期转换或我在查询中使用“天”这一事实。 I'm not sure. 我不确定。

Any suggestions? 有什么建议么?

EDIT: 编辑:

I also tried using the following with no luck (no result): 我也尝试使用以下方法,但没有运气(无结果):

CONVERT(date, [SnapshotDate]) = CONVERT(date, DATEADD(day,-40, GETDATE()))

I'm not sure what your date values are but I'm guessing this report was run on 2nd May. 我不确定您的日期值是多少,但是我猜这个报告是在5月2日生成的。 If this is correct then you need to change the range to exclude where the difference in dates is zero. 如果正确,那么您需要更改范围以排除日期差为零的地方。 Personally I use DATEDIFF in situations like this as it's easier to visualise for me. 就我个人而言,在这种情况下我会使用DATEDIFF ,因为它对我来说更容易可视化。

Try changing the where clause to something like this. 尝试将where子句更改为类似这样的内容。

WHERE (DATEDIFF(day, getdate(),[SnapshotDate]) BETWEEN -3 AND -1
    OR DATEDIFF(day, getdate(), [SnapshotDate]) = -40)
    AND [SnapshotKey] = 'Some text here'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM