• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

tough logic question

 
vishal gaiky
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello friends,
i have one database in that i have 100 questions..out or hudred i want to show any25 rows..and out of 25 row i want to show 1-5 number rows of database compulsary..how to achieve it
 
Ryan McGuire
Ranch Hand
Posts: 1082
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vishal gaiky wrote:hello friends,
i have one database in that i have 100 questions..out or hudred i want to show any25 rows..and out of 25 row i want to show 1-5 number rows of database compulsary..how to achieve it



I don't quite understand the question. Do you want to display 25 or 5 rows? And when you say "any25" do you mean you want to pick 25 random rows?

Since I know a clever (I like to think) way to show some number of random rows from a table in T-SQL / SQL Server, I'm going to pretend that that's what you're looking for:

Each time NEWID() is called, it returns a random 128 bit identifier. Each time the SELECT statement is run, a new temporary id is assigned to each row. Then the rows are sorted by this random number. Finally, the first 25 (or however many) rows are retrieved.

If you can clarify your question, we may be able to refine the solution.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12203
35
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I also don't see how this is a "logic" question, so much as an "SQL" question...
 
Ryan McGuire
Ranch Hand
Posts: 1082
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:I also don't see how this is a "logic" question, so much as an "SQL" question...

I suppose we could get into a discussion on whether SQL is logical.
 
vishal gaiky
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i want to display any 25 questions...5 questions per page..and total five pages.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12203
35
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that these two things are completely unrelated:

1) Getting a random 25 questions from a database
2) Assuming you have 25 questions, display them 5 per page.

I'd even make it more generic...given a list of some unknown size, display them X per page. that way, if you later decide you want 30 questions 3 per page, or 100 questions 10 per page, or 5 questions 1 per page, your method is still usable.

So...do you want help getting the random number of questions, or do you need help formatting/printing them X-per-page?
 
Ryan McGuire
Ranch Hand
Posts: 1082
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:Note that these two things are completely unrelated:

1) Getting a random 25 questions from a database
2) Assuming you have 25 questions, display them 5 per page.

I'd even make it more generic...given a list of some unknown size, display them X per page. that way, if you later decide you want 30 questions 3 per page, or 100 questions 10 per page, or 5 questions 1 per page, your method is still usable.

So...do you want help getting the random number of questions, or do you need help formatting/printing them X-per-page?


Given my development environment, I'd use a Java, C# or Perl program to issue the SQL command mentioned earlier to select Y (25) questions from the DB and then print them out X (5) per page with appropriate spacing, control characters, etc.

Would anyone use a pure SQL solution to pick the questions and generate the output? If so, how?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic