Selecting random ids using TOP and a CTE

While testing visualizations in a Flex application, I needed to do some underlying data cleanup in SQL Server.  One of my tasks was to manually update an entity table and set the status column to one of three possibilities.  Status group A and B both needed to be roughly 20% of my tables total record count, and status group C would be the remaining rows that weren’t touched by status A or status B.  Oh and there’s one more thing, the ids in each status group can not be in sequential order, they have to be random.

At first I thought no sweat.  My dataset is still small ( only 2000 rows ), so if we want uber control I could do the math and generate my id lists by hand.  Yes, hand crafting is possible and under a deadline that kind of logic almost makes sense.  However, I already know the table I’m working with will grow in the future, and I’ll probably have to do this data update again, so why not do this right?  While playing around with different select statements I had a “EUREKA!” moment.  SQL Server’s TOP operator supports PERCENT, not just number.  I couldn’t believe it.  I use TOP at least once a week and I always forget about TOP PERCENT.  Since I already know how to select random rows via CTE, it was time to put it all together.

Before giving you the final SQL, here are the important parts to be familiar with.  Also, for the sake of example I’m using the AdventureWorks database so you can play along at home.

TOP PERCENT

If you just need 50% of the rows in a table, but you’re not concerned about the sequence returned, you can fire this query.  This will give you a sequential listing of ProductIDs

[sql]
SELECT TOP 50 PERCENT ProductID
FROM Production.Product
ORDER BY ProductID
[/sql]

Which will look something like this.

SQL's TOP operator returns rows sequentially

COMMON TABLE EXPRESSION

Now let’s say you want to randomly pull all rows from a table.  This can be achieved using this CTE.

[sql]
WITH data( ProductID ) AS (
SELECT ProductID
FROM Production.Product
)
SELECT ProductID
FROM data
ORDER BY NEWID()
[/sql]

Which will look like this

Common Table Expressions in SQLSERVER are super helpful

If you’re looking to randomly select values from a pre-determined list, see my CTE sample here.

So now that you’ve seen TOP PERCENT and CTE in action, it’s time to put these together and solve my initial task of creating randomly selected groups of ids, of a percent size.

RANDOMLY SELECT TOP PERCENT

Putting it all together, here is the query I used to create my first status group.

[sql]
WITH data( ProductID ) AS (
SELECT ProductID
FROM Production.Product
)
SELECT TOP 20 PERCENT ProductID
FROM data
ORDER BY NEWID()
[/sql]

Which gives me a dataset that is 20% of all rows in Production.Product, and the ids are in random order.

And there you have it. Randomly selecting a percent sized data set from a table in SQL Server. The SQL here is really pretty simple, but for some reason I always forget TOP PERCENT. I’m hoping this post will help me remember TOP PERCENT, and maybe even help somebody else with some TSQL.

360 Flex, HTML5, Adobe AIR, and a bathrobe

I’m happy to share my 360 Flex presentation on HTML5 and Adobe AIR.  This is a topic I have a renewed interest in after Steve Jobs decided to start a war against Adobe’s Flash player.  Also, my very first major HTML5 project endlessmural.com was launched the week before 360 Flex.

Despite the title of this presentation, I didn’t put this together to bash HTML5 or Apple.  Steve Jobs fired me up when he declared Flash dead, and I realized it’s time to start having the “technology is a tool” conversation again.
Bottom line, know your problem so you can pick the appropriate tool for the job.  That tool might be HTML, it might be the Flash Platform, or it may be Lego blocks.  Chances are your target customer doesn’t care.
If you have any interest in the sample AIR applications I demoed, just drop me a line, comment, email, tweet, smoke signal, or carrier pigeon.

http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=preso-100922220734-phpapp02&rel=0&stripped_title=screw-html5-make-cool-shit-with-air-5264156&userName=ericfickes

View more presentations from Eric Fickes.
BTW, I gave this preso wearing a bathrobe.
I like to be comfy when I speak
* this was posted from the backseat of @thecompilers mini van while driving home from 360 Flex. We’re currently heading west on I70 ( Go go Sprint 4G! )

Want to see some kick ass HTML5?

I’m extremely happy to announce the www.endlessmural.com project was launched today and it was a huge success.  I intend on posting something with more details when I return home, but in the meantime please please check this site out.  It’s my current favorite example of HTML5 in action, and it works in all modern browsers ( yes, even iPad ).

The coolest HTML5 sample you will see on the internet

Go make art at endlessmural.com

Endlessmural.com is a generative drawing tool written in HTML5, Javascript, CSS3, on top of a Microsoft Azure backend.  Go, make art, share the url.

Here is a piece I made today.

endlessmural.com artwork