简体   繁体   中英

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. 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. 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. 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.

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.

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.

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 :

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

Table "Submit"
1.NO
2.Time
3.and other columns

and then

Table "PGPOL_ProdVlv"
1.No
2.FK_NoSubmit

Table "DTE_C_SCFM"
1.No
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...

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