Troubleshooting
Historian SQL vValue exclusion filter fails: use a subquery
Short answer
The direct vValue exclusion fails because SQL Server can push the comparison into the historian's internal time-series processor, which accepts less of the SQL language than SQL Server does. Keep the tag, time bounds and retrieval hints in the inner query and apply the not-equal test against Empty in an outer query over the derived table. A working equality test proves nothing about the exclusion form, and the optimizer split can move between SQL Server releases.
On this page
Wrap the History retrieval in a derived table and put the value comparison in the outer query. A filter on the historian's vValue column runs in its equality form — vValue = 'Empty' returns the sentinel rows — but fails when you ask for everything except Empty, because SQL Server can push too much of the request into the historian's internal time-series processor, which understands less of the SQL language than SQL Server itself. The fix is structural, not a setting: keep the tag, the time bounds and the retrieval hints in the inner query exactly as they are, and apply the not-equal comparison to the retrieved rows outside it. The sections below give the checks that confirm this fault, the rewritten query worked through with real numbers, and the NULL case the exclusion filter silently changes.
Why the direct exclusion fails while the equality test runs #
A historian SQL request is processed by two engines. SQL Server parses and executes the standard T-SQL around the query; behind it, the historian's own time-series processor computes the retrieval itself — which stored rows satisfy the tag filter, the time bounds and the hint columns such as wwRetrievalMode and wwQualityRule.
In the schema of the reported query, the History table plus those ww-prefixed hint columns identifies the product as AVEVA Historian. The two processors normally cooperate transparently, but SQL Server's optimizer routinely moves predicate work toward the data — a design known as pushdown — and how an individual operator is rewritten for that trip depends on the optimizer and the release. Here the optimizer hands the value comparison to the time-series processor, which handles the equality form in that position but not the exclusion, so the request fails during historian processing: not at parse time, not for a missing tag, not for an unreadable value.
Two consequences follow. A working equality test is not evidence that another comparison works in the same position. And because the optimizer split can move between SQL Server releases, a query that ran last quarter can fail after an upgrade — the same nested form fixes it again.
| Arrangement | Where the comparison sits | What happens |
|---|---|---|
| Direct equality | Inside the retrieval query | Runs and returns the sentinel rows; a diagnostic, not the requested result |
| Direct exclusion | Inside the retrieval query | Fails while retrieval and value filtering are combined |
| Derived-table exclusion | Outer query over the inner retrieval | Runs and returns every non-sentinel row; use this form |
When a historian query behaves against expectation, it is worth resetting what a SQL historian actually promises; the knowledge-base piece debunking fourteen time-series myths around SQL historians in Ignition SCADA does that in one read.
First checks before rewriting the query #
Confirm the fault is the filter and not the retrieval or the application before changing the query structure, and change one thing at a time:
- Run the retrieval with no
vValuecomparison at all, selectingTagName,DateTimeandvValueso every returned row can be identified. - Confirm the tag returns records inside the chosen window. If it does not, the fault is the tag name, the collection status or the time range — fix that first.
- Run the known-working equality test with
vValue = 'Empty'to confirm sentinel rows exist in the window. - Capture the full error text from the failing exclusion query and separate a parse or identifier error from a failure during historian query processing. Write the string literal in single quotes and compare with the standard not-equal operator; where SQL Server is set to read double-quoted text as an identifier, a value written in double quotes fails for an unrelated reason.
- Execute the query directly against SQL Server and compare it with what the application submits. If the direct execution runs and the application fails, inspect the submitted SQL text, the connection context and the result-column binding.
- Keep the retrieval hints — retrieval mode, quality rule, version selection — identical across these tests, so the one intended change is the only difference.
One boundary case: a permission failure when the application writes to that same column is a different fault — fixing a historian vValue write permission error on string tags covers that repair, not this one.
Worked example: non-Empty rows from UDM001.LotNumber for the last 8 hours #
The inputs, with their sources: historian tag UDM001.LotNumber, a string tag from the reported case whose source writes the text Empty whenever no lot is running; a window of the preceding 8 hours ending when the query runs, as the example window; and the retrieval hints unchanged from the reported query — wwRetrievalMode = 'delta' (delta retrieval returns the stored value changes), wwQualityRule = 'Extended' and wwVersion = 'Latest'. The Extended quality rule keeps values of both good and uncertain OPC quality in the retrieval calculations, as the historian's retrieval documentation defines it.
The rule: the inner query — the subquery — performs retrieval only: tag selection, hints and time bounds. The outer query performs the value comparison against the returned rows. That separation gives the optimizer nothing to hand to the time-series processor.
SELECT AllRows.vValue
FROM (
SELECT TagName, DateTime, vValue
FROM History
WHERE TagName IN ('UDM001.LotNumber')
AND wwRetrievalMode = 'delta'
AND wwQualityRule = 'Extended'
AND wwVersion = 'Latest'
AND DateTime >= DATEADD(hh, -8, GETDATE())
AND DateTime <= GETDATE()
) AS AllRows
WHERE AllRows.vValue <> 'Empty';
The arithmetic, on an illustrative run at 14:00: the start bound DATEADD(hh, -8, GETDATE()) evaluates to 06:00 the same day, so the inner query retrieves the stored changes between 06:00 and 14:00. With illustrative counts, delta retrieval returns 57 rows in that window and 12 carry the sentinel from idle periods between lots; the outer filter keeps 57 - 12 = 45 rows.
The result is 45 rows, each a stored lot number that is not the sentinel, retrieved with the requested delta mode, Extended quality rule and latest version intact. AllRows is the derived-table alias: close the inner query once, supply the alias, then filter. While testing, project AllRows.TagName and AllRows.DateTime as well; trim the projection once the result is proven.
Decision: use the derived-table form for any vValue comparison beyond the equality form the historian already accepts; when a new comparison fails directly, move it to the outer query instead of tuning the inner one.
Do NULL values come back through the exclusion filter #
No. SQL evaluates vValue <> 'Empty' as unknown when vValue is NULL, and unknown is not true, so rows with a missing value drop out together with the sentinel rows. Three cases are distinct: the literal text Empty, an empty string written as '', and NULL. If the requirement includes missing values as well, extend the outer filter:
WHERE AllRows.vValue <> 'Empty'
OR AllRows.vValue IS NULL
Decide which of the three belongs in the result before approving the filter, because the screen fed by this query shows exactly what was chosen. Capitalization: on a server installed with one of the common case-insensitive collations — the usual default — rows holding EMPTY are excluded together with Empty; a case-sensitive collation separates them, and the column's collation decides which applies.
Verify the rewrite and keep it working after upgrades #
Verify with fixed boundaries: put literal start and end timestamps, chosen once, into both the unfiltered inner query and the nested query, because repeated runs with GETDATE() shift the window and can compare different rows. Keep TagName, DateTime and vValue in the projection and compare the row sets before and after the rewrite — identical except for the dropped sentinel rows. Keep the tag and time restrictions inside the inner query throughout; widening the window while debugging a value comparison raises the retrieved volume without touching the processing boundary.
The nested form retrieves exactly the rows the unfiltered retrieval returns, so its runtime tracks retrieval volume rather than the extra nesting; historian data retrieval speed: what actually decides it breaks down what moves that runtime. Run the rewrite directly against SQL Server first, install it in the consuming application second, and recheck it after any SQL Server or historian upgrade, because the optimizer split that makes the rewrite necessary can move between releases.
The decisions, case by case: a direct equality test that runs — keep it only as a diagnostic. A direct exclusion that fails — replace it with the derived-table form, hints and time bounds unchanged in the inner query. A result that must include missing values — add the IS NULL clause to the outer filter. A direct query that runs while the application fails — the fault is the submitted SQL or the column binding, not the historian.
Questions asked
Does the same subquery fix apply to other historians?
It applies wherever a historian extends SQL Server with its own time-series query processor behind a table such as History. Keep that product's retrieval hints, tag filters and time bounds inside the inner query, and place value comparisons in the outer query. For another product, take the hint column names from its own SQL documentation instead of copying the ww-prefixed names, and test the exclusion form directly before it reaches a screen.
Can the nested query live in a view or stored procedure?
Yes. A view or stored procedure can hold the inner retrieval and the outer filter, so every screen and report gets the same split without each client pasting in the nesting. Keep the retrieval hints inside the view's inner query, and re-test the view after a SQL Server or historian upgrade, because the optimizer split that makes the rewrite necessary can move between releases.
Why not drop the Empty rows in the application instead?
You can retrieve every row and discard the sentinel in client code, and the screen will show the same data. The cost is transfer and drift: every unwanted row crosses the network, and each client re-implements the rule, so screens and reports can disagree over time. The outer query makes SQL Server drop those rows before transfer and keeps one definition of the filter in one place.
Does capitalization change what the exclusion filter matches?
It can. Whether the not-equal comparison with 'Empty' also excludes rows holding 'EMPTY' depends on the SQL Server collation of the column. SQL Server's common default collations are case-insensitive, so the two forms match; a case-sensitive collation treats them as different values. Inspect the actual returned strings rather than assuming, especially where a source writes the sentinel inconsistently.