zoukankan      html  css  js  c++  java
  • psql rank row

    rank() OVER (PARTITION BY f1 ORDER BY f2 DESC) 
    ROW_NUMBER() () OVER (PARTITION BY f1 ORDER BY f2 DESC) 
    为每个匹配的f1字段生成一个行号,并按照f2排序

    例子
    select DID,customerID,totalPrice,ROW_NUMBER() over(order by totalPrice) as rows from OP_Order


    可以用户讲多列的数据排序后横向比较,例如,oe的一个询价单,向多个供应商询价后,需要取前3个供应商价格横列显示。
    select 
            product_id,
            product_qty,
            max(part1) as parter1,
            max(price1) as price1,
            max(part2) as parter2,
            max(price2) as price2,
            max(part3) as parter3,
            max(price3) as price3
    from(
            select  
                    product_id, product_qty,
                    case  when  abc=1 then partner_id end as part1,
                    case  when  abc=2 then partner_id end as part2,
                    case  when  abc=3 then partner_id end as part3,
                    case  when  abc=1 then price_unit end as price1,
                    case  when  abc=2 then price_unit end as price2,
                    case  when  abc=3 then price_unit end as price3
            from (
                    select 
                            pol.id, pol.product_id, pol.partner_id, product_qty , pol.price_unit,
                            rank() OVER (PARTITION BY pol.product_id order by pol.partner_id) as abc 
                            from 
                                    purchase_requisition as rq
                                    left join purchase_order as po on  (po.requisition_id = rq.id)
                                    left join purchase_order_line as pol on (pol.order_id = po.id)
                            where rq.id = 44
                  ) as t
        ) as tt group by product_id,product_qty
    

    结果如下:

  • 相关阅读:
    poj-2376 Cleaning Shifts (排序+贪心)
    AOJ 0525
    POJ -3050 Hopscotch
    POJ-2718 Smallest Difference
    AOJ 0121: Seven Puzzle (BFS DP STL 逆向推理)(转载)
    POJ-3187 Backward Digit Sums (暴力枚举)
    POJ-3669 Meteor Shower(bfs)
    分布式消息系统Kafka初步
    nginx源码学习----内存池
    def文件格式
  • 原文地址:https://www.cnblogs.com/alangwansui/p/5345183.html
Copyright © 2011-2022 走看看