Question

For example now I have a table like this:

Col1      Col2  Col3    Col4
1         1     1       1
11        2     3       44
111       2     3       444
1111      3     3       3

I have another table with the same structure, except that it has an unique index include Col2 and Col3. So, I want to select from the first table and insert into the second table, but skip record that have the same unique index. So I can have a new table with data like:

Col1      Col2  Col3    Col4
1         1     1       1
11        2     3       44
1111      3     3       3

How can I do that ?

Currently I'm using Merge Into, but in the situation that my table have millions records it is very slow

Was it helpful?

Solution

Try this query, without unique index/constraint -

Query:

DECLARE @temp TABLE
(
       Col1 VARCHAR(10)
     , Col2 VARCHAR(10)
     , Col3 VARCHAR(10)
     , Col4 VARCHAR(10)
)

INSERT INTO @temp (Col1, Col2, Col3, Col4)
VALUES 
     ('1',     '1', '1', '1'),
     ('11',    '2', '3', '44'),
     ('111',   '2', '3', '444'),
     ('1111',  '3', '3', '3')

SELECT
      Col1 = MIN(Col1)
    , Col2
    , Col3
    , Col4 = MIN(Col4)
FROM @temp
GROUP BY
      Col2
    , Col3

Output:

Col1       Col2       Col3       Col4
---------- ---------- ---------- ----------
1          1          1          1
11         2          3          44
1111       3          3          3

OTHER TIPS

I doubt you'll be able to do much better than a merge, but you could try optimizing your merge query. Maybe post the query you are currently using? In any case, analytics tend to be fast in these case. Something along the following lines for example:

SELECT Col1, Col2, Col3, Col4
FROM (
    SELECT Col1,
           Col2,
           Col3,
           Col4,
           MIN(Col1) OVER (PARTITION BY COL2, COL3) AS MinCol1
    FROM someTable
) Temp
WHERE Col1 = MinCol1
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top