0

Say i have a table as shown:

id, auctionUser, auctionId, MinPrice, NumBids, PlacedBids

And then say i've got the following entries in above table that have the same auctionId:

1 | user1 | 99 | 10.25 | 20 | 0
2 | user2 | 99 | 10.50 | 50 | 0

Is there a way to write a query ( WHERE auctionId = 99 ) that would return a row for every 0.01 of MinPrice where the two rows would 'intersect' (don't know if that's the right word but it's the best i could come up with to describe it) based on the number of bids in NumBids? So for the data above, there would be an 'intersect' of the two users from 10.50 thru 10.75. I'd like to be able to create the flowing data to display like so , alternating bids between the users for the number of bids set in NumBids:

(bidAmount) | (auctionUser) | NumBids | PlacedBids
10.50 | user2 | 50 | 1
10.51 | user1 | 20 | 1
10.52 | user2 | 50 | 2
10.53 | user1 | 20 | 2
10.54 | user2 | 50 | 3
10.55 | user1 | 20 | 3
.
.
.
10.70 | user2 | 50 | 20
10.71 | user1 | 20 | 20 <-- ends here for user1 since 20 NumBids would be used up
10.72 | user2 | 50 | 21

I don't even know if this is possible via a sql query or not -- or even how to start such a query. I thought i'd throw it out there to see if any sql guru's had and idea. I figured if there was a way to do it, it would probably be much faster to produce it from a query that trying to use php to cycle through and produce the result...maybe not though.

As always, MUCHO THANKS for any time and advice you can spare on this!

Michael
  • 11
  • 2
  • Is the data correct? 10.25 + 20 bids (1c increment) doesn't even reach 10.50 – RichardTheKiwi Feb 26 '11 at 23:12
  • You are correct in a sense. I just used basic data for simplicity sake. I'm just trying to figure out given a range of overlap in a MinPrice and NumBids, how to generate an alternating sequence of rows that will allow me to create entries for bids if bidders have overlapping auto-bids setup until the overlap no longer exists. I think the answer below concerning a numbers table it on the right track... – Michael Feb 27 '11 at 01:47
  • So let me get this Right, You have your starting bid Price which is 10.25 from 1 user, and they have had 20 bids, and you have another user that has had 50 bids and the auction is now up to 10.72. So a bid is always a 0.01 increment everytime? – Robbie Tapping Feb 27 '11 at 02:25
  • Yes, each bid is 0.01 increment. Maybe my having one bid start at 10.25 and the other start at 10.50 is throwing some off. I should have just started both out at 10.50 - one with 20 bids and another with 50 bids. In reality there will be many more than just 2 users with a starting MinPrice and NubBids. I'm mainly trying to find a way to produce rows of bids (in 0.01 increments) based on the overlap of the starting MinPrice and NumBids. The application of this is an auction site that has a autobid feature to let users have the system autobid based on a MinPrice for X NumBids. – Michael Feb 27 '11 at 05:11

1 Answers1

0

I can't say I understand exactly what you want, but I think you need to generate rows. One way of generating rows is to use a Numbers table, which is basically a table of consecutive integers.

Have a look at my answer to this question. It is not related to your question, but there is code to generate such a numbers table.

So if you want to generate 1 row for each 0,01 difference, you would calculate nr of cents (or whatever the currency was) and join to the numbers table with a filter on n < nr_of_cents.

Edit: Ok, I'll try. First, some sample data.

create table auctions(
   auctionuser  int 
  ,auctionid    int
  ,minprice     decimal(5,2)
  ,numbids      int
);

insert into auctions values(1, 1, 2.20, 2);
insert into auctions values(2, 1, 3.30, 4);
insert into auctions values(3, 1, 4.40, 6);

select *
  from auctions
 where auctionid = 1;

+-------------+-----------+----------+---------+
| auctionuser | auctionid | minprice | numbids |
+-------------+-----------+----------+---------+
|           1 |         1 |     2.20 |       2 |
|           2 |         1 |     3.30 |       4 |
|           3 |         1 |     4.40 |       6 |
+-------------+-----------+----------+---------+
3 rows in set (0.00 sec)

I think the following is close to what you want. Note that I have used the numbers table in the post I linked to.

select a.auctionuser
      ,n as user_bid
      ,minprice
      ,numbids
      ,a.minprice + (0.01 * (n-1)) as bid
  from auctions a
      ,numbers
 where numbers.n <= a.numbids
   and a.auctionid = 1
 order 
    by n
      ,a.minprice
      ,a.auctionuser;


+-------------+----------+----------+---------+------+
| auctionuser | user_bid | minprice | numbids | bid  |
+-------------+----------+----------+---------+------+
|           1 |        1 |     2.20 |       2 | 2.20 |
|           2 |        1 |     3.30 |       4 | 3.30 |
|           3 |        1 |     4.40 |       6 | 4.40 |
|           1 |        2 |     2.20 |       2 | 2.21 |
|           2 |        2 |     3.30 |       4 | 3.31 |
|           3 |        2 |     4.40 |       6 | 4.41 |
|           2 |        3 |     3.30 |       4 | 3.32 |
|           3 |        3 |     4.40 |       6 | 4.42 |
|           2 |        4 |     3.30 |       4 | 3.33 |
|           3 |        4 |     4.40 |       6 | 4.43 |
|           3 |        5 |     4.40 |       6 | 4.44 |
|           3 |        6 |     4.40 |       6 | 4.45 |
+-------------+----------+----------+---------+------+
12 rows in set (0.00 sec)
Community
  • 1
  • 1
Ronnis
  • 12,593
  • 2
  • 32
  • 52
  • I think you are on the right track here...not sure how i would go about alternating rows for multiple users that have the MinPrice and NumBids overlap?!? – Michael Feb 27 '11 at 01:48
  • @Michael, I updated my answer in response to some of your comments above. – Ronnis Feb 27 '11 at 07:26
  • thats for your time on this. I tried for a while to take your examples and get it to fit my requirements but after a time i had to abandon the effort and go with a procedural solution instead in order to keep the project moving along. I am going to go back when i have time to see if i can make it work though! Thanks again. – Michael Feb 28 '11 at 01:58