PostgreSQL
 sql >> Base de données >  >> RDS >> PostgreSQL

Tri conditionnel par

SQL Fiddle

select 
    row_number() over() id, *
from (
    select
        r.routeid,
        p.pointid,
        label,
        type,
        labelstart,
        labelend
    from
        route r
        inner join
        point p on p.routeid = r.routeid
    where
        r.type = 'E' and p.label % 2 = 0
        or
        r.type = 'O' and p.label % 2 != 0
        or
        r.type = 'A'
    order by
        r.routeid, r.progres, r.id,
        case labelstart < labelend
            when true then label
            else label * - 1
        end
) s