x

projection from a query

i am trying to write a query that would do the following on the view update_winnerlist which is already GROUPed BY item_index

SELECT email_id (SELECT email_id,min(bid_amount) FROM update_winnerslist AS a1 WHERE a1.item_index = 10 )

i need to perform the above in a single query as i am using it in a trigger to update a table.
more ▼

asked Apr 26, 2012 at 12:53 AM in Default

heboy1908 gravatar image

heboy1908
10 1 1 1

the update_winnerslist table contains mail_id,item_index,bid_amount attributes.The table is already grouped by item_index values. I am trying to get the email_id for item_index=10 with the lowest bid_amount i.e. the email_id of the person who made the lowest bid on item_index 10.
Apr 26, 2012 at 02:05 AM heboy1908
(comments are locked)
10|1200 characters needed characters left

1 answer: sort voted first

@heboy1908 I did not understand your requirement properly. But if you want to hit your table only once then the following could be what you want

SELECT email FROM
(
SELECT email_id, bid_amount, ROW_NUMBER() OVER (ORDER BY bid_amount) ROWNUM
FROM update_winnerslist AS a1 WHERE a1.item_index = 10 
) MinBidAmount
WHERE [ROWNUM] = 1

Moreover, please keep in mind that there could be ties for the bid amount and if you are doing it in the trigger, you may need to get a random choice or whatever the requirement is. For that the TOP clause may be helpful.

PS: RANK() function could be used as well instead of ROW_NUMBER()
more ▼

answered Apr 26, 2012 at 04:52 AM

Usman Butt gravatar image

Usman Butt
13.9k 6 8 14

(comments are locked)
10|1200 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

SQL Server Central

Need long-form SQL discussion? SQLserverCentral.com is the place.

Topics:

x1951
x376
x70

asked: Apr 26, 2012 at 12:53 AM

Seen: 827 times

Last Updated: Apr 26, 2012 at 04:52 AM