Solved

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

Posted on 2011-09-04
10
243 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
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 8

Expert Comment

by:venk_r
Comment Utility
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 59

Expert Comment

by:Kevin Cross
Comment Utility
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 59

Accepted Solution

by:
Kevin Cross earned 250 total points
Comment Utility
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
 
LVL 8

Expert Comment

by:venk_r
Comment Utility
Thats correct. I should have sent the corrected sql .
0
 
LVL 51

Expert Comment

by:HainKurt
Comment Utility
what about

select top 24 * from (
select *, newid() myid from mytable
) order by myid
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 59

Expert Comment

by:Kevin Cross
Comment Utility
HainKurt, myid will be a UNIQUEIDENTIFIER in that case and not a sequential 1-24.
0
 
LVL 51

Expert Comment

by:HainKurt
Comment Utility
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 51

Assisted Solution

by:HainKurt
HainKurt earned 250 total points
Comment Utility
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 51

Expert Comment

by:HainKurt
Comment Utility
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
Comment Utility
Excellent guys!
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

771 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now