0
How to correctly use ORDER BY CASE WHEN with more than 9 records ?
In the example below I am using a case when with more than 9 records and when it arrives at position 10 , 11 ,12 e 13 the order is wrong.
What is the correct way to use in this case ?
ORDER BY CASE descricao
WHEN 'Rec. Op. Liquida' THEN 0
WHEN 'VENDA MERCADORIA' THEN 1
WHEN 'ICMS S.T S/ VENDA' THEN 2
WHEN 'DEVOLUÇÃO DE VENDA' THEN 3
WHEN 'VENDA LÍQUIDA' THEN 4
WHEN 'ICMS' THEN 5
WHEN 'ICMS ST' THEN 6
WHEN 'TOTAL ICMS' THEN 7
WHEN 'PIS' THEN 8
WHEN 'COFINS' THEN 9
WHEN 'TOTAL PIS/COFINS' THEN 10
WHEN 'DEV X FAT' THEN 11
WHEN 'BONF X FAT' THEN 12
WHEN 'BONIFICAÇÃO' THEN 13
ELSE descricao END;
In this image is the result and side the position of the element in the result.
Example in Sqlfiddle Sqlfiddle, I couldn’t get it right why I use many variables and multiple querys, if anyone can adjust to the example look better thanks, but the result can be seen in the image.