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
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAREAAAD8CAIAAADfWwcxAAAVtUlEQVR4nO2dPXLjOBOGcStVycmEuoMCre6gyImndAhHXF9Bob9yonTCqS3nPgq/QBQJEP9AN4Am+i3W1ixNAd0gHgKk8Iri9+/fv379GtGEWnj16gqIXEYFAq7bJmJm5vvvH/BNBhKj/LrVcUZVAsa+ynvFzDS9kcuImSHQghWr44yqBMzMEK4uYvt8fdm/fj3+IRadh4IZpcYAcwpslcZHxcw0Wd18IqE2uWcsJd+ue0+XNWfkCC/kT/Ixq+OzE3cxY0w8vmWYmSarK8SMvyJ0Zv6+n8Xxo8ApsCUe3zJdMvP5+rI/nvdCiOPHMiIfP/7++R6O0z+Wc/l+Frvr55+vt93juJe3W/QJ+/t+lgd904VWKv/9uhdCCLGeMxw/lOCXI6eQliN3108tTWPP8HVZU0Y3e3jKnz5O6lTHMc5ISU3FrlpMzy6TmTnx+JbplZmp3Zch+OttJ07v339v1/3u+vmARz7TUhPbO5nlhMkD/ftZ7K6Dzoxa/nL+tAjn4OV/7F+/5sjljijUPYaeIX0q7qJjD0+/PC9g6HcOtlzUFvvUsstl5llgfMv0yox9Yv1xEufhz8dpdx1eX07v38Px5e02X1wdg0xADzPtkXqJMmK4LsmmSYV6CyvMZIKMM2HhrdgwRm4r0HjAKjsL5zzOVGDmezi+vL2eH9f+/evH206+F/w4PedIwMyo5ScyowZmrAX2fsbFzDSndUUex0zQbQ/fzyAzY5ibPU72dL1fCFGmc7EXOX2mIV3DhqNQ5lG3614eIuyTn/U/1GdBygQP77mZY25myjGQGfPcTM0uixl+bpZ+yqVB39hew/EJEugzAKk0sd9pzwBuz+NnXNV7busFcq5FnamvDpamOK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amqexMzki1xGvTCTYV72iJnJFLmMemGGN96obBWBWZipGwSLRUjMDIsVJ2aGxYoTM8NixYmZYbHi9H/d6P50hiqisAAAAABJRU5ErkJggg==" alt="" />
可以用户讲多列的数据排序后横向比较,例如,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
结果如下: