Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 SQL Select Top 5 Records Order By NewID()

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2005-06-01 : 08:38:15
Rob writes "Hi I am attempting to Use NewID() to Select 5 Random Records I am not worried about how random the selection is I just dont want multiple Records that are the same being selected from the DB I am trying to implement this for my Site for a news DataList that Binds on PostBack and shows different Records each time this Code Returns an error stating that I cannot Use Distinct without using Order By in the Select. When I remove the Distinct Statement the code works but once in a while it will select the same record multiple times. Any help would be greatly appreciated. :)


Select Distinct TOP 5 nw.NewsID, nw.NewsEN, nw.Link, nw.Year, nw.Quarter, nw.DescEN
From Mtbl_News nw
ORDER BY NewID()

We are currently Using
Microsoft SQL 2000 and Windows 2000 Advanced Server"

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-06-01 : 09:00:39
Select * from(
Select Distinct TOP 5 nw.NewsID, nw.NewsEN, nw.Link, nw.Year, nw.Quarter, nw.DescEN
From Mtbl_News nw) T
ORDER BY NewID()



Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2005-06-01 : 09:19:35
>>I just dont want multiple Records that are the same being selected from the DB

I think you are attempting to solve this problem in the wrong place ... perhaps your DATA itself should be cleaned up to remove the duplicates? A meaningful unique constraint on your table might help if you don't have one already.

- Jeff
Go to Top of Page
   

- Advertisement -