Connecting Tech Pros Worldwide Forums | Help | Site Map

merge data from two sets into one

code green's Avatar
Expert
 
Join Date: Mar 2007
Location: England
Posts: 1,083
#1: Jul 15 '08
I have a table price_breaks with different prices identified by price_list ie:
Expand|Select|Wrap|Line Numbers
  1. part    price_list      price
  2. 401        WSPL                1.6
  3. 401        LEVEL-70         1.3
  4. 404        WSPL                3.0
  5. 404        LEVEL-70         3.3
  6. 502        WSPL                2.0
  7. 600        LEVEL-70      4.0
  8.  
I need to INSERT new records INTO this table,
which is a combination of the two price-lists as a new price_list WSPL70.
The criteria being
1. If a price for WSPL and LEVEL-70 exists, use the WSPL price
2. If only one price exists use that

I can JOIN to another table called products with the same parts.
The WSPL70 prices must be updated weekly from the same table
with any new products inserted
So I thought along the lines of
Expand|Select|Wrap|Line Numbers
  1. DELETE FROM price_breaks WHERE price_list = 'WSPL70';
  2.  
  3. INSERT INTO price_breaks
  4. (part,price_list,price)
  5. VALUES
  6. (SELECT COALESCE (pl.part,alt.part) part
  7. 'WSPL70' price_list,
  8. COALESCE (pl.price,alt.price) price,
  9. FROM products
  10. JOIN price_breaks AS pl 
  11. ON (part.part = pl.part 
  12. AND pl.price_list = 'WSPL')
  13. LEFT JOIN price_breaks AS alt 
  14. ON (part.part = alt.part 
  15. AND alt.price_list = 'LEVEL-70'
  16. AND pl.price_list IS NULL))
I have only tested the SELECT sub-query so far which doesn't return any 'LEVEL-70' prices
because the first JOIN filters them out.
If I change this to a LEFT JOIN
every part in products is returned with NULL values
whereas a RIGHT JOIN returns all the other price_lists (not shown in eg)

Can anybody help with this
Also, can I INSERT INTO a table with data from a sub-query from the same table as shown

deepuv04's Avatar
Expert
 
Join Date: Nov 2007
Posts: 202
#2: Jul 15 '08

re: merge data from two sets into one


Hi,
try using the following query

Expand|Select|Wrap|Line Numbers
  1. INSERT INTO price_breaks
  2. (part,price_list,price)
  3. SELECT  distinct t.part,'WSPL70',
  4.         case when wspl_Price is not null and level70_price is not null then wspl_price
  5.              when wspl_Price is not null and level70_price is  null then wspl_price
  6.              else level70_price end as price                                    
  7. FROM price_breaks as t left outer join
  8.         (select part,PRICE as wspl_Price from price_breaks as t1 where price_list = 'wspl'
  9.         ) AS t1 on t1.part = t.part left outer join
  10.         (select part,price as level70_price from price_breaks as t1 where price_list = 'Level-70' 
  11.         ) as  t2 on t2.part = t.part
  12.  
  13.  
Thanks
code green's Avatar
Expert
 
Join Date: Mar 2007
Location: England
Posts: 1,083
#3: Jul 15 '08

re: merge data from two sets into one


Different approach.
It was still producing null prices, probably from the other price_lists in the table.
(There are about 20 altogether) so I amended the query a little
Expand|Select|Wrap|Line Numbers
  1. SELECT DISTINCT t.part,'WSPL70',
  2. wspl_price, level70_price,
  3. CASE WHEN wspl_price IS NOT NULL 
  4.     THEN wspl_price
  5. WHEN wspl_Price IS NULL 
  6.     AND level70_price IS NOT NULL 
  7.     THEN level70_price END AS price                                    
  8. FROM price_breaks AS t 
  9. LEFT OUTER JOIN
  10.  (SELECT part,price AS wspl_price 
  11.   FROM price_breaks AS t1 
  12.     WHERE price_list = 'PCD-WSPL') AS t1 
  13. ON t1.part = t.part 
  14. LEFT OUTER JOIN
  15.   (SELECT part,price AS level70_price 
  16.   FROM price_breaks AS t2 
  17.       WHERE price_list = 'LEVEL-70') AS t2 
  18. ON t2.part = t.part
  19. WHERE price_list = 'LEVEL-70' OR price_list = 'PCD-WSPL'
  20. ORDER BY t.part
Basically I moved a CASE condition to a WHERE condition at the end.
The result looks promising.
Using DISTINCT is a cop out in my book because it is not as efficient as filtering the records beforehand.
But I can see why it is needed in this case.

Thanks very much deepuv04.
Reply


Similar Microsoft SQL Server bytes