开发者

make Multiple Order BY ignore some values and go to the next

Well I want to make my Multiple (actually only 2) order by to work this way.

I h开发者_JAVA百科ave two columns "valorMensal" Integer and "ordem_alternativa" Integer

Here is the piece of my query:

ORDER BY valorMensal DESC, ordem_alternativa DESC;

I want the SQL to fetch first the valorMensal and put it in order in which "valorMensal" integer is higher and only till it reaches 1, since the next number is 0 I don't want it to get the 0, I want it to go to the "ordem_alternativa" and get all its values in DESC and again till it reaches 0 then go back to "valorMensal" get the 0's left and then go to "ordem_alternativa" and get the 0's left.

Example here is the table:

id valorMensal    ordem_alternativa
1      0                  6
2      0                  5
3      1                  2
4      3                  1
5      4                  1
6      0                  0
7      0                  0

the ResultSet I want it to be like this:

id 5, 4, 3, 2 (because of ordem_alternativa being 5), 1 (because of ordem_alternativa being 6), 6, 7

How do I do that? ^^ I hope I'm being clear.


If the values of ordem_alternativa fall between 0..N (0 to N), you can do it something like this. I have not tested it though, but it should work:

ORDER BY
CASE 
WHEN valorMensal = 0 THEN (N - ordem_alternativa)
ELSE valorMensal
END DESC

Also, you should have marked the question with "SQL" tag.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜