简体   繁体   English

如何在Visual Studio SSRS 2008中使用SQL将平均值函数与多个条件语句一起使用

[英]How to use the average function with 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 AVERAGE number of BTUs sold in a 24 hour period(from 10:00am yesterday to 10:00am today) from samples taken every 5 minutes. 我要尝试执行的一个示例是,从每5分钟采集的样本中找出24小时内(从昨天上午10:00到今天上午10:00)销售的BTU的平均数量。 I'm reporting on a gas plant that has a gas flow value when running through a valve when it's open. 我报告的是一家天然气厂,它在打开时通过阀门运行时具有燃气流量值。 Gas that flows through a particular open valve has a BTU value that I need to average. 流经特定打开阀的气体的BTU值需要平均。 I have copied my current query below that has failures, but I think it's close to what it needs to be. 我在下面复制了当前的查询,但该查询失败了,但我认为它已经接近需要的值。 The tagname that I want to average the value of is DTE_BTU if the other 2 tags mentioned below are > 0. 如果下面提到的其他2个标签> 0,则我想要平均其值的标签名是DTE_BTU。

SET NOCOUNT ON
DECLARE @StartDate DateTime
DECLARE @EndDate DateTime
SET @StartDate = DateAdd(HOUR,-24,GetDate())
SET @EndDate = GetDate()
SET NOCOUNT OFF
SELECT case when temp.Value > 0 and temp2.value > 0 then AVG(Value where tagname ='DTE_BTU') end AS 'AvgDTEBTU'
From (
SELECT *
FROM History
WHERE History.TagName IN ('PGPOL_ProdVlv')
AND wwRetrievalMode = 'Cyclic'
AND wwCycleCount = 288
AND wwVersion = 'Latest'
AND DateTime >= @StartDate
AND DateTime <= @EndDate)as temp join
(SELECT * FROM History
WHERE History.TagName IN ('DTE_C_SCFM')
AND wwRetrievalMode = 'Cyclic'
AND wwCycleCount = 288
AND wwVersion = 'Latest'
AND DateTime >= @StartDate
AND DateTime <= @EndDate)as temp2 on temp.DateTime = temp2.DateTime
WHERE temp.StartDateTime >= @StartDate and temp2.StartDateTime >= @StartDate

Assuming I understand your information correctly, it seems like you really just need to see if the other two tags exist in the history table for that time period, so you don't necessarily need a case statement. 假设我正确理解了您的信息,似乎您真的只需要查看该时间段内历史记录表中是否存在其他两个标签,因此您不一定需要case语句。

SELECT AVG(Value) as Avg_DTE_BTU
FROM History h
WHERE h.wwRetrievalMode = 'Cyclic'
AND h.wwCycleCount = 288
AND h.wwVersion = 'Latest'
AND h.DateTime >= @StartDate
AND h.DateTime <= @EndDate
AND h.tagname = 'DTE_BTU'
AND EXISTS 
(
  SELECT 1
  FROM History x
  WHERE x.wwRetrievalMode = 'Cyclic'
  AND x.wwCycleCount = 288
  AND x.wwVersion = 'Latest'
  AND x.DateTime >= @StartDate
  AND x.DateTime <= @EndDate
  AND x.tagname = 'PGPOL_ProdVlv'
  AND x.value > 0
)
AND EXISTS 
(
  SELECT 1
  FROM History y
  WHERE y.wwRetrievalMode = 'Cyclic'
  AND y.wwCycleCount = 288
  AND y.wwVersion = 'Latest'
  AND y.DateTime >= @StartDate
  AND y.DateTime <= @EndDate
  AND y.tagname = 'DTE_C_SCFM'
  AND y.value > 0
)

The above should take the average of all DTE_BTU values within the time range if there also exists the other two tag names in the time range and give you one row in return. 如果在该时间范围内还存在其他两个标签名称,则上面的值应取该时间范围内所有DTE_BTU值的平均值,并作为回报给您一行。

I dun know why "add Comment" is off here for me.... anyway friend...I can solve this problem as well but it's a little illogical way... U know what...I think you could have used or created one table for every state and you used a table for every submit and then you put a FK from table for submitting to every tables related to your states.... I meant this : 我不知道为什么“添加评论”对我来说不可用....无论如何,朋友...我也可以解决此问题,但这是一种不合逻辑的方式...你知道吗...我想你可以使用或为每个州创建一个表,然后为每个提交使用一个表,然后将FK从表中提交给与您的州相关的每个表。...我的意思是:

you have Table "Submit" and also... Table "PGPOL_ProdVlv" - Table "DTE_C_SCFM" - Table "DTE_BTU" ...I meant using one table for every state .infact I meant (1 : N) relation 您有表“ Submit”,还有...表“ PGPOL_ProdVlv”-表“ DTE_C_SCFM”-表“ DTE_BTU” ...我的意思是为每个状态使用一个表。事实上,我的意思是(1:N)关系

Table "Submit" 表“提交”
1.NO 1.否
2.Time 2.时间
3.and other columns 3,其他栏目

and then 接着

Table "PGPOL_ProdVlv" 表“ PGPOL_ProdVlv”
1.No 1.没有
2.FK_NoSubmit 2.FK_NoSubmit

Table "DTE_C_SCFM" 表“ DTE_C_SCFM”
1.No 1.没有
2.FK_NoSubmit 2.FK_NoSubmit

and also other states like above as a table... this could has made your query more simple and faster as far as I got ...you are inserting these states in the same time... 以及上面表格中的其他状态...就我所知,这可能使您的查询更简单,更快捷...您正在同时插入这些状态...

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

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