select query

Dear Experts,
I have two requirements here with my select query ( see attach code)
I wanting to have 2.5 % of records. I want random records and not the TOP records. how could i get this?

Second requirement. if you see my where clause i have disposition in (RNR,ptp,nb,cb) now I want this in priority.
it should check if it got RNR records if not  then it should check for PTP and then NB .. so on.
how could I set the priorty search?
Any suggestions are highly appreciated.
 
select top 2.5 percent * from [table_name]
where status='negative' and deli_day='30' and 
[disposition] in ('RNR','PTP','NB','CB')

Open in new window

parpaaAsked:
Who is Participating?

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

x
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:
you want to use the TABLESAMPLE syntax:
http://www.mssqltips.com/tip.asp?tip=1308
0

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
JermTheWormCommented:
select top 2.5 percent * from [table_name]
 where status='negative' and deli_day='30' and
 [disposition] in ('RNT','PTP','NB','CB')
order by
 disposition desc,
  rand()*10000

This will work because 'RNT','PTP','NB','CB' happen to be in reverse alphabetical order.
0
parpaaAuthor Commented:
Thanks @Jerm,
what if it is not in the reverse alphabetical order

say something like this?
where in (PTP,ABC,RNR,NB)
0
IT Pros Agree: AI and Machine Learning Key

We’d all like to think our company’s data is well protected, but when you ask IT professionals they admit the data probably is not as safe as it could be.

parpaaAuthor Commented:
Thanks @Angel
Tablesample clause is something new to me. thanks for sharing
0
Anthony PerkinsCommented:
Don't use RAND() in a SQL Statement it is not random, instead use NEWID().
0
JermTheWormCommented:
then you'll have to do a CASE clause to get them in the order you want i.e.
... order by
(CASE [disposition]
   when 'PTP' THEN 1
   when 'ABC' THEN 2
 .... etc ....
END)

p.s. I didn't test that but it should work... I think.
0
parpaaAuthor Commented:
Thank you soo much for your time guys .. It was good learning.
@Jerm I will test that. I understand the logic behind, thats all I want.
0
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 2008

From novice to tech pro — start learning today.