1

我正在尝试使用 Visual Studio SSRS 2008 从 SQL 数据库中提取计算数据。尝试在我的 select/from/where 语句中使用条件时遇到问题。我正在尝试做的一个例子是从每 15 分钟采集的样本中找出 24 小时内(从昨天上午 10:00 到今天上午 10:00)的故障总数。我正在报道一家天然气厂,当阀门打开时,气体会流过阀门。通过特定打开的阀门的气体在燃烧室中被点燃。发生这种情况时,如果阀门打开且燃烧室温度低于 1400 度,则会发生故障。我目前有一个查询,每 15 分钟为我提供一次阀门状态和燃烧室温度的数据。正如我所说,我只关心阀门的状态为 2,即打开,同时燃烧室温度<1400。我在下面复制了我当前的查询以及结果数据的样本。如您所见,当前查询不包括我的必要条件。我需要添加看起来像这样的代码:

COUNT (IF RTO_FCV601_SSTS=2 AND RTO_COMB1TEMP < 1400)

SET NOCOUNT ON
DECLARE @StartDate DateTime
DECLARE @EndDate DateTime
SET @StartDate = DateAdd(HOUR,-24,GetDate())
SET @EndDate = GetDate()
SET NOCOUNT OFF
SELECT temp.TagName ,DateTime,Value
From (
SELECT  * 
 FROM History
 WHERE History.TagName IN ('RTO_FCV601_STS','RTO_COMB1TEMP')
 AND wwRetrievalMode = 'Cyclic'
 AND wwCycleCount = 96
 AND wwVersion = 'Latest'
 AND DateTime >= @StartDate
 AND DateTime <= @EndDate) temp
 WHERE temp.StartDateTime >= @StartDate

Resulting data:
TagName         DateTime                    Value
RTO_FCV601_STS  2013-04-08 15:44:40.6600000 2
RTO_COMB1TEMP   2013-04-08 15:44:40.6600000 1663
RTO_FCV601_STS  2013-04-08 15:59:50.1340000 2
RTO_COMB1TEMP   2013-04-08 15:59:50.1340000 1604
RTO_FCV601_STS  2013-04-08 16:14:59.6080000 2
RTO_COMB1TEMP   2013-04-08 16:14:59.6080000 1557
RTO_FCV601_STS  2013-04-08 16:30:09.0810000 0
RTO_COMB1TEMP   2013-04-08 16:30:09.0810000 1704
RTO_FCV601_STS  2013-04-08 16:45:18.5550000 2
RTO_COMB1TEMP   2013-04-08 16:45:18.5550000 1576
RTO_FCV601_STS  2013-04-08 17:00:28.0290000 2
RTO_COMB1TEMP   2013-04-08 17:00:28.0290000 1645

如果我不能在 SQL 中解决这个问题,我希望我能在 Visual Studio SSRS 中找到一种方法。在此先感谢您的帮助!

4

1 回答 1

0

在我的想法中并且没有看你的表格......我说如果你的 DateTime 仅适用于这两个州,请尝试这个查询,尽管我建议你(并且在我的想法中)加入下面的 Datetime 有点不合逻辑...您可以自己优化它...但无论如何您都可以使用“case when”语句

SET NOCOUNT ON
DECLARE @StartDate DateTime
DECLARE @EndDate DateTime
SET @StartDate = DateAdd(HOUR,-24,GetDate())
SET @EndDate = GetDate()
SET NOCOUNT OFF
SELECT Sum(case when temp.Value = 2 and temp2.value< 1400 then 1 else 0 end ) 
AS 'failureCount'
From (
 SELECT  *
 FROM History
 WHERE History.TagName IN ('RTO_FCV601_STS')
 AND wwRetrievalMode = 'Cyclic'
 AND wwCycleCount = 96
 AND wwVersion = 'Latest'
 AND DateTime >= @StartDate
AND DateTime <= @EndDate)as temp 
join
 (SELECT  * FROM History
   WHERE History.TagName IN ('RTO_COMB1TEMP')
   AND wwRetrievalMode = 'Cyclic'
   AND wwCycleCount = 96
   AND wwVersion = 'Latest'
   AND DateTime >= @StartDate
   AND DateTime <= @EndDate)as temp2 on temp.DateTime = temp2.DateTime

WHERE temp.StartDateTime >= @StartDate and temp2.StartDateTime >= @StartDate
于 2013-04-09T22:05:10.020 回答