Groups | Blog | Home
all groups > sql server (microsoft) > march 2005 >

sql server (microsoft) : Problem inserting unique records


choxio NO[at]SPAM yahoo.com
3/27/2005 7:37:10 AM
I have two identical tables, T1 and T2, each with fields f1, f2, and
f3. I want to populate T1 with all unique combinations of f1, f2, and
f3 from T2.

selet f1, f2, f3
from T2
group by f1, f2, f3

I was thinking of something like

insert into T1 (f1, f2, f3)
select f1, f2, f3
from T2
group by f1, f2, f3
MINUS
select f1, f2, f3
from T1
group by f1, f2, f3

but there is no MINUS operator.

Any ideas?
Madhivanan
3/27/2005 10:59:56 PM

Try this

insert into T1 (f1, f2, f3)
select temp2.* from t1 temp1, t2 temp2 where
temp1.f1<>temp2.f1 and temp1.f2<>temp2.f2 and temp1.f3<>temp2.f3
group by temp2.f1,temp2.f2,temp2.f3

Madhivanan
jcelko212 NO[at]SPAM earthlink.net
3/28/2005 5:57:19 PM
[quoted text, click to view]
f2, and f3. <<

No proper schema should have identical tables. In the RDBMS model, an
entity is the sum of its attributes, so this means you have to two
tables for one entity. Then you do not know that the columns and
fields are totally different concepts.

[quoted text, click to view]

SELECT DISTINCT f1, f2, f3 FROM T2;

I f you insist on having a screwed up schema, then use a NOT EXISTS()
predicate for this kludge.
AddThis Social Bookmark Button