SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 select random rows
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

younas02
Starting Member

Pakistan
21 Posts

Posted - 08/02/2012 :  06:13:36  Show Profile  Reply with Quote
i want to select random rows from a table.
i used
select * from tbluser order by newid().
it works fine but it also repeat rows for example if i run this statement 10 times it may repeat row no 3 two time . i want to avoid this if a row is select once than not repeat it again until all rows are retrieved once.

sunitabeck
Flowing Fount of Yak Knowledge

5152 Posts

Posted - 08/02/2012 :  08:13:35  Show Profile  Reply with Quote
I am assuming that you have some additional condition in your select statement such as a TOP (N) clause - otherwise each select would return all the rows.

To exclude previously selected rows, you would need some way of keeping track of what was selected. Also, when you get to near the end, if the requested number of rows are not returned then you would need to reuse the ones that were already used etc.

In any case, here is a partial solution. I am using a second table to store what was already consumed. This can be made simpler if you have a PK in your table. Also, it is not perfect because when you don't have enough unused rows it does not return as much as requested.
CREATE TABLE #B(id INT, id2 INT);

CREATE TABLE #A(id INT, id2 INT);
INSERT INTO #A VALUES (1,2),(3,4),(5,6),(7,8),(9,10);

---------------------------------------------------
DECLARE @rowsRequested INT;
SET @rowsRequested = 2;

INSERT INTO #b
OUTPUT INSERTED.*
SELECT TOP (@rowsRequested) * FROM 
(
	SELECT  * FROM #A 
	EXCEPT SELECT * FROM #B
)s
ORDER BY NEWID();

IF (@@ROWCOUNT < @rowsRequested)
BEGIN
	TRUNCATE TABLE #b;
END
---------------------------------------------------
DROP TABLE #A;
DROP TABLE #B;
Go to Top of Page

younas02
Starting Member

Pakistan
21 Posts

Posted - 08/04/2012 :  07:12:11  Show Profile  Reply with Quote
i used a column to keep track of which row is selected once. i updated the column with bit data type to true for the selected row and used the condition to select row with flag column having value false. it works fin. but another problm is that , our site have online tests and many user give tests in a day. i update database on test start and set flage column value to false.
it works fine for single user but if more than one user take test at the same time then we r having problm. because if user A solve questions 1,4,8 then it will not apear again for user A, but at the same time if user B take test then questions 1,4,8 are apearing because their flag value have been updated to true,
Is there any method that our same db table will appear differently for different users
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47189 Posts

Posted - 08/04/2012 :  11:49:20  Show Profile  Reply with Quote
i think you should be tracking user also against questions he's solving inside a table. then when user2 is assigned question, only take questions which are not in userassign table . this will ensure questions for user1 are not repeated for user2

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000