473,545 Members | 1,899 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Quick query question

Hi

How do I force the order of a SQL query?

My table has a field called ModelName which could contain 1 of 5 vehicle
models. I want to force the query to order these in a particular order (not
alphabetical, but an order that I have determined ('Range Rover',
'Discovery', Land Rover', 'Jeep', 'Others')

Let's say my current query is:
"SELECT DISTINCT ModelName FROM tblVehicles"

What do I need to tag on the end to order the result as stated?

Thanks for help!

--
Jul 19 '05 #1
5 1502
Tim
add another numeric field called neworder and use that

Tim

"Pooley" <po****@here.co m> wrote in message
news:uK******** *****@tk2msftng p13.phx.gbl...
Hi

How do I force the order of a SQL query?

My table has a field called ModelName which could contain 1 of 5 vehicle
models. I want to force the query to order these in a particular order (not alphabetical, but an order that I have determined ('Range Rover',
'Discovery', Land Rover', 'Jeep', 'Others')

Let's say my current query is:
"SELECT DISTINCT ModelName FROM tblVehicles"

What do I need to tag on the end to order the result as stated?

Thanks for help!

--

Jul 19 '05 #2
Sure. Is that the only way to do it? Just trying to be 'elegant' about it ;)

--

"Tim" <me@nottellingy ou.com> wrote in message
news:eL******** ******@TK2MSFTN GP11.phx.gbl...
add another numeric field called neworder and use that

Tim

"Pooley" <po****@here.co m> wrote in message
news:uK******** *****@tk2msftng p13.phx.gbl...
Hi

How do I force the order of a SQL query?

My table has a field called ModelName which could contain 1 of 5 vehicle
models. I want to force the query to order these in a particular order

(not
alphabetical, but an order that I have determined ('Range Rover',
'Discovery', Land Rover', 'Jeep', 'Others')

Let's say my current query is:
"SELECT DISTINCT ModelName FROM tblVehicles"

What do I need to tag on the end to order the result as stated?

Thanks for help!

--


Jul 19 '05 #3
That is not only the "elegant" way to do it, it is also the most efficient
way to do it, since you can put an index on the column and get your results
very quickly.

There is another way, but it depends on knowing what your database is (type
and version)

HTH,
Bob Barros

Pooley wrote:
Sure. Is that the only way to do it? Just trying to be 'elegant'
about it ;)
"Tim" <me@nottellingy ou.com> wrote in message
news:eL******** ******@TK2MSFTN GP11.phx.gbl...
add another numeric field called neworder and use that

Tim

"Pooley" <po****@here.co m> wrote in message
news:uK******** *****@tk2msftng p13.phx.gbl...
Hi

How do I force the order of a SQL query?

My table has a field called ModelName which could contain 1 of 5
vehicle models. I want to force the query to order these in a
particular order (not alphabetical, but an order that I have
determined ('Range Rover', 'Discovery', Land Rover', 'Jeep',
'Others')

Let's say my current query is:
"SELECT DISTINCT ModelName FROM tblVehicles"

What do I need to tag on the end to order the result as stated?

Thanks for help!

--

Jul 19 '05 #4
Hi Bob

Thanks for that. I appreciate what you say (in fact I've already put Tim's
method into practice) but I'm interested to know your other method.
It's a SQL Server 2000 database.

--
Pooley
"Bob Barrows" <re******@NOyah oo.SPAMcom> wrote in message
news:OC******** ********@TK2MSF TNGP10.phx.gbl. ..
That is not only the "elegant" way to do it, it is also the most efficient
way to do it, since you can put an index on the column and get your results very quickly.

There is another way, but it depends on knowing what your database is (type and version)

HTH,
Bob Barros

Pooley wrote:
Sure. Is that the only way to do it? Just trying to be 'elegant'
about it ;)
"Tim" <me@nottellingy ou.com> wrote in message
news:eL******** ******@TK2MSFTN GP11.phx.gbl...
add another numeric field called neworder and use that

Tim

"Pooley" <po****@here.co m> wrote in message
news:uK******** *****@tk2msftng p13.phx.gbl...
Hi

How do I force the order of a SQL query?

My table has a field called ModelName which could contain 1 of 5
vehicle models. I want to force the query to order these in a
particular order (not alphabetical, but an order that I have
determined ('Range Rover', 'Discovery', Land Rover', 'Jeep',
'Others')

Let's say my current query is:
"SELECT DISTINCT ModelName FROM tblVehicles"

What do I need to tag on the end to order the result as stated?

Thanks for help!

--


Jul 19 '05 #5
SELECT DISTINCT ModelName FROM tblVehicles
ORDER BY
CASE ModelName WHEN 'Range Rover' THEN 0 ELSE 1 END,
CASE ModelName WHEN 'Discovery' THEN 0 ELSE 1 END,
CASE ModelName WHEN Land Rover' THEN 0 ELSE 1 END,
CASE ModelName WHEN 'Jeep' THEN 0 ELSE 1 END

Again, this will be inefficient, since a table or index scan will be
required. However, with a small number of records, it may perform
acceptably, since a scan will probably be done anyways with a small set of
records.

Bob Barrows

PS. I consider the original solution, the data-driven solution, to be the
more elegant since it can be modified at will without touching any of your
query code.

Pooley wrote:
Hi Bob

Thanks for that. I appreciate what you say (in fact I've already put
Tim's method into practice) but I'm interested to know your other
method.
It's a SQL Server 2000 database.
> My table has a field called ModelName which could contain 1 of 5
> vehicle models. I want to force the query to order these in a
> particular order (not alphabetical, but an order that I have
> determined ('Range Rover', 'Discovery', Land Rover', 'Jeep',
> 'Others')
>
> Let's say my current query is:
> "SELECT DISTINCT ModelName FROM tblVehicles"
>
> What do I need to tag on the end to order the result as stated?
>
> Thanks for help!
>
> --


Jul 19 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
7479
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7669
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7439
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7773
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5987
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5343
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3450
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1028
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
722
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.