Ask a Question related to ASP Database, Design and Development.
-
Luke #1
Random records (but with logic)
Hi,
I need to extract randomly 5 records from the table "Questions". Now I use
SELECT TOP 5 FROM Questions ORDERBY NEWID()
And it works. The problem is that I need an additional thing: if SQL
extracts record with ID=4, then it should not extract record with ID=9,
because they are similar. I mean, I'd like something to tell SQL that if it
extracts some questions, then it SHOULD NOT extract other ones.
How can I do it?
Thanks!
Luke
Luke Guest
-
Random records from MySQL
is there any way i can get my page to pull a record ot of a table at random in PHP and MySQL?????????:confused; -
random records from Access?
Hi I'm using a Windows 2003 server an access database, with ODBC connection Is there anywhere I can find info about displaying all records in... -
all records in random orde?
Ok, this has probably been asked a thousand times, apologies for repeating.. Is there a tutorial or extension to display all records from a... -
Using Math.random to go to random frames
Hello everyone. My cerebral density is preventing me from seeing the solution to this problem. I have the following code which causes the user to... -
Random image in a random place.
Anyone know javascript? I have a grid(4 x 4) of 16 spacer images and a few text links on the side. Each text link represents a different folder of... -
Bob Barrows [MVP] #2
Re: Random records (but with logic)
Luke wrote:
I can't think of any way that does not involve using a cursor.> Hi,
>
> I need to extract randomly 5 records from the table "Questions". Now
> I use
>
> SELECT TOP 5 FROM Questions ORDERBY NEWID()
>
> And it works. The problem is that I need an additional thing: if SQL
> extracts record with ID=4, then it should not extract record with
> ID=9, because they are similar. I mean, I'd like something to tell
> SQL that if it extracts some questions, then it SHOULD NOT extract
> other ones.
>
My suggestion would be to do TOP 6 instead of TOP 5, While processing the
returned recordset, , first check to see if the ID=4 record was retrieved,
if so, ignore the ID=9 record.
Sorry,
Bob Barrows
--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.
Bob Barrows [MVP] Guest
-
Bob Barrows #3
Re: Random records (but with logic)
Luke wrote:
Wait a minute. What makes these two rows similar? Perhaps we could do> Hi,
>
> I need to extract randomly 5 records from the table "Questions". Now
> I use
>
> SELECT TOP 5 FROM Questions ORDERBY NEWID()
>
> And it works. The problem is that I need an additional thing: if SQL
> extracts record with ID=4, then it should not extract record with
> ID=9, because they are similar. I mean, I'd like something to tell
> SQL that if it extracts some questions, then it SHOULD NOT extract
> other ones.
>
> How can I do it?
>
something with that ...
Bob Barrows
--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"
Bob Barrows Guest
-
Aaron Bertrand [MVP] #4
Re: Random records (but with logic)
> because they are similar.
We need this information, rather than the fact that their IDs are 4 and 9.
A
Aaron Bertrand [MVP] Guest
-
Luke #5
Re: Random records (but with logic)
"Bob Barrows" <reb01501@NOyahoo.SPAMcom> ha scritto nel messaggio
news:#AuRiL2GEHA.2224@TK2MSFTNGP12.phx.gbl...Thanks,> Wait a minute. What makes these two rows similar? Perhaps we could do
> something with that ...
There is nothing in the database that I can use to see if questions are
similar or not. My customer just told me to extract 50 questions (out of
100) but:
- Questions 4 and 9 should not be extracted together
- Questions 11 and 12 should not be extracted together
- If question 25 is extracted, then also question 26 should be extracted
- From question 50 to 60, only max 2 questions can be extracted
....
I like the idea to make a TOP 6 (in my first message the select was made
simpler), but I can I cycle through records and see if question 4 and 9 have
been extracted?
Thanks.
Luke
Luke Guest
-
Luke #6
Re: Random records (but with logic)
"Aaron Bertrand [MVP]" <aaron@TRASHaspfaq.com> ha scritto nel messaggio
news:#nUydQ5GEHA.576@TK2MSFTNGP11.phx.gbl...Aaron, see my reply to Bob.>> > because they are similar.
> We need this information, rather than the fact that their IDs are 4 and 9.
>
> A
Thanks,
Luke
Luke Guest
-
Luke #7
Re: Random records (but with logic)
> "Aaron Bertrand [MVP]" <aaron@TRASHaspfaq.com> ha scritto nel messaggio
9.> news:#nUydQ5GEHA.576@TK2MSFTNGP11.phx.gbl...> >> > > because they are similar.
> > We need this information, rather than the fact that their IDs are 4 and
My Customer only told me not to return a recordset with both IDs 4 and 9
together (I can have a recordset with ID 4 only, or with ID 9 only, but NOT
with both IDs 4 and 9).
Thanks,
Luke
Luke Guest
-
Bob Barrows #8
Re: Random records (but with logic)
Luke wrote:
That means we are going to have to use a union to make sure 26 is always> "Bob Barrows" <reb01501@NOyahoo.SPAMcom> ha scritto nel messaggio
> news:#AuRiL2GEHA.2224@TK2MSFTNGP12.phx.gbl...>>> Wait a minute. What makes these two rows similar? Perhaps we could do
>> something with that ...
> Thanks,
>
> There is nothing in the database that I can use to see if questions
> are similar or not. My customer just told me to extract 50 questions
> (out of 100) but:
>
> - Questions 4 and 9 should not be extracted together
> - Questions 11 and 12 should not be extracted together
> - If question 25 is extracted, then also question 26 should be
> extracted
selected.
Well, we're going to need more than 6 records - 17 should do it, plus the> - From question 50 to 60, only max 2 questions can be extracted
unioned record from above.
> ...
>
> I like the idea to make a TOP 6 (in my first message the select was
> made simpler), but I can I cycle through records and see if question
> 4 and 9 have been extracted?
>
> Thanks.
>
> Luke
I will illustrate this using dynamic sql, but I urge you to use a stored
procedure instead.
This is untested air code:
dim conn,rs,sSQL,arData, arFiftyToSixty(1), bFourNine
dim bElevenTwelve,bTwentyFive, i
sSQL="SELECT TOP 17 0 as rank, NewID() as sortid, " & _
"id, <column list> FROM table UNION ALL " & _
"SELECT 1,26,id,<column list> FROM table " & _
"WHERE id=26 ORDER BY rank,sortid"
'instantiate and open connection using conn variable, then
set rs=conn.execute(" select top 16 ... ",,1)
if not rs.eof then arData=rs.getrows
rs.close:set rs=nothing
conn.close:set conn=nothing
for i = 0 to ubound(arData,2)
select case arData(2,i) 'the id value
case 4,9
if not bFourNine then
'write the data to the Response
bFourNine=true
end if
case 11,12
if not bElevenTwelve then
'write the data to the Response
bElevenTwelve=true
end if
case 50-60
if arFiftyToSixty(0) = "" then
arFiftyToSixty(0) = arData(2,i)
'write the data to the Response
elseif arFiftyToSixty(1) = "" then
arFiftyToSixty(1) = arData(2,i)
'write the data to the Response
end if
case 25
'write the data to the Response
'write the data in arData(,17) to the Response
end select
next
I would write a function that accepts the arData array and the row index to
write the data to the Response.
I have to go. Post back if you need more details
Bob Barrows
--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"
Bob Barrows Guest
-
Roland Hall #9
Re: Random records (but with logic)
"Luke" wrote in message news:Byscc.19225$hc5.928552@news3.tin.it...
: "Bob Barrows" <reb01501@NOyahoo.SPAMcom> ha scritto nel messaggio
: news:#AuRiL2GEHA.2224@TK2MSFTNGP12.phx.gbl...
: - Questions 4 and 9 should not be extracted together
: - Questions 11 and 12 should not be extracted together
: - If question 25 is extracted, then also question 26 should be extracted
: - From question 50 to 60, only max 2 questions can be extracted
If 26 is extracted, must 25 also be extracted?
If 25 is extracted, must 26 be the next question or can they be in any
order?
--
Roland Hall
/* This information is distributed in the hope that it will be useful, but
without any warranty; without even the implied warranty of merchantability
or fitness for a particular purpose. */
Technet Script Center - [url]http://www.microsoft.com/technet/scriptcenter/[/url]
WSH 5.6 Documentation - [url]http://msdn.microsoft.com/downloads/list/webdev.asp[/url]
MSDN Library - [url]http://msdn.microsoft.com/library/default.asp[/url]
Roland Hall Guest
-
Mark Schupp #10
Re: Random records (but with logic)
If you have the option of modifying the database you would probably be
better of using "question pools". Similar questions would belong in the same
"pool" and you would only choose a single question from those pools. You
could also have a different type of pool where you choose a fixed number of
questions randomly from that pool.
You could then create a union of select statements, program logic, or a
stored procedure to collect the questions.
--
Mark Schupp
Head of Development
Integrity eLearning
[url]www.ielearning.com[/url]
"Luke" <nospam@nospam.com> wrote in message
news:4Cscc.19233$hc5.929396@news3.tin.it...and> > "Aaron Bertrand [MVP]" <aaron@TRASHaspfaq.com> ha scritto nel messaggio
> > news:#nUydQ5GEHA.576@TK2MSFTNGP11.phx.gbl...> > > > because they are similar.
> > >
> > > We need this information, rather than the fact that their IDs are 4NOT> 9.
>
> My Customer only told me not to return a recordset with both IDs 4 and 9
> together (I can have a recordset with ID 4 only, or with ID 9 only, but> with both IDs 4 and 9).
>
> Thanks,
>
> Luke
>
>
Mark Schupp Guest
-
Luke #11
Re: Random records (but with logic)
"Roland Hall" <nobody@nowhere> ha scritto nel messaggio
news:eTUi0M9GEHA.3576@tk2msftngp13.phx.gbl...Yes> "Luke" wrote in message news:Byscc.19225$hc5.928552@news3.tin.it...
> : "Bob Barrows" <reb01501@NOyahoo.SPAMcom> ha scritto nel messaggio
> : news:#AuRiL2GEHA.2224@TK2MSFTNGP12.phx.gbl...
> : - Questions 4 and 9 should not be extracted together
> : - Questions 11 and 12 should not be extracted together
> : - If question 25 is extracted, then also question 26 should be extracted
> : - From question 50 to 60, only max 2 questions can be extracted
>
> If 26 is extracted, must 25 also be extracted?
Any order.> If 25 is extracted, must 26 be the next question or can they be in any
> order?
Thanks,
Luke
Luke Guest
-
Roland Hall #12
Re: Random records (but with logic)
"Luke" wrote in message news:tkAcc.21206$hc5.1004536@news3.tin.it...
: "Roland Hall" <nobody@nowhere> ha scritto nel messaggio
: news:eTUi0M9GEHA.3576@tk2msftngp13.phx.gbl...
: > "Luke" wrote in message news:Byscc.19225$hc5.928552@news3.tin.it...
: > : "Bob Barrows" <reb01501@NOyahoo.SPAMcom> ha scritto nel messaggio
: > : news:#AuRiL2GEHA.2224@TK2MSFTNGP12.phx.gbl...
: > : - Questions 4 and 9 should not be extracted together
: > : - Questions 11 and 12 should not be extracted together
: > : - If question 25 is extracted, then also question 26 should be
extracted
: > : - From question 50 to 60, only max 2 questions can be extracted
: >
: > If 26 is extracted, must 25 also be extracted?
:
: Yes
Dayum. I was afraid you were going to say that. I thought I was done
although I took a completely different approach for a solution. I'll post
when I'm ready.
: > If 25 is extracted, must 26 be the next question or can they be in any
: > order?
:
: Any order.
Oh good. ~sigh~ of relief.
--
Roland Hall
/* This information is distributed in the hope that it will be useful, but
without any warranty; without even the implied warranty of merchantability
or fitness for a particular purpose. */
Technet Script Center - [url]http://www.microsoft.com/technet/scriptcenter/[/url]
WSH 5.6 Documentation - [url]http://msdn.microsoft.com/downloads/list/webdev.asp[/url]
MSDN Library - [url]http://msdn.microsoft.com/library/default.asp[/url]
Roland Hall Guest
-
Roland Hall #13
Re: Random records (but with logic)
"Roland Hall" wrote in message news:eOFlxKGHEHA.3832@TK2MSFTNGP10.phx.gbl...
: "Luke" wrote in message news:tkAcc.21206$hc5.1004536@news3.tin.it...
: : "Roland Hall" <nobody@nowhere> ha scritto nel messaggio
: : news:eTUi0M9GEHA.3576@tk2msftngp13.phx.gbl...
: : > "Luke" wrote in message news:Byscc.19225$hc5.928552@news3.tin.it...
: : > : "Bob Barrows" <reb01501@NOyahoo.SPAMcom> ha scritto nel messaggio
: : > : news:#AuRiL2GEHA.2224@TK2MSFTNGP12.phx.gbl...
: : > : - Questions 4 and 9 should not be extracted together
: : > : - Questions 11 and 12 should not be extracted together
: : > : - If question 25 is extracted, then also question 26 should be
: extracted
: : > : - From question 50 to 60, only max 2 questions can be extracted
: : >
: : > If 26 is extracted, must 25 also be extracted?
: :
: : Yes
:
: Dayum. I was afraid you were going to say that. I thought I was done
: although I took a completely different approach for a solution. I'll post
: when I'm ready.
:
: : > If 25 is extracted, must 26 be the next question or can they be in any
: : > order?
: :
: : Any order.
Hi Luke..
Here is what I came up with since I don't have Bob's expertise with SQL. I
chose to obtain the 5 random numbers, with the restrictions you mentioned,
(4 OR 9, 11 OR 12, if 25 then also 26, if 26 then also 25, 2 only from range
50-60) first and then you could just plug that into your SQL statement to
pick the records you wanted.
This requires your ID field be sequential and specific if it is
autogenerated. If not, then a separate field that can be controlled would
be required for the index.
You can test it here: [url]http://kiddanger.com/lab/random.asp[/url]
I made it visual so you can see what is returned from the initial sort of
100 numbers and what the result is after applying the restrictions. Just
refresh the page to see the different results.
HTH...
--
Roland Hall
/* This information is distributed in the hope that it will be useful, but
without any warranty; without even the implied warranty of merchantability
or fitness for a particular purpose. */
Technet Script Center - [url]http://www.microsoft.com/technet/scriptcenter/[/url]
WSH 5.6 Documentation - [url]http://msdn.microsoft.com/downloads/list/webdev.asp[/url]
MSDN Library - [url]http://msdn.microsoft.com/library/default.asp[/url]
Roland Hall Guest



Reply With Quote

