Advertisement

03.05.2008 at 11:37AM PST, ID: 23217192
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

9.5

Can you alias column values to change sort order using SQL?

Asked by maxmohave in SQL Query Syntax, MySQL Server

Tags: , ,

Question is can you alias a column value to change sort order in SQL?

Background:
We created tables prepending numbers to change fix our search order. We created a column called status in filled in weather status values. Original we wanted "weather watch" to go first. So we prepended a number. Other values including Hurricane, Tornado, snow and clear weather as below:

05 Weather Watch
10 Hurricane Watch
15 Tornado Watch
20 Snow Watch
25 Clear Weather

Then when we sorted by status weather watch would always go first. For example,

select * from mysort order by region,status;
+----+--------------------+------------+-------------+
| id | status                        | region     | city        |
+----+--------------------+------------+-------------+
|  1 | 05 Weather Watch   | East Coast | New York  |
|  7 | 20 Snow Watch       | East Coast | Boston      |
|  8 | 25 Clear Weather     | East Coast | New Jersey  |
|  5 | 05 Weather Watch   | Gulf       | Seattle     |
|  2 | 10 Hurricane Watch | Gulf       | New Orleans |
|  3 | 25 Clear Weather   | Gulf       | Tampa       |
|  9 | 25 Clear Weather   | Gulf       | Houston     |
|  6 | 10 Hurricane Watch  | Midwest    | St Louis    |
|  4 | 15 Tornado Watch   | Midwest    | Kansas City |
+----+--------------------+------------+-------------+

Problem is now is for some sorts we want "05 Weather Watch" to go first,.

But for others we want 10 Hurricane Watch to go first.
And for the other categories, so we don't care what comes:

after "10 Hurricane Watch" it can be "05 Weather watch" or tornado watch, etc. As long as the region
is in order (east coast, gulf, midwest).

I have included my very simple table and query to show what I'm working with. Right now. I believe only solution is to do two sorts.

Is that correct? Thank you, craigStart Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
create DATABASE test;
use test;
 
drop table mysort;
 
create table mysort (
	id int unsigned not null auto_increment,
	status varchar(40) not null,
	region varchar(40) not null,
	city varchar(50) not null,
	primary key (id)
);
insert into mysort values (1,"05 Weather Watch","East Coast","New York");
insert into mysort values (2,"10 Hurricane Watch","Gulf","New Orleans");
insert into mysort values (3,"25 Clear Weather","Gulf","Tampa");
insert into mysort values (4,"15 Tornado Watch","Midwest","Kansas City");
insert into mysort values (5,"05 Weather Watch","Gulf","Seattle");
insert into mysort values (6,"10 Hurricane Watch","Midwest","St Louis");
insert into mysort values (7,"20 Snow Watch","East Coast","Boston");
insert into mysort values (8,"25 Clear Weather","East Coast","New Jersey");
insert into mysort values (9,"25 Clear Weather","Gulf","Houston");
 
select * from mysort order by region,status;
 
Loading Advertisement...
 
[+][-]03.05.2008 at 11:59AM PST, ID: 21053851

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: SQL Query Syntax, MySQL Server
Tags: mysql (soon to be sun), Mysql, 5.1
Sign Up Now!
Solution Provided By: dqmq
Participating Experts: 1
Solution Grade: A
 
 
[+][-]03.05.2008 at 12:45PM PST, ID: 21054288

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628