i have a Dimension Table like this :
my_Table
pk1 Primary key
pk2 Primary key
pk3 Primary key
col1
col2
col3
...
and using a procedure to fill this table with the MERGE INTO statement :
MERGE INTO my_Table dest
USING
( SELECT <columns>
FROM <tables>
WHERE <conditions> ) src
ON
(dest.pk1 = src.pk1 AND dest.pk2 = src.pk2 AND pk3 = src.pk3)
WHEN MATCHED THEN UPDATE SET dest.col1 = src.col1 ,
dest.col2 = src.col2 ,
dest.col3 = src.col3
WHEN NOT MATCHED THEN INSERT (pk1, pk2, pk3, col1, col2, col3)
VALUES (src.pk1, src.pk2, src.pk3, src.col1, src.col2, src.col3);
my problem is that the query ‘src‘ returns some rows with same Primary keys, but they are no duplicated rows, example
| pk1 | pk2 | pk3 | col1 | col2 | col3 |
--------------------------------------------------
| 100 | abc | x99 | 6000,00 | 01/01/2010 | 50,00 |
| 100 | abc | x99 | 0,00 | 01/01/2010 | 30,00 |
| 110 | rty | b50 | 345,00 | 08/10/2009 | 10,00 |
| 120 | xyz | y91 | 1200,00 | 13/02/2009 | 12,50 |
| 120 | xyz | y91 | 1200,00 | 13/02/2009 | 0,00 |
| 120 | xyz | y91 | 1200,00 | 13/02/2009 | 0,00 |
| 120 | xyz | y91 | 1200,00 | 13/02/2009 | 0,00 |
So, when i call my procedure it returns the unique constraint violated error.
what could be the solution of this problem? im not the writer of the query src by the way …
if i need to modify src i should have for the pk (100,abc,x99) :
| pk1 | pk2 | pk3 | col1 | col2 | col3 |
--------------------------------------------------
| 100 | abc | x99 | 6000,00 | 01/01/2010 | 80,00 |
and for the pk (120,xyz,y91) i would have :
| pk1 | pk2 | pk3 | col1 | col2 | col3 |
--------------------------------------------------
| 120 | xyz | y91 | 1200,00 | 13/02/2009 | 12,50 |
Thanks in advance for any suggestion.
It’s obvious that something has to be done with
srcto make it return unique primary keys. If you don’t care too much about the quality of the data in your table, you could use aggregation and change it e.g. toBut chances are you need more sophisticated processing to make any sense of that stuff.
EDIT: changed first
sumtomax, to reflect the edit in the question