Skip to content
Advertisement

SQL Query ORDER BY column values

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.

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement