MS SQL random results question in a query

I have a table with 2 colums:   Location    Word
that looks like this:
W1  Dust
W1  Dirt
W1  Soil
W1  Mud
W2  Rock
W2  Brick
W3  Fly
W3  Run
W3  Jump

Each location can have any number of words in it.  I have a total of 31 locations (W1 thru W31).

I need a select statement that will select 1 random word from each location  (i.e. returning 31 rows)

Can someone help me with this?  Thank you.

arthurh88Asked:
Who is Participating?
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.

Rajkumar GsSoftware EngineerCommented:
Check this

Raj
create table tblRandom
(
	Location varchar(20),
	Word	varchar(20)
)

insert into tblRandom
select 'W1',  'Dust'
union  all
select 'W1',  'Dirt'
union  all
select 'W1',  'Soil'
union  all
select 'W1',  'Mud'
union  all
select 'W2',  'Rock'
union  all
select 'W2',  'Brick'
union  all
select 'W3',  'Fly'
union  all
select 'W3',  'Run'
union  all
select 'W3',  'Jump'

select * from tblRandom

-- Random Query
select DISTINCT Location, 
	(SELECT TOP 1 Word 
    FROM tblRandom 
	WHERE Location = R.Location  
    ORDER BY NEWID() )
FROM tblRandom R

Open in new window

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
arthurh88Author Commented:
thanks!
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
Query Syntax

From novice to tech pro — start learning today.