Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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?

TT

(*_*)

 

There are 10 types of people in this world;

those that understand binary and those that don't.

Posted

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 celeron

with 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:)

  • *Experts*
Posted

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

"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
Posted

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.

"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

Posted

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

"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

Posted

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()

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.

Posted

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'.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...