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
Post a Comment