0

Here is the code to create the database:

CREATE TABLE foo (
    id TEXT PRIMARY KEY,
    value TEXT
);

INSERT INTO foo VALUES(1, 10), (2, 20), (3, 30), (5, 50);

Now I have a set of rows and I want back 0 if the row doesnt exist, 1 if the row exists but is not the same, and 2 if the row exists exactly.

So the result of the query on (1, 11), (2, 20), (4, 40) should be 1, 2, 0.

The reason I want this is to know what query to use to insert the data into the database. If it is a 0, I do a normal insert, if it is a 1 I do an update, and if it is a 2 I skip the row. I know that INSERT OR REPLACE will result in nearly the same rows, but the problem is that it doesnt trigger the correct triggers (it will always trigger an on insert trigger instead of an update trigger or no trigger if the row exists exactly).

Also, I want to do one query with all of the rows, not one query per row.

3 Answers 3

1

The idea is to use an aggregation query. Count the number of times that the id matches. If there are none, then return 0. Then check the value to distinguish between 1 and 2:

select (case when max(id = 1) = 0 then 0
             when max(id = 1 and value = 11) = 0 then 1
             else 2
        end) as flag
from table t;

You need to plug the values into the query.

EDIT:

If you want to match a bunch of rows, do something like this:

select testvalue.id,
       (case when max(t.id = testvalue.id) = 0 then 0
             when max(t.id = testvalue.id and t.value = testvalue.value) = 0 then 1
             else 2
        end) as flag
from table t cross join
     (select 1 as id 10 as value union all
      select 2, 20 union all
      select 4, 40
     ) as testvalues
group by testvalues.id;
3
  • No, when takes a boolean, hence when max(value > 1) = 0 then 1 when max(id = 1) = 0 then 0 and so on...
    – hd1
    Mar 23, 2014 at 1:50
  • Thanks, I shared what I went with below which was inspired from your answer. It has a few memory/performance benefits (i think) over this solution which is why I posted it. I'm leaving your answer as correct because without it I wouldnt have gotten to my answer and I want to thank you for that.
    – chacham15
    Mar 23, 2014 at 2:13
  • 1
    @chacham15 . . . Your solution makes perfect sense. I was thinking about the problem in terms of one row at a time. Mar 23, 2014 at 2:16
1

You can use the EXISTS argument in Transact-SQL. MSDN Documentation. This returns true if a row exists.
You can then use an If statement within that to check if the row is the same or different, and if true, use the RETURN argument with your specified values. MSDN Documentation.

0

This is based off of Gordon Linoff's answer so upvote him. I just wanted to share what I actually went with:

select testvalues.id,
       (case when t.id != testvalues.id then 0
             when t.value != testvalues.value then 1
             else 2
        end) as flag
from (select 1 as id, 11 as entity union all
     select 2, 20 union all
      select 4, 40 
     ) as testvalues
LEFT OUTER JOIN foo t on testvalues.id=t.id

This prevents the full memory usage of a cross join and group by clauses.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.