Select random percentage


Consider the following table structure:
Group_Code (INT) Prod_Code(INT) TestPoint(SMALLINT)Test_Date(DATETIME) ExportID(INT)

Group_Code,Prod_Code, TestPoint and Test_Date are the primary key.

The exportID could be entered multiple times. For example:

Group_Code  Prod_Code TestPoint Test_Date  ExportID
23456                78654           1            15 Jun 14    12345
23456                78654           2            15 Jun 14    12345
23456                78654           3            15 Jun 14    12345
23456                78654           4            15 Jun 14    12345
23456                78654           1            17 Jul 14     23456
23456                78654           2            17 Jul 14     23456
23456                78654           3           17 Jul 14      23456
Same Group_Code same Prod_Code but different Test_Date generates a different ExportID.
Is there a way that I can select a random 10percent of the export ids together with the other columns in the table. There could be in excess of 100 rows containing the same ExportID and I would need to gather all of the 10percent. So if one of the random percent was 12345, I would neeed to be able to select all 4 rows with that ExportID.

Any guidance would be appreciated.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Guy Hengel [angelIII / a3]Billing EngineerCommented:
yes, doing this in "2 steps":
first, get the random 10 percent export ID's, and join that back to the main table to get the rest of the data.
for example, something like this:
select t.*
  from yourtable t
where t.ExportID in ( select top 10 percent exportid from ( select exportid  from yourtable group by exportid  ) sq order by newid() )

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Morpheus7Author Commented:
Hi Guy,

Many thanks for your reply, that's great. Just one thing, what is the significance of sq just before the order by.

Guy Hengel [angelIII / a3]Billing EngineerCommented:
the "sq" is the alias to the subquery
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2005

From novice to tech pro — start learning today.