Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

how to select random recods or rows from sql server table including row no

Posted on 2011-09-04
10
Medium Priority
?
258 Views
Last Modified: 2012-05-12
My sql query is below

SELECT top 24 ROW_NUMBER() OVER (ORDER BY QuestionID) AS QuestionNo, Q.QuestionID From Questions Q
ORDER BY NEWID()

The first part " top 24 ROW_NUMBER() OVER (ORDER BY QuestionID) AS QuestionNo" gives me a row no. This part "ORDER BY NEWID()" gives me random rows. The results are like below

QuestionNo      QuestionID
2      aa
3      ss
1      qq


The above query first adds row no column to the other columns and then pulls the random records whereas I want the random questions first and then added row number to them e.g.

QuestionNo      QuestionID
1      aa
2      ss
3      qq

0
Comment
Question by:mmalik15
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 8

Expert Comment

by:venk_r
ID: 36481614
You dont need to use order by NEWID() function.You willl get the desired result without using it as it will order by using the function by itself.
0
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 36481685
You probably meant something more like this:

SELECT top 24 ROW_NUMBER() OVER (ORDER BY NEWID()) AS QuestionNo, Q.QuestionID
FROM Questions Q
0
 
LVL 60

Accepted Solution

by:
Kevin Cross earned 1000 total points
ID: 36481688
The ROW_NUMBER() function should cause the result set to be ordered by QuestionNo, but to make this more explicit to your intention, you can you do like this also:

SELECT QuestionNo, QuestionID
FROM (
   SELECT ROW_NUMBER() OVER(ORDER BY NEWID()) AS QuestionNo
        , Q.QuestionID
   FROM Questions Q
) derived
WHERE QuestionNo <= 24
;

Hope that helps!
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 8

Expert Comment

by:venk_r
ID: 36481689
Thats correct. I should have sent the corrected sql .
0
 
LVL 60

Expert Comment

by:HainKurt
ID: 36481718
what about

select top 24 * from (
select *, newid() myid from mytable
) order by myid
0
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 36481728
HainKurt, myid will be a UNIQUEIDENTIFIER in that case and not a sequential 1-24.
0
 
LVL 60

Expert Comment

by:HainKurt
ID: 36481742
got it :) what about this?

select top 24 *, row_number() over(order by myid) rowNo  from (
select *, newid() myid from mytable
) order by myid
0
 
LVL 60

Assisted Solution

by:HainKurt
HainKurt earned 1000 total points
ID: 36481747
oops, i guess there is no need for order by in this case

select top 24 *, row_number() over(order by myid) rowNo  from (
select *, newid() myid from mytable
) x
0
 
LVL 60

Expert Comment

by:HainKurt
ID: 36481758
the query above adds newid to all rows, then order by this and add row numbers, then we select top 24

if it is same/similar to yours above, just ignore it :)
0
 

Author Closing Comment

by:mmalik15
ID: 36485598
Excellent guys!
0

Featured Post

Fill in the form and get your FREE NFR key NOW!

Veeam® is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question