したがって、必要なユーザー数である@nを指定します。写真を持っている必要があるユーザーの割合である@xを指定します。
select top (@n) *
from
(
select top (@n * @x / 100) *
from users
where picture is not null
union all
select top (@n) *
from users
where picture is null
) u
order by case when picture is not null then 1 else 2 end;
だから...あなたはせいぜい@n*@x / 100人の写真を持っているユーザーが欲しいです、そして残りは写真を持っていない人々でなければなりません。だから私は@n* @ x/100の写真の人々と私の@nを完成させるのに十分な他の人々の間で「すべての結合」を行っています。それから私はそれらを選択し直し、写真を持っている人を確実に保つために私のTOPを注文します。
ロブ
編集:実際には、これはより良いでしょう:
select top (@n) *
from
(
select top (@n * @x / 100) *, 0 as NoPicture
from users
where picture is not null
union all
select top (@n) *, 1 as NoPicture
from users
where picture is null
) u
order by NoPicture;
...それはORDERBYの影響を取り除くからです。