Use SliceBy
- Last UpdatedMar 20, 2025
- 4 minute read
You can retrieve summary statistics for a tag per batch by using the SliceBy parameter in a query. Batches can be defined by changes in a different tag, such as a batch ID or valve position.
For example, suppose you wanted statistics on flow rate depending on the position of a valve. Each time the valve position changes, a new batch is reported. Here is a query to retrieve that information:
select SliceByValue, TagName, StartDateTime, EndDateTime, OPCQuality, PercentGood, wwResolution, Average
from AnalogSummaryHistory
where TagName='M31.FlowIn'
and SliceBy='M31.ValveIn'
and StartDateTime>='2018-11-27 0:00'
and EndDateTime<='2018-11-28 0:00'
The results are:
|
SliceByValue |
TagName |
StartDateTime |
EndDateTime |
OPCQuality |
PercentGood |
wwResolution |
Average |
|
0 |
M31.FlowIn |
2018-11-27 07:56:11 |
2018-11-27 09:49:47 |
192 |
100 |
6816000 |
57.4379 |
|
1 |
M31.FlowIn |
2018-11-27 09:49:47 |
2018-11-27 15:23:23 |
192 |
100 |
20016000 |
0.5108 |
|
0 |
M31.FlowIn |
2018-11-27 15:23:23 |
2018-11-27 17:24:17 |
192 |
100 |
7254000 |
50.3615 |
|
1 |
M31.FlowIn |
2018-11-27 17:24:17 |
2018-11-27 21:50:59 |
192 |
100 |
16002000 |
0.5220 |
|
0 |
M31.FlowIn |
2018-11-27 21:50:59 |
2018-11-27 23:47:17 |
192 |
100 |
6975000 |
51.4363 |
This query uses the same data. This time, using SliceByValue, the query retrieves statistics on the flow rate, but only for those batches when the valve is open.
select SliceByValue, TagName, StartDateTime, EndDateTime, OPCQuality, PercentGood, wwResolution, Average
from AnalogSummaryHistory
where TagName='M31.FlowIn'
and SliceBy='M31.ValveIn'
and SliceByValue=1
and StartDateTime>='2018-11-27 0:00'
and EndDateTime<='2018-11-28 0:00'
The results this time are:
|
SliceByValue |
TagName |
StartDateTime |
EndDateTime |
OPCQuality |
PercentGood |
wwResolution |
Average |
|
1 |
M31.FlowIn |
2018-11-27 09:49:47 |
2018-11-27 15:23:23 |
192 |
100 |
20016000 |
0.5108 |
|
1 |
M31.FlowIn |
2018-11-27 17:24:17 |
2018-11-27 21:50:59 |
192 |
100 |
16002000 |
0.5220 |
|
In the previous two queries, "StartDateTime>=" and "EndDateTime<=" define boundaries for the query to include only those batches that both start and end within the boundaries set. Any batches that begin before or end after the reporting period are excluded. |
|
|
This next query is the same as the previous one, but swaps the start and end time criteria to include batches that begin before or end after the reporting period: |
|
select SliceByValue, TagName, StartDateTime, EndDateTime, OPCQuality, PercentGood, wwResolution, Average
from AnalogSummaryHistory
where TagName='M31.FlowIn'
and SliceBy='M31.ValveIn'
and SliceByValue=1
and EndDateTime>='2018-11-27 0:00'
and StartDateTime<='2018-11-28 0:00'
The results are:
|
SliceByValue |
TagName |
StartDateTime |
EndDateTime |
OPCQuality |
PercentGood |
wwResolution |
Average |
|
1 |
M31.FlowIn |
2018-11-26 23:08:08 |
2018-11-27 07:56:11 |
192 |
100 |
31683000 |
0.5386 |
|
1 |
M31.FlowIn |
2018-11-27 09:49:47 |
2018-11-27 15:23:23 |
192 |
100 |
20016000 |
0.5108 |
|
1 |
M31.FlowIn |
2018-11-27 17:24:17 |
2018-11-27 21:50:59 |
192 |
100 |
16002000 |
0.5220 |
|
1 |
M31.FlowIn |
2018-11-27 23:47:14 |
2018-11-28 10:53:20 |
192 |
100 |
39966000 |
0.5039 |
SliceBy Queries That Include Future
If you define a timeframe that includes a batch that is not completed, you will get an summary statictics for the batch data currently available, but it will be marked with "OPCQuality=64", meaning the quality is uncertain because the end time is unknown. An ending batch can be incomplete because:
-
The batch is currently in progress and the next batch has not yet initiated. Batches are defined by changes in a different tag. If that tag has not yet changed, the current batch is in progress and the next batch has not initiated.
-
The batch did physically completed, but the data hasn’t yet been written to the server due to latency, power-outage, etc.
Here is an example. This query retrieves average values for batches that completed on Nov 28. The "select getdate()" line shows that the current timestamp for the query is Nov 28, 2018 at 4:31. Since the day is not over, the system cannot know for sure that another batch won’t be initiated. Therefore, the last retrieved batch is incomplete.
select SliceByValue, TagName, StartDateTime, EndDateTime, OPCQuality, PercentGood, wwResolution, Average
from AnalogSummaryHistory
where TagName='M31.Level'
and SliceBy='M31.Batch'
and EndDateTime>='2018-11-28 0:00'
and StartDateTime<='2018-11-29 0:00'
select getdate()
The results are:
|
SliceByValue |
TagName |
StartDateTime |
EndDateTime |
OPCQuality |
PercentGood |
wwResolution |
Average |
|
CRC-4 |
M31.Level |
2018-11-27 21:44:23 |
2018-11-28 03:03:41 |
192 |
100 |
19158000 |
94.9624 |
|
M31.Level |
2018-11-28 03:03:41 |
2018-11-28 04:12:20 |
192 |
100 |
4119000 |
1.6700 |
|
|
BLB-7 |
M31.Level |
2018-11-28 04:12:20 |
2018-11-28 00:00:00 |
64 |
1.7020 |
NULL |
134.8897 |
|
(No column name) |
|
2018-11-28 04:31:33 |
Using Group By with SliceBy
You can use Group By in a query to get an overall summary off all occurrences of a state. For example, you might want an overall summary of production when a piece of equipment was in a certain position.
Here is an example of using Group By to organize data by tag state. This query retrieves an overall summary of statistics for each position of the valve. One line summarizes all batches for the day where the valve was closed (0) and another line for all batches with the valve open (1).
select SliceByValue, TagName, StartDateTime=min(StartDateTime), EndDateTime=max(EndDateTime), TotalTime=sum(wwResolution), Maxiumum=max(Maximum), Total=sum(Integral), Average=sum(Average*wwResolution)/sum(wwResolution), AvgOfAvg=avg(Average)
from AnalogSummaryHistory
where TagName='M31.FlowIn'
and SliceBy='M31.ValveIn'
and EndDateTime>='2018-11-27 0:00'
and StartDateTime<='2018-11-28 0:00'
group by TagName, SliceByValue
The results are:
|
TagName |
SliceByValue |
StartTime |
EndTime |
TotalTime |
Maximum |
Total |
Average |
AvgOfAvg |
|
M31. |
0 |
2018-11-27 09:49:47 |
2018-11-27 21:50:59 |
36018000 |
6.75 |
309.66 |
0.51 |
0.51 |
|
M31. |
1 |
2018-11-27 07:56:11 |
2018-11-27 23:47:14 |
21045000 |
114.92 |
18593.13 |
53.00 |
53.078 |
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.

