SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Top 1 record order by column and random


Top 1 record order by column and random

Author
Message
KGJ-Dev
KGJ-Dev
SSCrazy
SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)

Group: General Forum Members
Points: 2451 Visits: 987
Hi,
Please find the below sample records to play with. basically i am looking to get top 1 ramdomized record. assume in my actual table i have 100*100 records. the tricky here is first randomization has to apply for the condition IsMember = 1. if there is no IsMember = 1 then randomization can apply to IsMember = 0.
Declare @T Table(Id int identity(1,1), name varchar(100),IsMember bit)

insert into @T(name,IsMember)

select 'Tom1', 1 union all
select 'Tom2', 1 union all
select 'Tom3', 1 union all
select 'Tom4', 1 union all
select 'Tom5', 1 union all
select 'Tom6', 0 union all
select 'Tom7',0 union all
select 'Tom8', 1 union all
select 'Tom9', 0 union all
select 'Tom10', 1 union all
select 'Tom11', 0


any best way to achieve this. tried googling and got advice to use order by newid(). but i am not sure how to apply the order by for IsMember conditional logic of my records. any sample query please
ZZartin
ZZartin
SSChampion
SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)

Group: General Forum Members
Points: 13927 Visits: 14549
Just put IsMember at the front of the order by BigGrin

SELECT TOP 1 * FROM @T ORDER BY IsMember DESC, NEWID()

KGJ-Dev
KGJ-Dev
SSCrazy
SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)

Group: General Forum Members
Points: 2451 Visits: 987
thank you. it worked
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum








































































































































































SQLServerCentral


Search