 |
|
|
 |
|
 |
I'm trying to fetch questions and possible answers from MSSQL SERVER. each time users start the test by clicking the start button, when they get to the question page it should execute this to randomly pick questions from the database:
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT Top 5 [QuestionID], [Title], [Answer1], [Answer2], [Answer3], [Answer4], [CorrectAnswer], [QuestionOrder] FROM [Question] WHERE ([QuizID] = @QuizID) ORDER BY [QuestionOrder],[NewID()]">
<SelectParameters>
<asp:SessionParameter DefaultValue="0" Name="QuizID" SessionField="QuizID" />
</SelectParameters>
Its not working after Ordering by [QuestionOrder],[NewID()]
then i tried: ORDER BY [NewID()]...and its shows an error: NewID field is invalid
Pls, any suggestive solution is welcome Thank you
|
|
|
|
 |
|
 |
I'm trying to fetch questions and possible answers from MSSQL SERVER. each time users start the test by clicking the start button, when they get to the question page it should execute this to randomly pick questions from the database:
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT Top 5 [QuestionID], [Title], [Answer1], [Answer2], [Answer3], [Answer4], [CorrectAnswer], [QuestionOrder] FROM [Question] WHERE ([QuizID] = @QuizID) ORDER BY [QuestionOrder],[NewID()]">
<SelectParameters>
<asp:SessionParameter DefaultValue="0" Name="QuizID" SessionField="QuizID" />
</SelectParameters>
Its not working after Ordering by [QuestionOrder],[NewID()]
Pls, any suggestive solution is welcome Thank you
|
|
|
|
 |
|
 |
Cool Tip... ...thanks bro your tip is excellent
|
|
|
|
 |
|
 |
Was using different techniques to select unique random numbers . found ur tip to extremely useful. Great. keep going.
|
|
|
|
 |
|
 |
Caution: It seems this doesn't work on all servers. See http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=2242.
|
|
|
|
 |
|
 |
I think the Rand(0 function can also help
if exists ( select 1 from sysobjects where name ='TestRan')
drop table TestRand
go
Create table TestRand
(
ADID int identity (0,1)
,ADlocation varchar(20)
)
go
INSERT INTO TestRand(ADlocation) VALUES ('WWW.LOCATION1')
INSERT INTO TestRand(ADlocation) VALUES ('WWW.LOCATION2')
INSERT INTO TestRand(ADlocation) VALUES ('WWW.LOCATION3')
INSERT INTO TestRand(ADlocation) VALUES ('WWW.LOCATION4')
INSERT INTO TestRand(ADlocation) VALUES ('WWW.LOCATION5')
GO
DECLARE @TOTALRECORDS int
DECLARE @AdidToBePicked int
select @TOTALRECORDS = count(*) from TestRand
select @AdidToBePicked = rand() * @TOTALRECORDS
select ADlocation from TestRand where ADID = @AdidToBePicked
regds
Himanshu
|
|
|
|
 |
|
 |
Infact when I first look at the SQL statement It does not make sense to me....I was taking NewID() as some other function...
Yet It is builtin SQL Server Method and you just have to copy and paste the Query in your Query Analyzer..
Great any way.........
|
|
|
|
 |
|
 |
Thanks your tip!
Make a brighter day...
|
|
|
|
 |
|
 |
Excellent piece of code!
I've been working as a SQL man for some years now and I've never realized this was possible. Always thought NewId's were assigned on insertions, not on "ORDER BY"
You deserve a beer or two!
|
|
|
|
 |
|
 |
This is amazing. Nowhere in reading BOL would you guess that you can do this.
The doc for Order By says that Order "Specifies a column on which to sort. A sort column can be specified as a name or column alias (which can be qualified by the table or view name), an expression, ..."
So you can sort by an expression. It seems that, magically, a newid() is appended to each record, even though there is not a newid() field in the original table, and the table is sorted by that, then of course the top 5 of those sorted records are selected.
You can also do " Select top 5 * from [whatever] Order By 'a' " although it's not very useful. I'm just surprised that you can do this.
Where is it documented (and explained) that you can sort by fields that don't even exist in the table? BOL might mention that you can sort by an "expression" although it sure doesn't explain what that really means or give ANY examples.
David Walker
|
|
|
|
 |
|
 |
Actually, BOL does say "The ORDER BY clause can include items not appearing in the select list." But it sure doesn't explain how this works or what really happens if you sort by items not in the select list.
|
|
|
|
 |
|
 |
I imagine every single row in the table gets assigned this NEWID()... on large tables do you know if there would be performance issues? (e.g. on a high traffic website).
|
|
|
|
 |
|
|
 |
|
 |
I wrote this solution that is considerable faster then the NewID() solution but has no flexibility and is based on tables with int primary keys (the ideal is an identity primary key).
If you dont need to make joins with other tables or other kinds of filters and, like me, always have identity primary keys, it will fit in most cases
Usage Examples (not good examples since 'Northwind..Products' only has 77 rows): :->
--return 100 random rows in a random order with all Products columns
exec spGetRandomRows 'Northwind..Products','ProductID'
--return 1000 random rows in a random order with columns ProductID,ProductName
exec spGetRandom 'Northwind..Products', 'ProductID', 1000, 'ProductID,ProductName'
--return 1000 random rows with columns ProductID, SupplierID, ProductName ordered by SupplierID and ProductName
exec spGetRandomRows 'Northwind..Products', 'ProductID', 1000, 'ProductID,SupplierID,ProductName', 'SupplierID,ProductName'
create proc spGetRandomRows
@source_table sysname,
@source_key sysname,
@return_count bigint = 100,
@return_columns varchar(200) = '',
@return_order varchar(200) = ''
as
set nocount on
select
@return_columns = case when len(@return_columns) > 0 then @return_columns else @source_table+'.*' end,
@return_order = case when len(@return_order) > 0 then ' order by '+@return_order else ' order by f.the_random_order' end
declare @sql varchar(8000)
if @return_count > 0
begin
set @sql = '
declare @i bigint, @count bigint, @return_count bigint
select @i = 0, @return_count = '+convert(varchar(20),@return_count)+'
declare @the_random_keys table(the_random_key bigint, the_random_order bigint identity(1,1) primary key)
declare @the_max_key table(the_max_key float, the_row_count bigint)
insert into @the_max_key(the_max_key,the_row_count) select max('+@source_key+'), count(*) from '+@source_table+'
select @return_count = case when @return_count < the_row_count then @return_count else the_row_count end from @the_max_key
while @i < @return_count
begin
declare @the_random_key bigint
while @the_random_key is null
select top 1 @the_random_key = o1.'+@source_key+' from '+@source_table+' o1 with(nolock)
where '+@source_key+' >
(select rand() * the_max_key from @the_max_key)
and o1.'+@source_key+' not in (select the_random_key from @the_random_keys)
insert into @the_random_keys(the_random_key)values(@the_random_key)
set @the_random_key = null
set @i = @i + 1
end
select '+@return_columns+' from '+@source_table+' with(nolock)
inner join @the_random_keys f on f.the_random_key = '+@source_table+'.'+@source_key
+@return_order
exec(@sql)
end
else
select null
|
|
|
|
 |
|
 |
easy ,simple,gr8 and does the necessary! this is what i love!
Thanks!is the most useful line of code i have seen in weeks
Kill The Tester
|
|
|
|
 |
|
|
 |