byrak79
byrak79

Reputation: 61

Inserting a new row of data into a table based on condition that compares with another table

I have a tracker table that tracks a certain number associated with our customers called "Tracker". The table looks like this:

customer_id    value     date_of_value
11111            2          2020-12-14
23332            6          2021-01-15

This table takes this information from another table called "Values", which is the exact same table as above but without the date_of_value column. This table keeps getting updated daily, and the Value associated with the customer gets overwritten with new values. What I would like to be able to do is to create a small job so the "Tracker" table looks at the "Values" table to see whether the value was updated. If so, I would like to insert a new row into the "Tracker" table with the customer's ID, the new value, and the date the value changed. In my mind, the query would look something like this:

CASE WHEN Tracker.value != Values.value
   THEN INSERT INTO Tracker (customer_id, value, date_of_value)
   VALUES (Values.id, values.value, GETDATE())

I tried using this along with UPDATE statement but none of them worked. Ideally, the "Tracker" table will look like this:

customerr_id    value      date_of_value
11111             2           2020-12-14
2332              6           2021-01-15
11111             3           2021-01-22
2332              8           2021-01-24 

How can I set up this query such that when I create a job, a new row is inserted with the updated information? When I tried using the UPDATE method, it overwrote the information in the "Tracker" table.

Upvotes: 0

Views: 38

Answers (2)

Charlieface
Charlieface

Reputation: 71159

We can create a trigger on the main table to insert a new row into the tracker table:

CREATE TRIGGER trg_Tracking
ON MainTable
AFTER INSERT, UPDATE AS

IF (NOT UPDATE(value) OR NOT EXISTS
    (SELECT customer_id, value FROM inserted
    EXCEPT
    SELECT customer_id, value FROM deleted))
    RETURN;        -- early bail-out if we can

INSERT Tracker (customerr_id, value, date_of_value)
SELECT customer_id, value, GETDATE()
FROM (
    SELECT customer_id, value FROM inserted
    EXCEPT     --make sure to compare inserted and deleted values (AFTER INSERT has empty deleted values)
    SELECT customer_id, value FROM deleted
) changed;

GO     -- No BEGIN and END as the whole batch is the trigger

Upvotes: 0

Gordon Linoff
Gordon Linoff

Reputation: 1269493

Hmmm . . . You want the most recent value from the tracker table. Then compare that to the existing values and insert where there are differences:

INSERT INTO Tracker (customer_id, value, date_of_value)
    SELECT v.customer_id, v.value, GETDATE()
    FROM (SELECT v.*,
                 ROW_NUMBER() OVER (PARTITION BY v.customer_id ORDER BY v.value DESC) as seqnum
          FROM values v
         ) v LEFT JOIN
         Tracker t
         ON v.customer_id = t.customer_id AND
            v.value = t.value
    WHERE v.seqnum = 1 AND t.customer_id IS NULL;

Upvotes: 2

Related Questions