Are you using FactorySQL or Ignition? If Ignition, are you using the SQL Bridge module? If so, or if FactorySQL, you just need to add the two items to a standard group (or a historical group), and then on the trigger panel select "Async Trigger" or "Only evaluate when values have changed" option (the names are a little different between the two programs).
If you're using Ignition and want to do it in scripting, I would just create a gateway tag change script that listens to the tags you want, and then use the system.tag.read()
functions. The documenation
for read() has an example of using both of them together.
Hope this helps,