Wrox Programmer Forums
|
Classic ASP Databases Discuss using ASP 3 to work with data in databases, including ASP Database Setup issues from the old P2P forum on this specific subtopic. See also the book forum Beginning ASP.NET Databases for questions specific to that book. NOT for ASP.NET 1.0, 1.1, or 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Classic ASP Databases section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 31st, 2007, 02:19 AM
Authorized User
 
Join Date: Oct 2006
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
Default ASP- SQL Server..Query help....

Hello,

I am using ASP and MS SQL Sever 2000 as backend database..

I am writing a SQL query which will pick up top 5 recipes details who have scored the highest marks from the following table

tblrecipes -

Code:
 ID |  recipe_name | Total
[u]The following points to be noted:</u>

- The table has 500 records.
- 50 of them have achieved the highest marks ie 100 marks each
- My following query is picking up top 5 records who have scored 100 marks

Code:
SELECT TOP 5 ID, total, recipe_name
FROM tblrecipes 
ORDER BY total DESC
and getting this output:

http://img2.freeimagehosting.net/uploads/3d14a5272a.jpg


Now my question is:

On what basis its showing up this sequence of listing? I mean why the ID 89 is on the top and 136 at the bottom? Why the ID is not the other way round?

Like the following?

ID
__
178
136
89
286
340


Can someone explain please?


Thanx

-----------------------------------------------
www.chargertek.in - Cheapest WebHosting
__________________
-----------------------------------------------
www.chargertek.in - Cheapest WebHosting
 
Old January 31st, 2007, 06:47 AM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 683
Thanks: 0
Thanked 1 Time in 1 Post
Default

You are only ordering your query by total and not by ID in any way, therefore the order of the ID's returned will be unpredictable.

How do you want to order your results?

Cheers,

Chris

 
Old January 31st, 2007, 07:01 AM
Authorized User
 
Join Date: Oct 2006
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
Default

By saying "unpredictable" do you mean "random"?

I dnt want it to order any other way I just want to know why is it generating that way?

-----------------------------------------------
www.chargertek.in - Cheapest WebHosting
 
Old January 31st, 2007, 09:49 AM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

The reason it is ordering that way is because:

The total for id 178 is lower then 136 is lower then 89 etc.

Your ids will always be listed based upon your total column so, while your total column will always be sorted lowest to highest, your IDs will always be jumbled because you are sorting on the total column.

================================================== =========
I will only tell you how to do it, not do it for you.
Unless, of course, you want to hire me to do work for you.
================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from planoie's profile^^
^^Modified text taken from gbianchi profile^^
================================================== =========
 
Old January 31st, 2007, 01:17 PM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 599
Thanks: 6
Thanked 3 Times in 3 Posts
Default

What dparsons is saying, is that if you want the IDs in ascending order then use this SQL statement:

SELECT TOP 5 ID, total, recipe_name
FROM tblrecipes
ORDER BY ID

This will order you data in ascending order by default. If you want it the other way round, then:

ORDER BY ID DESC

 
Old February 1st, 2007, 06:26 AM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 683
Thanks: 0
Thanked 1 Time in 1 Post
Default

The result set will not be random, as if you run the query twice it will return the same result set. This may change as the data in your database changes as the db will just fetch the records and sort them by total with no regard for any of the other columns.

HTH,

Chris







Similar Threads
Thread Thread Starter Forum Replies Last Post
SQL server query file naeem.net SQL Server ASP 0 June 22nd, 2007 01:07 PM
SQL Server Crosstab Query elygp SQL Server 2000 2 May 5th, 2007 11:57 AM
SQL SERVER Query pavanpareta SQL Server ASP 2 December 18th, 2006 04:51 PM
Crosstab query in SQL Server Dhodie SQL Server 2000 3 June 17th, 2004 08:53 PM
Query runs in SQL Server but not in ASP Ron Howerton ASP.NET 1.0 and 1.1 Basics 2 July 14th, 2003 04:33 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.