sql - Random select preventing some duplicates -


from table

name groupid       null b       1 c       1 d       2 e      null f      null g       3 

result expected random top 4 selection

name gruopid  null b   1 e  null g   3 

resuming want random names 1 kind of groupid if groupid <> null select newid() type returns "select top(4) * table order newid()"

name gruopid  null b   1 e  null c   1 

i don´t want that. hope made self clear! in advance

you can try (on sql server 2005+).

;with cte (     select *, row_number() over(partition groupid order newid()) corr     yourtable )  select top 4 name, groupid cte groupid null or corr = 1 order newid() 

Comments

Popular posts from this blog

linux - Mailx and Gmail nss config dir -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -