Note that there are some explanatory texts on larger screens.

plurals
  1. PODetermine which columns have changed in rows of a table
    primarykey
    data
    text
    <p>I have a SQL Server table which contains incremental <code>Measurement</code>s of several different <code>Attribute</code>s pertaining to various <code>Object</code>s:</p> <pre><code>MeasurementID ObjectID Attribute1 Attribute2 Attribute3 100 1 'blue' 111 'large' 101 1 'blue' 114 'large' 102 1 'red' 114 'large' 103 1 'red' 117 'large' 104 1 'cyan' 118 'large' 105 2 'blue' 450 'huge' 106 3 'blue' 450 'huge' 107 3 'red' 450 'huge' </code></pre> <p>My objective is to find an efficient query which, given a specific <code>ObjectID</code> tells me which attributes changed and when. For example, let's say <code>ObjectID=1</code>. <code>Attribute1</code> changed at <code>MeasurementID=102</code> (from <code>'blue'</code> to <code>'red'</code>), and similarly for <code>Attribute2</code> at <code>MeasurementID=101</code> and <code>MeasurementID=103</code>, and for <code>Attribute3</code> at no time (<code>NULL</code>). Conceptually, I'm looking for something which fulfills this pseudo-code definition:</p> <pre><code>CREATE FUNCTION GetMeasurementChanges (ObjectID int, AttributeName varchar) RETURNS @returnMeasurementIDs TABLE (MeasurementID int) -- rows of MeasurementIDs </code></pre> <p>I can think of a slow way to do it with poor performance by selecting into a temp table on an <code>Object</code>-by-<code>Object</code> basis and then iterating through the rows, testing each <code>Attribute</code>, but I suspect that's going to perform very poorly.</p> <p>Anyone got a trick or an analogous question they can point me to?</p> <p>Thanks!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload