Remove Duplicates from ScriptState Table
Summary
This document outlines the process to clear duplicate entries from the scriptstate table.
It is a client script that can be scheduled to run once per day.
Sample Run
This script is to be scheduled as a client script.

Variables
| Name | Description |
|---|---|
| @Count@ | Saves the total number of duplicate variables per script per computer |
| @SqlScriptid@ | Distinct Scriptid |
| @SqlComputerid@ | Distinct Computerid |
| @SqlVariable@ | Distinct Variable |
| @SqlNumberofValues@ | Total number of duplicate entries for the distinct variable for the distinct computer for the distinct scriptid |
| @Limit@ | @SqlNumberofValues - 1 |
| %sqlresult% | Used to refresh the loop counter |
Process
Step 1: Fetch the total number of duplicate entries per scriptid, per computerid, and per variable from the scriptstate table using the following SQL query:
SELECT scriptid, variable, computerid, COUNT(*) AS NumberofValues
FROM scriptstate
GROUP BY scriptid, variable, computerid
HAVING NumberofValues > 1;
Step 2: Exit if the total number of results from the above step is zero.
Step 3: Run a loop to delete all the duplicate entries for each script state per computer per script.