I asked almost the same question before, but I cannot use the MERGE syntax because of a sequencer. The problem is that we have price rules for people in a database. I want to synchronise these price rules for other people that should have the same rules.
+---------------------------+
| id | name | belongs_to_id |
|----+------+---------------+
| 1 | A | |
| 2 | B | 1 |
| 3 | C | 1 |
| 4 | D | |
+----+------+---------------+
Ok so now people B and C need, eventually, to have the same price rules as user A. So I have a pricerule table (simplified) like this:
+-----------------------------------+
| id | user_id | product_id | price |
+----+---------+------------+-------+
| 1 | 1 | 1 | 0.12 |
| 2 | 1 | 2 | 0.10 |
| 3 | 1 | 3 | 0.03 |
| 4 | 2 | 2 | 0.10 |
| 5 | 2 | 3 | 0.10 |
| 6 | 3 | 1 | 0.12 |
| 7 | 3 | 2 | 0.10 |
| 8 | 3 | 3 | 0.03 |
| 9 | 3 | 4 | 0.25 |
+----+---------+------------+-------+
So in this example, what should happen is:
- Give user 2 his price for product 1 with price 0.10
- Update price for user 2 product 3, to 0.03
- Delete price rule for user 3, product 4
I figured I have to take three steps: delete, update, add. I got the first two. But I’m thinking in circles about the insert statement. Basically I need to select all the price rules for user 1, then left join by product_id for each user. How do I do this for each user?
I am using Oracle 10.1
1 Answer