shahab Posted March 23, 2004 Posted March 23, 2004 Hi ,all How can i select 20 records of a table Randomly using the Id Column? :rolleyes: Quote
TechnoTone Posted March 24, 2004 Posted March 24, 2004 There are many ways you could do this, each with different merits depending on the situation. You could write a routine that pulls back all the records into an array and then picks out 20 random items. This would not be a very good solution if there were a lot of records to choose from. It could be improved by first getting just the ID column and then, once you've picked you random 20 items, get the rest of the data. Another way could be to add another column to the table with a floating point data type. Then, you could run an update query that would set this new column to a random number for each record. Finally, you could select the top 20 records, sorting by this random number column. Again, this may take a while if you have lots of records in the table. There are probably other ways that I haven't considered but the point is - the best solution is dependent upon your situation. Are you using a database server that could do some of the hard work for you? How many records are there? Is there any data in the table that could help you to randomize the selection and reduce the number of records that you pull back? Quote TT (*_*) There are 10 types of people in this world; those that understand binary and those that don't.
Celeron Posted March 24, 2004 Posted March 24, 2004 If I were doing that, I would generate some random numbers beforehand with Random Number class and then call all rows with ID's of those random numbers. Quote
shahab Posted March 24, 2004 Author Posted March 24, 2004 Thanks techno,My Table is this: CREATE TABLE [dbo].[tblQBank] ( [QID] [int] IDENTITY (1, 1) NOT NULL , [CatId] [int] NOT NULL , [QLevel] [tinyint] NOT NULL , [Question] [nvarchar] (4000) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL , [DescAnswer] [nvarchar] (4000) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [Answer] [tinyint] NULL ) ON [PRIMARY] GO CREATE TABLE [dbo].[tblQCategory] ( [CatId] [int] IDENTITY (1, 1) NOT NULL , [major] [nchar] (20) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL , [lesson] [nchar] (20) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL , [topic] [nchar] (20) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ) ON [PRIMARY] This is really a huge table with at last 400,000 record(Predictable)!this is a questionDatabase with answer ,... using for online tests!! *Would u please show me a code about those two ways u talked about:) ------------------------------------------------------------- Dear celeronwith Random Number class and then call all rows with ID's of those random number would u please show a code i am not familiar enough with this class:) Quote
*Experts* Nerseus Posted March 24, 2004 *Experts* Posted March 24, 2004 If you know how to query the table, you're almost done... if you don't, then ask how to write the code to query a table. If you can guarantee that the IDs in your table are 100% in order with no gaps, then you can use pure client side code to randomly pick 20 numbers (make sure you don't have duplicates). Then comma separate those numbers and build up a SQL string like this: "SELECT * FROM tblQBank WHERE QID IN (" + ids + ")" So if ids is a string that looks like "1, 56, 1054, 8763, 456, 23" then the SQL becomes: SELECT * FROM tblQBank WHERE QID IN (1, 56, 1054, 8763, 456, 23) If you can't guarantee the ID column will be sequential with NO gaps, then you'll need another approach. Here's a nice one (pseudo sql server code): CREATE TABLE #IDList ([id] int) SELECT qID INTO #AllIDs FROM tblQBank SET @i = 0 WHILE @i < 20 BEGIN -- Change the 0 below to use something more random SET @rnd = RAND(0) SELECT @Value = at position @rnd FROM #AllIDs -- Check if @Value exists in #IDList IF <doesn't exist> BEGIN -- Insert @Value INSERT INTO #IDList SELECT @Value SET @i = @i + 1 END END -Nerseus Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Arch4ngel Posted March 24, 2004 Posted March 24, 2004 Like a wise monk said... It's always better to centralize the code on the server so it's easier understandable, updatable and require less coffee for programmers. By doing this on the server... you also prevent some form of hacking through your web page. It's kinda more secure. Don't forget be Zen. Quote "If someone say : "Die mortal !"... don't stay to see if he isn't." - Unknown "Learning to program is like going out with a new girl friend. There's always something that wasn't mentioned in the documentation..." - Me "A drunk girl is like an animal... it scream at everything like a cat and roll in the grass like a dog." - Me after seeing my girlfriend drunk and some of her drunk friend. C# TO VB TRANSLATOR
shahab Posted March 24, 2004 Author Posted March 24, 2004 SELECT @Value = at position @rnd FROM #AllIDsWould u please explain it? Quote
Arch4ngel Posted March 24, 2004 Posted March 24, 2004 I don't understand either where he was going... but If you use a CURSOR with FETCH NEXT until you reach the right row... you should get a good result. (What's the meaning of this line ?????????) SELECT @Value = at position @rnd FROM #AllIDs Quote "If someone say : "Die mortal !"... don't stay to see if he isn't." - Unknown "Learning to program is like going out with a new girl friend. There's always something that wasn't mentioned in the documentation..." - Me "A drunk girl is like an animal... it scream at everything like a cat and roll in the grass like a dog." - Me after seeing my girlfriend drunk and some of her drunk friend. C# TO VB TRANSLATOR
shahab Posted March 25, 2004 Author Posted March 25, 2004 I had no experiene in using cursors and T-Sql like that,Does anyone say about Nerseus Program in a simle way?! Quote
Joe Mamma Posted March 31, 2004 Posted March 31, 2004 the answer Hi ,all How can i select 20 records of a table Randomly using the Id Column? :rolleyes: SQL server??? simple forget about your id column and order by an inline call to newId() select top 20 from mytable order by newID() Quote Joe Mamma Amendment 4: The right of the people to be secure in their persons, houses, papers, and effects, against unreasonable searches and seizures, shall not be violated, and no warrants shall issue, but upon probable cause, supported by oath or affirmation, and particularly describing the place to be searched, and the persons or things to be seized. Amendment 9: The enumeration in the Constitution, of certain rights, shall not be construed to deny or disparage others retained by the people.
georgepatotk Posted March 31, 2004 Posted March 31, 2004 select top 20 from mytable order by newID() Such a good idea.. Quote George C.K. Low
shahab Posted April 5, 2004 Author Posted April 5, 2004 SELECT TOP 10 * FROM tblQBank ORDER BY NEWID() Now just another problem.... How can I add some conditions to this selection: SELECT TOP 10 * FROM tblName ORDER BY NEWID() where field_name <> 0 Error msg: Server: Msg 156, Level 15, State 1, Line 2 Incorrect syntax near the keyword 'where'. Quote
Administrators PlausiblyDamp Posted April 5, 2004 Administrators Posted April 5, 2004 SELECT TOP 10 * FROM tblName where field_name <> 0 ORDER BY NEWID() Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.