Make a calculated tag in Wonderware to count the occurrences for you, when the value changed over a moving window .. then you read the current tag value via PCo / inSQLPCo.
You can't rely on the DB only because in the case of implementations transferring lots of data, the runtime table is getting updated with certain delays.