ASP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingASP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old October 29th, 2002, 03:34 PM
aspnewbie aspnewbie is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2002
Location: The Great White North
Posts: 361 aspnewbie User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 58 m 50 sec
Reputation Power: 7
Send a message via MSN to aspnewbie
Problem with SQL Statement

I am writing an ASP page to display the winners of an auction. Right now, I am getting all of the bids instead of the highest bid for each item.

Here is my SQL statement:

Code:

strSQL = "SELECT Items.ItemID, Items.ItemName, 
Bids.BidderName, Bids.BidDate, Bids.BidTime, 
Max(Bids.BidAmount) AS CurrentBid 
FROM Items INNER JOIN Bids ON Items.ItemID = Bids.ItemID 
GROUP BY Items.ItemID, Items.ItemName, Bids.BidderName, 
Bids.BidDate, Bids.BidTime 
ORDER BY Items.ItemID "


Can this be re-written so that only the winning bids are displayed or is that done in the body of the asp page? Thanks in advance.

Reply With Quote
  #2  
Old November 2nd, 2002, 03:21 AM
rdoekes rdoekes is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2002
Location: Strasbourg, France
Posts: 181 rdoekes User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 m 20 sec
Reputation Power: 7
Send a message via AIM to rdoekes Send a message via Yahoo to rdoekes
nested select

Your entire request can be done in 1 query, but you have to do some fancy footworking with SQL.
It can be achieved by using nesting select statement. In essence, you create a temporary table (tbl_1) which is created at run time, which stores by itemID de maximum price. Afterwards, you use this as input in your search in the bid table to find the rest of the record, and you use this as input for finding the itemname in item.
Code:
SELECT 
         tbl_1.itemID, 
               b.BidderName, 
               b.BidDate, 
               b.BidTime, 
         tbl_1.MaxBid, 
               i.itemName
FROM (
   (SELECT b.itemID, Max(b.BidAmount) as MaxBid 
        FROM bids b 
        GROUP by b.ItemID) 
   AS tbl_1 
INNER JOIN Bids AS b ON 
             (tbl_1.MaxBid = b.BidAmount) AND 
              (tbl_1.itemID = b.ItemID)) 
INNER JOIN Item AS i ON 
       tbl_1.itemID = i.itemID


Good luck
__________________
- Rogier Doekes

Last edited by rdoekes : November 2nd, 2002 at 08:26 AM.

Reply With Quote
  #3  
Old November 2nd, 2002, 01:08 PM
aspnewbie aspnewbie is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2002
Location: The Great White North
Posts: 361 aspnewbie User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 58 m 50 sec
Reputation Power: 7
Send a message via MSN to aspnewbie
Thanks as always for your great advice!

Playing around with Access, I realized that I could create one query and then build that query into another to get the results I needed. I figured that a nested statement could do it, I just didn't know how to go about it. Now, thanks to you, I do!

BTW, the auction went very well! The validation (that you had helped me on previously) worked great. Thanks to you and markerdave for all your assistance.

Last edited by aspnewbie : November 2nd, 2002 at 01:22 PM.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingASP Development > Problem with SQL Statement


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway