Calculating a new interim value and then sorting by the new value


I have a table 'myTable' in a database with columns 'xxx' and 'yyy'. I would like to calculate a new column 'rating' based on some conditional statements and then sort the record set using the new column.

At the moment, I call the record set and store the values in PHP variables, then use the following PHP conditional statements to assign the rating:

	echo "ERROR: Cant caclulate rating - arguments missing";
	$msg="ERROR: Cant caclulate rating - arguments missing";

Open in new window

The rating is then output to the screen.

This approach is ok, but it doesnt allow me to sort the information by this new rating value. Could someone suggest a method that i might use (preferably supply the SQL statement) to enable this operation to be processed by the MySQL statement?

LVL 12
Dean OBrienAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Brian CroweDatabase AdministratorCommented:
SELECT xxx, yyy,
            WHEN xxx > 10 AND yyy > 1000 THEN 5
            WHEN xxx > 7 AND yyy > 100 THEN 4
            WHEN xxx > 10 AND yyy > 70 THEN 4
            WHEN xxx > 7 AND yyy > 70 THEN 3
            WHEN xxx > 3 AND yyy > 70 THEN 3
            WHEN xxx > 7 AND yyy > 30 THEN 2
            WHEN xxx > 3 AND yyy > 70 THEN 2
            ELSE 1
      END AS rating
FROM myTable

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
You can create calculated fields in SQL Server, and then do things like order queries on the table by the value in a calculated field.

Here's a very simple table to try playing with:

CREATE TABLE [dbo].[__MyTest](
	[id] [int] NULL,
	[one] [int] NULL,
	[two] [int] NULL,
	[sum]  AS ([one]+[two]) PERSISTED,
	[product]  AS ([one]*[two]) PERSISTED

Open in new window

Fill in the first two fields and then check out what you get - like this:
insert into __MyTest (id, one, two) select 1, 2, 4
select * from __MyTest

Open in new window


Ray PaseurCommented:
If you can't get the results in the SQL query, the theory and practice here may be helpful.
Dean OBrienAuthor Commented:
Thanks to everyone for their contributions.

Brian's solution did the trick perfectly.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.