Use the COUNT() function
- Last UpdatedMar 20, 2025
- 1 minute read
The COUNT(*) function works directly in a four-part query, but is not supported inside of the OPENQUERY function.
For example:
SELECT count(*)
FROM History
WHERE TagName = 'SysTimeSec'
AND DateTime >= '2001-12-20 0:00'
AND DateTime <= '2001-12-20 0:05'
AND wwRetrievalMode = 'delta'
AND Value >= 30
The result is:
|
150 |
If you use the OPENQUERY function, you cannot perform arithmetic functions on the COUNT(*) column. However, you can perform the count outside of the OPENQUERY, as follows:
SELECT count(*), count(*)/2 FROM OPENQUERY(INSQL, 'SELECT DateTime, vValue, Quality, QualityDetail
FROM History
WHERE TagName IN ("SysTimeSec")
AND DateTime >= "2002-04-16 03:00:00.000"
AND DateTime <= "2002-04-16 06:00:00.000"
AND wwRetrievalMode = "Delta"
')
The result is:
|
10801 |
5400 |
|
(1 row(s) affected) |