开发者

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 wa开发者_StackOverflowy 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.


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.


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)


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


You can add multiple fields to the Order By Clause

select type1, type2 from table order by type1, type2


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜