Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 按条件排序_Sql_Postgresql_Postgresql 9.2 - Fatal编程技术网

Sql 按条件排序

Sql 按条件排序,sql,postgresql,postgresql-9.2,Sql,Postgresql,Postgresql 9.2,我有一个表,其中有一组带有开始标签和结束标签的路由。每行有一列progres,它是应用全局ORDERBY子句的列,最后是一个选择列,它告诉您哪些标签类型必须按奇数、偶数或全部排序。 如果LabelStart>LabelEnd=>按ASC排序,否则按DESC排序 例如,下面是路径 Routes ID RouteID, Progres, LabelStart, LabelEnd Type 1 1 5 1 21 O 2 1

我有一个表,其中有一组带有开始标签和结束标签的路由。每行有一列progres,它是应用全局ORDERBY子句的列,最后是一个选择列,它告诉您哪些标签类型必须按奇数、偶数或全部排序。 如果LabelStart>LabelEnd=>按ASC排序,否则按DESC排序

例如,下面是路径

Routes ID RouteID, Progres, LabelStart, LabelEnd Type 1 1 5 1 21 O 2 1 10 10 2 E 4 2 15 2 25 A 5 3 20 1 11 O 6 3 22 4 10 E 7 4 30 5 11 O 8 4 31 2 12 E 这里是属于路线的点

Points PoinID RouteID, Label 1 1 3 2 1 2 4 1 1 5 1 8 6 1 5 7 1 6 8 1 9 9 1 21 10 1 10 11 1 11 12 2 1 13 2 2 14 2 12 15 2 3 16 2 25 17 2 14 ... 我需要的是一个表格,其中所有的点都按照路由进程进行全局排序,根据类型按偶数、奇数或全部分组,如果LabelStart>LabelEnd else by DESC,则最终由ASC排序。结果应该是:

ID RouteID, PointID 1 1 4 2 1 1 4 1 6 5 1 8 6 1 11 7 1 9 8 1 10 9 1 5 10 1 7 11 1 2 12 2 13 13 2 15 ...
使用CASE语句可能很容易处理分组。动态切换排序顺序虽然。。。您可能必须使用PL/PgSQL函数生成执行格式的查询。。。使用。。。因此,为什么案例结构不适用于order by子句/确实非常好。不管怎样,我试图动态地设置Asc和Desc顺序,但没有成功
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