Solved

How to load data into a table based on select criteria from another table

Posted on 2008-10-03
2
193 Views
Last Modified: 2010-04-21
I have a table Order . I need to load data into another table OrderDuplicates based on a select criteria where there are more than one occurrence of the following two column grouping, OrderID, OrderNumber.
Order and OrderDuplicates have same structure

Order
---------
OrderID
OrderNumber
OrderDate
OrderPrice
OrderUserid

So if I have the foll data
OrderID     OrderNumber    OrderDate            OrderPrice      OrderUserid
1                21                      09/01/2008             10.00           abc
1                21                      09/02/2008             12.00            lmn
1                21                       09/03/2008             11.00           xyz
1               20                       09/01/2008                10.00m       abc
2               19                       09/01/2008               9.00            xyz
2               19                       09/02/2008               10.00            abc
3               13                      09/02/2008               10.00             xyz
5               12                      09/03/2008               11.00            abc
6               12                       09/01/2008              10.00             xyz
6               12                       09/03/2008               9.00                abx

I would want the foll rows loaded into OrderDuplicates
1                21                      09/01/2008             10.00           abc
1                21                      09/02/2008             12.00            lmn
1                21                       09/03/2008             11.00           xyz
2               19                       09/01/2008               9.00            xyz
2               19                       09/02/2008               10.00            abc
6               12                       09/01/2008              10.00             xyz
6               12                       09/03/2008               9.00                abx



0
Comment
Question by:countrymeister
2 Comments
 
LVL 60

Accepted Solution

by:
chapmandew earned 500 total points
ID: 22633692
insert into orderduplicates(fieldlist)
select o.*
from
order o
join
(
select orderid, ordernumber
from order
group by orderid, ordernumber
having count(*) > 1
) d on o.orderid = d.orderid and o.ordernumber = d.ordernumber
0
 
LVL 1

Author Closing Comment

by:countrymeister
ID: 31502745
Great, thanks for your help and for the quick, accurate answer.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Using MERGE to UPDATE a third table 14 37
possible to record changes (trigger I think) msql 11 35
SYbase 4 32
MySQL Warning Statements when you have a LIMIT clause. 6 30
I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question