Results 1 to 10 of 10

Thread: SQL Subquery Help

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2001
    Location
    London UK
    Posts
    671

    SQL Subquery Help

    I am currently writing a basic charity online auction asp application for our intranet. All the bids are in a bid table which has fields for

    BidID, ItemID, BidTime, and BidValue.

    The ItemID joins to an Item table to get the ItemName.
    The BidderID joins to a Bidder table to get the BidderName.

    I am trying to write a query that will generate a list of all items, the current winning bidder and the Winning bid. But I am struggling with this.

    I can get a list of WinnningBids and ItemIDs with the below,

    SELECT MAX(BidValue) AS MaxBid, ItemID, BidderID
    FROM Bid
    GROUP BY ItemID, BidderID

    but if 2 people have bid the same it must go to the one with the earlier bid time (and I must allow same bids so it can go to the second person if the first person claims not to have bidded!)

    Any ideas how I could go about this in one query?

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974
    Something like this should do the trick..
    Code:
    SELECT MAX(BidValue) AS MaxBid, ItemID, BidderID
    FROM Bid
    
    WHERE <datefield> = (
       SELECT Min(<datefield>)
       FROM Bid Bid2
       WHERE ItemID = Bid.ItemID
       AND BidValue = Bid.BidValue  
       )
    
    GROUP BY ItemID, BidderID

  3. #3
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343
    SELECT MAX(BidValue) AS MaxBid, ItemID, BidderID, datetimebidded
    FROM Bid
    GROUP BY Desc ItemID,datetimebidded, BidderID
    I think that will return in the right order ....

    As to the cancellation of a bid, you'd need to figure out how to do that. Perhaps a Boolean field and then you can filter on that too.


    Vince

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2001
    Location
    London UK
    Posts
    671
    Thanks - but I still haven't got it exactly

    Si.....

    I tried.....

    SELECT MAX(BidValue) AS MaxBid, ItemID, BidderID
    FROM Bid
    WHERE BidTime =
    (SELECT MIN(BidTime)
    FROM Bid Bid2
    WHERE ItemID = Bid.ItemID AND BidValue = Bid.BidValue)
    GROUP BY ItemID, BidderID


    This Returned.......

    2200&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ANS
    900&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MSM
    200&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MSM


    I have no idea where that 200 has come from though - Help!

    Ecniv

    I couldn't get your SQL as written to work...

    Did you mean this?

    SELECT MAX(BidValue) AS MaxBid, ItemID, BidderID,
    BidTime
    FROM Bid
    GROUP BY BidTime, ItemID, BidderID
    ORDER BY BidTime DESC

    If so this unfortunately returns every row in the table as the time is different for each row.

  5. #5
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343
    Yeah I meant that (sorry).

    And as to the returned all records .. they are ordered though ?

    Your question said you wanted the first unless the first says they didn't bid then you need the second. This should list the top ten (say) in order of bids which yiu can produce.

    Then highlight the top bid and if they say no - you can disable and give to the second top most bid .. etc.



    Vince

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2001
    Location
    London UK
    Posts
    671
    Aah I see!

    I probably didn't make the question clear enough. The purpose of the query is to display a list of all current Bids, Items, and Values whilst the auction is running.

    I know that I could just get all the data, order it by MaxBid and Time and then just loop through it and do the processing on the page itself but I am sure there must be a way of doing it that just brings back the relevant results in the first place.

    If someone claims not to have bidded then I will just delete that bid record anyway so if I manage to get the first query working then the results it brings back should still be correct.

  7. #7
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343
    I see what you need I think.

    You want the grab the max bid with the min bid time.

    Hmm.
    Ok, roughed out the sort of thing you want... try this...
    VB Code:
    1. SELECT Max(bid.BidValue) AS MaxOfBidValue, bid.ItemID, bid.BidderId, Min(bid.BidTime) AS MinOfBidTime
    2. FROM bid
    3. GROUP BY bid.ItemID, bid.BidderId
    4. ORDER BY Max(bid.BidValue) DESC , Min(bid.BidTime);

    This was run on a table bid of :
    Code:
    BidID	ItemID	BidderId	BidTime	BidValue
    1	1	1	01/01/01 00:00:10	10
    2	1	2	01/01/01 00:00:12	11
    3	1	1	01/01/01 00:00:20	15
    4	1	3	01/01/01 00:00:15	15
    and gave the results :
    Code:
    MaxOfBidValue	ItemID	BidderId	MinOfBidTime
    15	1	1	01/01/01 00:00:10
    15	1	3	01/01/01 00:00:15
    11	1	2	01/01/01 00:00:12


    Vince

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  8. #8
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974
    oops, slight mistake.. the BidValue in the subquery should have been "MaxBid", but that isn't allowed.. this should work:

    Code:
    SELECT MAX(BidValue) AS MaxBid, ItemID, BidderID
    FROM Bid
    WHERE BidTime =
       (SELECT MIN(BidTime)
        FROM Bid Bid2
        WHERE ItemID = Bid.ItemID 
        AND BidValue = 
             (SELECT Max (BidValue)
              FROM Bid3 
              WHERE ItemID = Bid.ItemID)
       )
    GROUP BY ItemID, BidderID
    not too pretty tho', it should be possible to make it look nicer (and maybe a bit quicker), but my brain is dead at the mo!

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2001
    Location
    London UK
    Posts
    671
    Thanks very much both of you.

    That looks to be exactly what I need.

    I really do struggle when it comes to subqueries for some reason!

    Oh, and please excuse the lateness of my thanks but for some reason I didn't get any notifications about replies!?

  10. #10
    Member
    Join Date
    Jun 2002
    Posts
    36
    Try this. May be it would also work for you:
    select top 1 bidid from Bids WHERE BidValue=(SELECT Max(b.BidValue) FROM Bids b)
    ORDER BY BidTime Asc

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width