[英]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. 我试图使用Visual Studio SSRS 2008从SQL数据库中提取计算的数据。尝试在select / from / where语句中使用条件条件时遇到问题。 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. 我要尝试执行的一个示例是从每15分钟抽取的样本中找出24小时内的故障总数(从昨天的10:00 am到今天的10:00 am)。 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. 发生这种情况时,如果阀门打开并且燃烧室中的温度低于1400度,则会发生故障。 I currently have a query that gives me data for the valve status and the combustion chamber temp every 15 minutes. 我目前有一个查询,每15分钟会给我有关阀状态和燃烧室温度的数据。 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. 如我所说,我只关心阀门的状态为2(打开),并且燃烧室温度同时小于1400。 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. 如果无法在SQL中解决此问题,希望我可以在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 在我的想法中,并且不看您的表。...我说,如果仅针对这两个州,您的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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.