0

I have a column which can take any values from 0 to 100.

Now i have a type TYPE1 which can take values 2, 4, 16

TYPE2 which can take values 8,12,64.

Now i want to sort the column by TYPE1 values first and then TYPE2 values. Is there any way to do that.

My column has only these values. It does not know the Type1 or Type2. I am using SQL2005

NOTE: TYPE1 and TYPE2 are not columns. They are just logical entities.

2
  • "My column has only these values" -- how does your one column hold three values e.g. are you using CREATE TYPE TYPE1 AS TABLE...? Commented Jun 24, 2011 at 9:35
  • By these values i mean they hold numbers ranging from 0 to 100 in each no row. It does not hold 3 values in a single column for a row. Commented Jun 24, 2011 at 9:37

5 Answers 5

3

You can specify both columns in ORDER BY caluse:

SELECT * FROM yourTable ORDER BY Type1, Type2

This would by default sort the fields in ascending order. If you want to order in reverse, you can use DESC keyword just after column names.

Sign up to request clarification or add additional context in comments.

2 Comments

@ckv: You can replace Type1 and Type2 with your actual column names you want to sort against, the syntax will be the same.
Hey Sarfraz, Yeah i can replace the column name with exact name but the problem is i will get sorted in a mixed way. I want to sort in such a way that i get 2, 4, 16 as one set and rest as other. Because the TYPE2 has a value 8 which will come in between.
2

What are TYPE1 and TYPE2 and what is your database? In MySQL you can do ORDER BY FIELD(columnnane, 2,4,16,8,12,64)

Comments

0
SELECT *
FROM table
ORDER BY type1, type2 -- the columns you want to sort in order

Comments

0

You can add multiple fields to the Order By Clause

select type1, type2 from table order by type1, type2

Comments

0

use a case to separate type 1 and type 2, then order by the column:

SELECT * FROM yourTable ORDER BY case when column in (2,4,16) then 1, else 2 end,column

This way, first you order by type 1 or 2, them order by value of the column

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.