Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 How to code to insert unique records?

Author  Topic 

Sun Foster
Aged Yak Warrior

515 Posts

Posted - 2009-03-04 : 12:05:35
There are two tables: Order1 and Order2.
How to code to insert all records from Order1 to Order2 but keep OrderID and ProductID both are unique in Order2?
That is, OrderID only list one time and ProductID only list one time as well. If a OrderID has more than one ProductID, can only list one ProductID.

raky
Aged Yak Warrior

767 Posts

Posted - 2009-03-04 : 12:14:14
try some thing like this

insert into order2
select
t.col1,t.col2,...,t.coln
from
( select row_number() over ( partition by orderid,productid order by orderid,productid ) as sno, col1,col2,...,coln
from order1 ) t
where t.sno = 1
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-03-04 : 12:47:11
quote:
Originally posted by raky

try some thing like this

insert into order2
select
t.col1,t.col2,...,t.coln
from
( select row_number() over ( partition by orderid,productid order by orderid, productid ) as sno, col1,col2,...,coln
from order1 ) t
where t.sno = 1


it seems op needs only one record per orderid, so modify as above
Go to Top of Page
   

- Advertisement -