Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
RichardEttinger
Starting Member
7 Posts |
Posted - 2004-02-20 : 13:01:37
|
| We are building an advertising system based on auctions where customers can bid on positions. The highest bidder for a position should get that position..The table (simplified for this post) is called Adswith this query (below)select a1.AdId, a1.Originalpos, a1.pos, a1.currentbid, a1.listingDate from ads a1order by currentbid desc, listingdate descrreturns this dataAdId Originalpos pos currentbid listingDate ----------- ----------- ---- --------------------- --------------------------- 2 2 2 .1000 2002-01-30 00:00:008 8 8 .1000 2002-01-30 00:00:0019 5 5 .1000 2002-01-30 00:00:0025 1 1 .1000 2002-01-30 00:00:004 4 4 .0800 2002-01-30 00:00:0014 4 4 .0800 2002-01-30 00:00:0016 2 2 .0700 2002-01-30 00:00:0023 9 9 .0700 2002-01-30 00:00:0035 2 2 .0600 2004-02-10 08:47:0034 2 2 .0600 2004-02-10 00:00:0017 3 3 .0600 2002-01-30 00:00:001 15 15 .0500 2002-01-30 00:00:006 6 6 .0500 2002-01-30 00:00:0013 1 1 .0500 2002-01-30 00:00:009 9 9 .0500 2002-01-30 00:00:0010 10 10 .0500 2002-01-30 00:00:0018 4 4 .0500 2002-01-30 00:00:0020 6 6 .0500 2002-01-30 00:00:0024 10 10 .0500 2002-01-30 00:00:0026 8 8 .0500 2002-01-30 00:00:0029 1 1 .0500 2002-01-30 00:00:0030 1 1 .0500 2002-01-30 00:00:007 7 7 .0400 2002-01-30 00:00:0015 1 1 .0400 2002-01-30 00:00:0011 2 2 .0400 2002-01-30 00:00:0022 8 8 .0400 2002-01-30 00:00:0028 1 1 .0400 2002-01-30 00:00:0031 2 2 .0400 2002-01-30 00:00:003 3 3 .0300 2002-01-30 00:00:005 5 5 .0300 2002-01-30 00:00:0021 7 7 .0300 2002-01-30 00:00:0032 3 3 .0300 2002-01-30 00:00:0033 4 4 .0200 2002-01-30 00:00:0027 NULL NULL .0100 2002-01-30 00:00:00What we need to do is return either the Original Position (which was the Bidders first choice) or the next available position which has not yet been taken by a higher bidder) in a column named NewPos (so we can update the pos from this select query)I would like to avoid a cursor, unless someone can convince me it would be faster.. I think we need somekind of correlated subquery with a case statement which checks to see if a2.currentbid<a1.currentbid and a2.listingdate<a1.listingdate..I just can't seem to figure this one out....Thank you all so much for any help with this query...Richard Ettinger |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2004-02-20 : 13:04:32
|
| Please post the DDL for your table (CREATE TABLE statement) and post the data in the form of INSERT INTO statements. Without this information, we have to do all of the typing. So it makes it easier if the poster does the leg-work. You'll find that your question is answered quickly once we have the needed information. Also, show us what you want the result set to look like.Tara |
 |
|
|
RichardEttinger
Starting Member
7 Posts |
Posted - 2004-02-20 : 15:04:22
|
| Sorry Tara, I should have done that.. Also I apologize for the formatting of the return results, when I put it in it appeared fine, but in reviwing the post the data got smashed together..The Create Table statement isCREATE TABLE [dbo].[AdTest] ([AdId] [int] NOT NULL ,[OriginalPos] [tinyint] NULL ,[Pos] [tinyint] NULL ,[CurrentBid] [money] NULL ,[ListingDate] [datetime] NULL ,) ON [PRIMARY]GOInsert Into AdTest(AdId, OriginalPos, pos, currentbid,listingdate)values(2,2,2,.1000,'2002-01-30 00:00:00')Insert Into AdTest(AdId, OriginalPos, pos, currentbid,listingdate)values(8,2,2,.1000,'2002-01-30')Insert Into AdTest(AdId, OriginalPos, pos, currentbid,listingdate)values(19,5,5,.1000,'2002-01-30 00:00:00')Insert Into AdTest(AdId, OriginalPos, pos, currentbid,listingdate)values(25,1,1,.10,'2002-01-30')Insert Into AdTest(AdId, OriginalPos, pos, currentbid,listingdate)values(4,4,4,.08,'2002-01-30')Insert Into AdTest(AdId, OriginalPos, pos, currentbid,listingdate)values(14,4,4,.08,'2002-01-28')Insert Into AdTest(AdId, OriginalPos, pos, currentbid,listingdate)values(16,2,2,.07,'2002-01-29')Insert Into AdTest(AdId, OriginalPos, pos, currentbid,listingdate)values(23,3,3,.07,'2002-02-1')Insert Into AdTest(AdId, OriginalPos, pos, currentbid,listingdate)values(35,2,2,.06,'2002-02-1')Insert Into AdTest(AdId, OriginalPos, pos, currentbid,listingdate)values(34,2,2,.06,'2002-02-2')Insert Into AdTest(AdId, OriginalPos, pos, currentbid,listingdate)values(17,3,3,.06,'2002-02-1')Insert Into AdTest(AdId, OriginalPos, pos, currentbid,listingdate)values(1,15,15,.05,'2002-02-3')Insert Into AdTest(AdId, OriginalPos, pos, currentbid,listingdate)values(6,6,6,.05,'2002-02-3')Insert Into AdTest(AdId, OriginalPos, pos, currentbid,listingdate)values(13,1,1,.05,'2002-02-3')Insert Into AdTest(AdId, OriginalPos, pos, currentbid,listingdate)values(9,1,1,.05,'2002-02-3')Insert Into AdTest(AdId, OriginalPos, pos, currentbid,listingdate)values(10,2,2,.05,'2002-02-3')Insert Into AdTest(AdId, OriginalPos, pos, currentbid,listingdate)values(18,2,2,.06,'2002-02-3')Insert Into AdTest(AdId, OriginalPos, pos, currentbid,listingdate)values(20,1,1,.06,'2002-02-3')Insert Into AdTest(AdId, OriginalPos, pos, currentbid,listingdate)values(24,3,3,.05,'2002-02-3')Insert Into AdTest(AdId, OriginalPos, pos, currentbid,listingdate)values(26,1,1,.05,'2002-02-3')Data should ORIGINALLY look soething like:AdId____Originalpos_______pos_______currentbid_______listingDate 8_______2_______2_______.1000_______2002-01-28 00:00:00.0002_______2_______2_______.1000_______2002-01-30 00:00:00.00019______5_______5_______.1000_______2002-01-30 00:00:00.00025______1_______1_______.1000_______2002-01-30 00:00:00.00014______4_______4_______.0800_______2002-01-28 00:00:00.0004_______4_______4_______.0800_______2002-01-30 00:00:00.00016______2 ______2_______.0700_______2002-01-29 00:00:00.00023______9_______9_______.0700_______2002-02-01 00:00:00.00035______2_______2_______.0600_______2002-02-01 00:00:00.00034______2_______2_______.0600_______2002-02-01 00:00:00.00017______3_______3_______.0600_______2002-02-01 00:00:00.00018______2_______2_______.0600_______2002-02-03 00:00:00.0001_______15______15______.0500_______2002-02-03 00:00:00.0006_______6_______6_______.0500_______2002-02-03 00:00:00.00013______1 ______1_______.0500_______2002-02-03 00:00:00.0009_______1_______1_______.0500_______2002-02-03 00:00:00.00010______2_______2_______.0500_______2002-02-03 00:00:00.000Pos is the position the ad shows up in on our site. So pos 1 would be the top position of an ad, pos 2 would be the next position, pos 3 the next position, etc, etc, etc... We allow bidders to choose their position... But the hghest bidder for a pos (position) gets their choice and the next lower bidder gets the next sequential position of value)Ideally what we would like is to UPDATE pos (which is the position 1 through x) to a unique posgiving the highest bidders their choice for their Originalpos which is their first choice.. If the pos is already taken by the highest bidder it should then become the next available sequential position of value.The Ideal Output Table would be:AdId____Originalpos_______pos_______currentbid_______listingDate 8_______2_______1_______.1000_______2002-01-28 00:00:00.0002_______2_______2_______.1000_______2002-01-30 00:00:00.00019______5_______5_______.1000_______2002-01-30 00:00:00.00025______1_______3_______.1000_______2002-01-30 00:00:00.00014______4_______4_______.0800_______2002-01-28 00:00:00.0004_______4_______6_______.0800_______2002-01-30 00:00:00.00016______2 ______7_______.0700_______2002-01-29 00:00:00.00023______9_______8_______.0700_______2002-02-01 00:00:00.00035______2_______9_______.0600_______2002-02-01 00:00:00.00034______2_______10_______.0600_______2002-02-01 00:00:00.00017______3_______11_______.0600_______2002-02-01 00:00:00.00018______2_______12_______.0600_______2002-02-03 00:00:00.0001_______15______13______.0500_______2002-02-03 00:00:00.0006_______6_______14_______.0500_______2002-02-03 00:00:00.00013______1 ______15_______.0500_______2002-02-03 00:00:00.0009_______1_______16_______.0500_______2002-02-03 00:00:00.00010______2_______17_______.0500_______2002-02-03 00:00:00.000I know this is a tough one..... Its gotta perform fairly well as well..Thank you agin in advance...Richard :-) |
 |
|
|
RichardEttinger
Starting Member
7 Posts |
Posted - 2004-02-20 : 15:20:45
|
| If it makes the T-sql statement easier, the below output is also acceptable:Thethe high bidder could keep his position, if he is the high bidder for that position in the case of AdId 1 and 23 below..In this output the ads were not reassigned a new value..either way is acceptable, especially if one will perform better..AdId____Originalpos_______pos_______currentbid_______listingDate 8_______2_______1_______.1000_______2002-01-28 00:00:00.0002_______2_______2_______.1000_______2002-01-30 00:00:00.00019______5_______5_______.1000_______2002-01-30 00:00:00.00025______1_______3_______.1000_______2002-01-30 00:00:00.00014______4_______4_______.0800_______2002-01-28 00:00:00.0004_______4_______6_______.0800_______2002-01-30 00:00:00.00016______2 ______7_______.0700_______2002-01-29 00:00:00.00023______9_______9_______.0700_______2002-02-01 00:00:00.00035______2_______8_______.0600_______2002-02-01 00:00:00.00034______2_______10_______.0600_______2002-02-01 00:00:00.00017______3_______11_______.0600_______2002-02-01 00:00:00.00018______2_______12_______.0600_______2002-02-03 00:00:00.0001_______15______15______.0500_______2002-02-03 00:00:00.0006_______6_______13_______.0500_______2002-02-03 00:00:00.00013______1 ______14_______.0500_______2002-02-03 00:00:00.0009_______1_______15_______.0500_______2002-02-03 00:00:00.00010______2_______16_______.0500_______2002-02-03 00:00:00.000This is the toughest SQL problem I have ever had...Thank you in advanceRichard |
 |
|
|
jbkayne
Posting Yak Master
100 Posts |
Posted - 2004-02-20 : 20:24:24
|
| Here is a good start. How do you want to handle ties? Also can you explain listingDate?select a1.AdId , a1.Originalpos , a1.pos , a1.currentbid , a1.listingDate , (select count(*) from adtest where currentbid >= a1.currentbid) as posgroup into #temptablefrom adtest a1select temptbl.AdId , temptbl.Originalpos , temptbl.pos , temptbl.currentbid , temptbl.listingDate , posgroup ,(select count(*) from #temptable as temptbl2 where posgroup = temptbl.posgroup and originalpos <= temptbl.originalpos) + isnull((select top 1 posgroup from #temptable where posgroup = (select max(posgroup) from #temptable where posgroup < temptbl.posgroup)),0) as newposfrom #temptable as temptblorder by newposdrop table #temptable |
 |
|
|
|
|
|
|
|