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
    

    结果如下:

  • 相关阅读:
    自动布局
    初探 iOS8 中的 Size Class
    iOS数据安全性问题
    iOS应用程序之间的跳转
    iOS 关于xml解析的问题
    iOS中的一些基础知识点
    关于iOS项目中使用ZBarSDK
    iOS中关于google地图的用法
    基于4.5Framework web程序、SQLSERVER数据库打包
    docker私有仓库搭建
  • 原文地址:https://www.cnblogs.com/alangwansui/p/5345183.html
Copyright © 2011-2022 走看看