Random sampling with sample frequency in SQL


<pre>

<code>

–create table images ( id int identity(1,1), link varchar(200),img varchar(max), Freq int)
–insert into images values (‘aaa’,’blob1′, 20)
–insert into images values (‘bbb’,’blob2′, 60)
–insert into images values (‘ccc’,’blob3′, 40)
–insert into images values (‘zzz’,’blob4′, 0)

;WITH mycte
as
(

SELECT 1 as num
UNION ALL
SELECT num+1 as num from mycte WHERE num<100

)

select top 10 b.num, a.link, a.img from mycte b, images a
 WHERE a.Freq>=b.num
 ORDER BY NEWID()
 — a.link,a.img,b.num

</code>

</pre>

Advertisements


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s