简体   繁体   中英

How to use multiple conditional statements using SQL in Visual Studio SSRS 2008

I am trying to pull calculated data out of a SQL database using Visual Studio SSRS 2008. I am having problems trying to use conditionals in my select/from/where statements. One example of what I'm trying to do is to find the total number of failures in a 24 hour period(from 10:00am yesterday to 10:00am today) from samples taken every 15 minutes. I'm reporting on a gas plant that has gas running through valves when they are open. Gas that goes trough a particular open valve gets ignited in a combustion chamber. When this happens, a failure occurs if the valve is open and the temperature in the combustion chamber is less than 1400 degrees. I currently have a query that gives me data for the valve status and the combustion chamber temp every 15 minutes. As I said, I only care about when the valve has a status of 2, which is open, and the combustion chamber temp is < 1400 at the same time. I have copied my current query below as well as a sample of the resulting data. As you can see, the current query does not include my necessary conditionals. I need to add code that looks something like this:

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

If I can't solve this problem within SQL, I'm hoping I can find a way within the Visual Studio SSRS. Thanks in advance for the help!

in my idea and by having no look on your tables....I say If your DateTime is uniq just for those two states try this query although I advise you (and in my idea) it's a little illogical to have join on Datetime below...you can optimize it yourself...but you can use "case when" statement anyway

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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