Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Database > SQL Language
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Language section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old September 1st, 2005, 08:09 AM
Authorized User
 
Join Date: Jun 2004
Location: York, East Yorkshire, United Kingdom.
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default aggregate update?!

I have two tables which are linked by product code, in table one I have the product (12000 of them) and in table two I have the same products but with mulitple prices, I want to take the lowest price from table two and update table one with it. Simple? Well I can't figure it out.. :-/

I have included the two approaches tried, maybe I have just missed something small, or maybe its just a mess?

Approach one produced the following error: an aggregate function may not appear in an update statement

UPDATE tbl_one
SET tbl_one.price = MIN(tbl_two.price)
FROM tbl_two JOIN tbl_one.stockcode = tbl_two.stockcode
WHERE tbl_one.stockcode = tbl_two.stockcode

approach two doesn't throw an error, and updates the records but not with the lowest price:

UPDATE tbl_one
SET tbl_one.price= tbl_two.price
FROM tbl_two
WHERE tbl_two.stockcode = tbl_one.stockcode
   SELECT MIN(price)
   FROM tbl_two INNER JOIN tbl_one
      ON tbl_two.stockcode = tbl_one.stockcode
   WHERE tbl_two.stockcode = tbl_one.stockcode

As always any quick help or pointers greatfully received! :)

Thanks in advance

Stuart
Reply With Quote
  #2 (permalink)  
Old September 1st, 2005, 09:01 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Hudson, MA, USA.
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

You've more or less bracketed the solution, although a JOIN is not necessary.

Let's restate your first attempt in pseudocode:
Code:
UPDATE tbl_one
SET tbl_one.price = <the minimum price from tbl_two for this tbl_one.stockcode>
Now, to get the value of the expression in brackets for a given stockcode:
Code:
SELECT MIN(price) FROM tbl_two WHERE stockcode = <somevalue>
Now, we combine them. The mechanism we use to combine them is a correlated subquery, and the <somevalue> will be the stockcode in tbl_one that is being updated at the moment:
Code:
UPDATE tbl_one
SET tbl_one.price = (SELECT MIN(tbl_two.price) FROM tbl_two WHERE tbl_two.stockcode = tbl_one.stockcode);
You need to be careful about NULL here; if tbl_one has a stockcode which is not in tbl_two, the subquery will return NULL and that will update the tbl_one.price. Be sure that's what you want

Jeff Mason
Custom Apps, Inc.
www.custom-apps.com
Reply With Quote
  #3 (permalink)  
Old September 1st, 2005, 09:19 AM
Authorized User
 
Join Date: Jun 2004
Location: York, East Yorkshire, United Kingdom.
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default

You wouldn't believe how many different trys I made to do this, so close and yet NOT! Thank you so much for you quick response and help.
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
aggregate function polofson BOOK: Professional SQL Server 2005 Reporting Services ISBN: 0-7645-8497-9 0 August 14th, 2007 01:29 PM
How to use the SUM Aggregate gregalb SQL Server 2000 1 May 10th, 2007 01:19 AM
DataShaping with Aggregate Jon4Paz Classic ASP Databases 0 September 15th, 2006 01:29 PM
Aggregate Concatenation 12th_Man SQL Language 4 January 10th, 2006 03:26 PM
Aggregate Count also need o sizemore SQL Language 2 November 22nd, 2004 04:28 PM



All times are GMT -4. The time now is 07:24 AM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.