Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Selection problem
Message
From
29/06/2011 09:11:47
 
General information
Forum:
Microsoft SQL Server
Category:
SQL syntax
Miscellaneous
Thread ID:
01516648
Message ID:
01516674
Views:
36
>>>I believe you need to reverse the condition, e.g.
>>>
>>>;with cte as (select T.*, row_number() over (order by NewID()) as RandomRow 
>>>from Test T where not exists (select 1 from Test T2 where T.FamilyID = T2.PartnerID))
>>>
>>>select * from cte where RandomRow <=4
>>>
>>>The above is from the top of my head - and may be a bit slow.
>>
>>
>>With such cross data (if you see Nicholas has the same IDs in BOTH fields) you wouldn't get any rows.
>
>Good point.
>
>I will need to think about it more - it's not very trivial.

Well I've ended up with this
select select top (@RecCount)  m.membership_no,m.member_ref
into tempdb.dbo.step1
from member m
join individual i
on m.individual_ref = i.individual_ref
left join individual i2
on i2.family_ref = i.individual_ref
where  ((i.individual_ref < i2.individual_ref ) or i2.individual_ref is null) -- suppress partner duplicates
order by newid()
Previous
Reply
Map
View

Click here to load this message in the networking platform