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 2005 Forums
 Transact-SQL (2005)
 select random rows

Author  Topic 

younas02
Starting Member

28 Posts

Posted - 2012-08-02 : 06:13:36
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
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-08-02 : 08:13:35
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

28 Posts

Posted - 2012-08-04 : 07:12:11
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

52326 Posts

Posted - 2012-08-04 : 11:49:20
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
   

- Advertisement -