Click here to Skip to main content
15,892,480 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want help from you to create a query. I am new in software career and i am trying to developing a KBC Type Quiz Game for Windows Mobile Application. For That i want try to fetch random records from database and if one record is appear at one time after that record will not appear again. I use This query "SELECT TOP 1 * FROM Quiz ORDER BY NEWID()" but records are repeat. My Table structure is given below.

ColumnName DataType

Id int
Que varchar(150)
Ans1 varchar(100)
Ans2 varchar(100)
Ans3 varchar(100)
TrueAns varchar(100)

http://i.stack.imgur.com/0kKPw.png

I try to create a Store Procedure also which is given below

SQL
DECLARE @counter int, @randno int, @uBound int, @lBound int

            SELECT @uBound = Max(Id) FROM Quiz
            SELECT @lBound = Min(Id) FROM Quiz

            SELECT @randno = Round(((@uBound - @lBound) * Rand() + @lBound), 0)
            SET @Counter = 0

            WHILE @counter = 0
            BEGIN
                IF EXISTS(SELECT Id FROM Quiz WHERE Id = @randno)
                BEGIN
                    SET NOCOUNT OFF
                    SELECT * FROM Quiz WHERE Id = @randno
                    SET @counter = 1
                END
                ELSE
                BEGIN
                    SELECT @randno = Round(((@uBound - @lBound -1 ) * Rand() + @lBound), 0)
                END
            END



but i can't get success. My Table contain this fields Que, Ans1, Ans2, Ans3, TrueAns. Please Help me for this problem. And i want to also create a web-service which return all the records
Posted
Updated 3-Apr-13 22:28pm
v3
Comments
Maciej Los 4-Apr-13 2:35am    
Please, show me the structure of your table and example data.
How many records should return your SP (only 1 in a time)? Do you have a table to store a questions with answers?
Member 9762961 4-Apr-13 2:41am    
yes SP should return only one record at a time and i am create a field for answer in same table and i want whenever user give answer of question after that other question is show with it's options.
Maciej Los 4-Apr-13 3:07am    
Please, see my answer. Example SP wasn't tested...

check out this msdn article
http://msdn.microsoft.com/en-us/library/cc441928.aspx[^]
also try this ..store retrieve id in one variable and append in where clause for not in clause ...i want say something like below code...hope it wil
SQL
DECLARE @counter int, @randno int, @uBound int, @lBound int ,@notInId varchar(100)

SELECT @uBound = Max(Id) FROM Quiz
SELECT @lBound = Min(Id) FROM Quiz
SET @notInId=""
SELECT @randno = SELECT TOP 1 id, NewID() as Random FROM Quiz where id notin @notInId ORDER BY Random
SET @notInId=notInId+"'"+@randno+"'"
SET @Counter = 0

        WHILE @counter = 0
           BEGIN
            IF EXISTS(SELECT Id FROM Quiz WHERE Id = @randno)
                BEGIN
                    SET NOCOUNT OFF
                    SELECT * FROM Quiz WHERE Id = @randno
                    SET @counter = 1
                END
                ELSE
                BEGIN
                    SELECT @randno = SELECT TOP 1 id, NewID() as Random FROM Quiz where id notin @notInId ORDER BY Random
            SET @notInId=notInId+"'"+@randno+"'"
                END
            END
 
Share this answer
 
v2
Comments
Member 9762961 4-Apr-13 3:24am    
Thanks a lot i'll try your solution and resolve my problem.
Pallavi Waikar 4-Apr-13 3:27am    
after posting this i saw problem solved that's why i post this sorry
Pallavi Waikar 4-Apr-13 5:10am    
try this...first create store procedure that take no records u want that will return that much records
CREATE PROCEDURE dbo.GetQuestions (
@noOfRoas int
)
AS
SELECT * FROM Quiz WHERE Id in (SELECT DISTINCT TOP @noOfRoas id, NewID() as Random FROM Quiz ORDER BY Random)

-----------------
in c# use following code to access it
SqlConnection sqlConnection1 = new SqlConnection("Your Connection String");
SqlCommand cmd = new SqlCommand();
SqlDataReader reader;
int noOfRows=5;
cmd.CommandText = "GetQuestions";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@noOfRoas", SqlDbType.Int)).Value = noOfRows;
cmd.Connection = sqlConnection1;

sqlConnection1.Open();

reader = cmd.ExecuteReader();
// Data is accessible through the DataReader object here.

sqlConnection1.Close();
Member 9762961 4-Apr-13 5:29am    
Thanks a lot for your reply i think your solution is really helpful for me.
If the field for answer is in the same table, you need to change SELECT statement:
SQL
SELECT @counter=COALESCE(ID,0)
FROM Quiz
WHERE Id = @randno AND HasBeenAlreadyAnswered = False


You should randomize untill @counter=0.

In pseudo code:
1) generate random number (set @random)
2) check if question with ID = @random has been already answered
a) if yes, goto 1)
b) if not, exit while loop and return record


Below SP should works for you:
SQL
DECLARE @id INT
DECLARE @random INT

SET @id =0
SET @random = Round(((@uBound - @lBound) * Rand() + @lBound), 0)
WHILE (SELECT COALESCE(ID,0) FROM Quiz WHERE ID = @id AND HasBeenAlreadyAnswered=True)=0
BEGIN
    SET @random = Round(((@uBound - @lBound) * Rand() + @lBound), 0)
    SELECT @id = ID FROM Quiz WHERE ID = @random
END

--finally
SELECT *
FROM Quiz
WHERE ID = @id AND HasBeenAlreadyAnswered=False


That's all ;)

More about: WHILE[^]


[EDIT #1]
After your explanation, i see that your database design is bad. It's not possible to generate random question, if you do not store information about user answers... This is necessary to eliminate duplicates inside "random algorithm".
See examples:
Create a Quiz (for kids)[^]
Online Quiz[^]
Quiz - C# and .NET Secrets[^]
Online Exam in C# ASP NET[^]

At this moment solution sounds like: You need to redesign database.
 
Share this answer
 
v3
Comments
Member 9762961 4-Apr-13 3:08am    
Thanks a lot i'll try your solution and try to resolve my problem.
Maciej Los 4-Apr-13 3:11am    
You're welcome. Don't forget to vote (if solution was helpful) ;)
Member 9762961 4-Apr-13 3:22am    
sure i have one question in this solution HasBeenAlreadyAnswered is some field you take for answered question?
Maciej Los 4-Apr-13 3:26am    
Exactly! Remember, this is only an example. I don't see your database and table structure...
Member 9762961 4-Apr-13 3:28am    
how can i send my table structure?
DECLARE @id INT
DECLARE @random INT

SET @id =0
SET @random = Round(((@uBound - @lBound) * Rand() + @lBound), 0)
WHILE (SELECT COALESCE(ID,0) FROM Quiz WHERE ID = @id AND HasBeenAlreadyAnswered=True)=0
BEGIN
SET @random = Round(((@uBound - @lBound) * Rand() + @lBound), 0)
SELECT @id = ID FROM Quiz WHERE ID = @random
END

--finally
SELECT *
FROM Quiz
WHERE ID = @id AND HasBeenAlreadyAnswered=False
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900