Scenario 10: Get summary statistics when a certain condition applies
- Last UpdatedMar 18, 2021
- 2 minute read
You can use SliceBy to answer questions like:
-
How much energy was consumed per batch?
-
What was the average flow rate when a valve was open?
-
What was lowest pressure today while a pump was running?
Using the SliceBy Parameter
SliceBy lets you define batches to be used with an Analog Summary query to get averages by batch. When you also use SliceByValue, you can get summary information per batch when a certain condition exists.
Example: Finding Total Energy Consumed per Batch
This query finds the total energy consumed per batch.
select SliceBy, SliceByValue, TagName, MIN(StartDateTime) as PeriodStart,
MAX(EndDateTime) as PeriodEnd, SUM(wwResolution)/1000 as [Duration(Sec)],
MIN(Minimum) as [Min], MAX(Maximum) as [Max], SUM(Integral) as [Total]
from AnalogSummaryHistory
where TagName='PowerMeter'
and SliceBy='R31.BatchNum'
and StartDateTime>='2019-04-17 0:00'
and EndDateTime<='2019-04-17 2:00'
group by TagName, SliceBy, SliceByValue
The results are:
|
SliceBy |
SliceBy |
TagName |
PeriodStart |
PeriodEnd |
Duration(Sec) |
Min |
Max |
Total |
|
R31.BatchNum |
844 |
PowerMeter |
4/17/2019 0:02:42 |
4/17/2019 0:11:57 |
554 |
8.4944 |
15.1857 |
610.6690 |
|
R31.BatchNum |
845 |
PowerMeter |
4/17/2019 0:11:57 |
4/17/2019 0:25:30 |
812 |
6.7169 |
14.8926 |
1267.8577 |
|
R31.BatchNum |
846 |
PowerMeter |
4/17/2019 0:25:30 |
4/17/2019 0:34:45 |
554 |
5.3918 |
17.2337 |
431.7148 |
Example: Finding Average Flow Rate When Valve is Open
This query retrieves statistics on the flow rate, but only while the valve is open. The query finds an average of weighted averages.
First, the query defines a batch by instance that the valve is open. Then, it calculates the total volume of the flow for that batch and calculates a weighted average by duration. Finally, it calculates an "average of averages" for the batch.
select SliceBy, SliceByValue, TagName, MIN(StartDateTime) as PeriodStart,
MAX(EndDateTime) as PeriodEnd, SUM(wwResolution)/1000 as [Duration(Sec)], MAX(Maximum) as [Max],
SUM(Integral) as [Total], AVG=SUM(Average*wwResolution)/SUM(wwResolution)
from AnalogSummaryHistory
where TagName='WaterFlow'
and SliceBy='R31_PLC.WaterValve'
and SliceByValue = 1 -- Valve Open
and StartDateTime>='2019-04-17 0:00'
and EndDateTime<='2019-04-17 2:00'
group by TagName, SliceBy, SliceByValue
The results are:
|
SliceBy |
SliceBy |
TagName |
PeriodStart |
PeriodEnd |
Duration |
Max |
Total |
Avg |
|
R31_PLC.WaterValve |
1 |
WaterFlow |
4/17/2019 0:02:45 |
4/17/2019 1:54:42 |
1199 |
1000 |
614995.85 |
512.51234 |
Note: This example correctly calculates the overall average for each state in the "Average" column by weighting the duration of each state. As explained by Simpson’s Paradox, the simpler, "AvgOfAvg" calculation is not statistically accurate and can differ significantly with some data sets.
Example: Finding the Lowest Pressure Today While a Pump Was Running
This example finds today's lowest pressure reading for a pump while it was running.
declare @date datetime
set @date = convert(date, getdate())
select SliceBy, SliceByValue, TagName, MIN(StartDateTime) as PeriodStart,
MAX(EndDateTime) as PeriodEnd, SUM(wwResolution)/1000 as [Duration(Sec)], MIN(Minimum) [Min],
MAX(Maximum) as [Max]
from AnalogSummaryHistory
where TagName='LinePressure'
and SliceBy='StorageTank_001.OutletValve'
and SliceByValue = 1 -- Valve Open
and StartDateTime>= @date
group by TagName, SliceBy, SliceByValue
Where results might be:
|
SliceBy |
SliceBy |
TagName |
PeriodStart |
PeriodEnd |
Duration |
Min |
Max |
|
Storage |
1 |
LinePressure |
4/19/2019 0:25:10 |
4/19/2019 8:05:07 |
3094 |
9.0150 |
15.4113 |