Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
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.
Looking at a table that holds order information and there could be mulitple records with the same ord_no. I want to insert one record for each ord_no. What I'm doing now is inserting a record for each record in the table. Example:
In the sample above I want to insert one record for each ord_no. This code inserts a record for each record. How do I modify it so that it only inserts one record for each ord_no?????insert into table(ord_no,vendor,date)select ord_no, vendor, datefrom poordhdr_sql
madhivanan
Premature Yak Congratulator
22864 Posts
Posted - 2009-11-20 : 09:32:21
Tryinsert into table(ord_no,vendor,date)select ord_no, vendor, max(date)from poordhdr_sqlgroup by ord_no, vendorMadhivananFailing to plan is Planning to fail
russell
Pyro-ma-ni-yak
5072 Posts
Posted - 2009-11-20 : 09:39:51
Well, you'll have to decide which record you want inserted somehow. This will put the 1st (chronicalogically) in
with orders (rank, ord_no, vendor, date) as ( SELECT rank() over(partition by ord_no order by ord_no, date), ord_no, vendor, dt FROM poordhdr_sql)SELECT ord_no, vendor, date FROM orders WHERE rank = 1