473,396 Members | 2,147 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

merge data from two sets into one

code green
1,726 Expert 1GB
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
Jul 15 '08 #1
2 1199
deepuv04
227 Expert 100+
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
Jul 15 '08 #2
code green
1,726 Expert 1GB
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.
Jul 15 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Alan Little | last post by:
I need to merge and de-duplicate some lists, and I have some code which works but doesn't seem particularly elegant. I was wondering if somebody could point me at a cleaner way to do it. Here's...
3
by: Kevin King | last post by:
I have a question about an assignment I have. I need to count the number of comparisons in my merge sort. I know that the function is roughly nlog(n), but I am definately coming up with too many...
2
by: William Wisnieski | last post by:
Hi Everyone, Access 2000 I have some code behind a button that performs a word merge with a query data source. The merge works fine. But what I'd like to do somehow is after the merge is...
1
by: Lisa | last post by:
I have a query named QryDept where one of the fields is DeptID. The query is used for the data source of a mail merge letter. I would like to control which department is to get the mail merge...
8
by: Squirrel | last post by:
Hi everyone, I've created a mail merge Word doc. (using Office XP) , the data source is an Access query. Functionality I'm attempting to set up is: User sets a boolean field to true for...
9
by: Neil Ginsberg | last post by:
I have a strange situation using Access to automate a Word mail merge. Using Access 2000 and Word 2000, the code opens Word, opens the document in Word, sets a table in the calling Access...
3
by: Andy Davis | last post by:
I have set up a mail merge document in Word 2003 which gets its data from my Access 2000 database. I want to set up a button on a form that: 1. runs the query to provide the dat for the merge...
6
by: iKiLL | last post by:
Hi all I am developing in C#, CF2 and SQL Mobile. Currently my app is using Merge Replication. This is all working well. I have now decided to try and use Result sets in my application but I...
0
by: mayankaerry | last post by:
Hi, I am facing an error on calling Datset update method. Following is my scenario: There are Three Excel Files Test1.xls,Test2.xls and Test3.xls. There is data in Test1.xls as under: B
2
by: JeffN825 | last post by:
Hello, Thank you in advance for any assistance. I am trying to set up a mail merge from VB6/VBA code with Word 2000 using an ODBC data source (SQL 2000). I have successfully gotten the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.