sql ROW_NUMBER() 排序函数 (转)

1使用row_number()函数进行编号:如

select email,customerID, ROW_NUMBER() over(order by psd) as rows from QT_Customer

  

原理:先按psd进行排序,排序完后,给每条数据进行编号。

2.在订单中按价格的升序进行排序,并给每条记录进行排序

代码如下:

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

3.统计出每一个各户的所有订单并按每一个客户下的订单的金额 升序排序,同时给每一个客户的订单进行编号。这样就知道每个客户下几单了。

如图:

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/fzyAzY5ibPU72dL1fCFGmc7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5tflYehAzUk8y4gOPrIcPM8uWMEEKI/Odm9+AXF0cJfJyh7m3W8nMNcHqQCQoPOK0PNM5M1sbrAEBELiPsgJkZYJHrYV6Ry4iZYWYqi1xGHTEDamqexMzki1xGvTCTYV72iJnJFLmMemGGN96obBWBWZipGwSLRUjMDIsVJ2aGxYoTM8NixYmZYbHi9H/d6P50hiqisAAAAABJRU5ErkJggg==" alt="" />

代码如下:

select ROW_NUMBER() over(partition by customerID  order by totalPrice) as rows,customerID,totalPrice, DID from OP_Order
1 select ROW_NUMBER() over(partition by customerID  order by totalPrice) as rows,customerID,totalPrice, DID from OP_Order

4.统计每一个客户最近下的订单是第几次下的订单。

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAKMAAABMCAIAAACOHJCAAAAFzElEQVR4nO2dPZLyPAzHdStmoKHkDhT7cAcqGhgOQZVnr0DJMzTbUmbeoecoeQvyodiS7Dh2YrP+T2Ynm8iOkp9lO4k2C6es3yFYZ/0OQRVM6/U6XOVBla7ngqCqqmf58L6cTqf2eoWoP9ySrufySWXSn+O5fFIRkQYAYYuyVzZWtrxX8E+/nse/zEAaTCJpkXiM7HFxhbqD5/Mvt8Nqebi/V9AV2xVRkpZpKbuMTQHz082MG8OSbsGEIN3VfD0vzbBjJ63bcPZCfJMBLRw3NdJWB4qCNBde9jGt9PB6y8ABPYL0ZYc7THx9m/X7cfE2WB0v5yUAAKj97fa7tt/ulj3L1fH67Fkuzjdsuf3mSJeX3bvayElzl94mpuVwV2ogm4W157iTvOxgcS500s0Vvx1WGAwqez8u4OvyvB1WLciO6OFeXs/LxfmG6mwNSj6mcan4SZMU5ZgWqAvbHUnrPSQR01c1OnUwLT9lV8cVi2xPicf0UBuyJze2FZuDjiBd7/r+4jtbM+k+M/IoiY7THEVBelmOos0QYOe51nvfUCQVW+j1tNfzEocj1XuTpPFR1CHgc+feRhsjXaXd4L3Dx+mHOiND8y9YLrQZ2bWxb0futyUJGMNrj4LHb400avfJ309bkuZ6hafWn+u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Nps5JH6pSMw3whJJ6//qxT/p5GL6LVfS+ufIml6YzyeRvlQk5hv1JOachBynO88jjOhwOSdtR6x9c8iK9IBSqmbKGGzPPErM1aQZgzUhK9IDSqmaJwu4ihtzFVcWsFjPmHF6osz+GEfnTn5Ji5n9TY/MzqLFLxWNnHu399NuAW0kbZ8TM5e8x7T+hSFhC38/bV9KVX5GRitdzzll0rTS9ZxTJk0rXc855ewiWul6zul/uVGBOlKOu0MAAAAASUVORK5CYII=" alt="" />

代码如下:

with tabs as
(
    select ROW_NUMBER() over(partition by customerID  order by totalPrice) as rows,customerID,totalPrice, DID from OP_Order
 )
 
 select MAX(rows) as '下单次数',customerID from tabs group by customerID
1 with tabs as
2 (
3 select ROW_NUMBER() over(partition by customerID  order by totalPrice) as rows,customerID,totalPrice, DID from OP_Order
4 )

6 select MAX(rows) as '下单次数',customerID from tabs group by customerID

5.统计每一个客户所有的订单中购买的金额最小,而且并统计改订单中,客户是第几次购买的。

如图:

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAQAAAABUCAIAAADieH5kAAAJHklEQVR4nO1dvW7bMBDmGxXoIsReMnrp0NkFAk9dMnpygdow0DcI3EXw3sloFgNevAadMmT3o6iDZJEU/8Uj5ZPvA1G4Cnm8I+/jURZPZh/vbyjKZrN5fHwcXI3R6EmlLmxwDTxL7VgVgQAKNASgQiVFYRsC4Y7BNpvNI4Fwr2gIkG6PlVT44N1lADqLMigM2AUnQIoNlsiuDPu5zN2RRYMoDLtkEwFuuqCziAgw5Pyhc5fxWUQEGHL+btddjqvpZHWqPzCORZnRor46wEyBqdNwrYgA6btrZwWqiNPMJR+2E4f/6S2yqOfzJ7FOp3604TYCaA0PHxkiQPruMhHA3VFyArzvFmy+zzAFJsPDRwY/AY6r6WS+mDDG5nse+Ob797ePct584BOzW7Bie3w7rYu63nR9CB79991CjK26JVCQv9tOGGOMdUPzfC8pz2s2KvGaxfaomKmdZpf/6Sw6mNWT/rR/kncUlgggGNWI7YyYal0kAV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Aj21Aj2adVtEXSAbf2CJJpeHXN/VSyI24KYuQHw2cLdNhOiu3xfiPAFVef0J6m1580Fv4X0KqLAIuEXrxzT/R6xMzKKCJAW46raaoIUAMJAdrFVTjWrqZ3GH8iKaBVjEVispdeW0tzf5XsGA8BTuuC7gGqTpLhufvjil4ECGgVZ1Hj8UIgkPu1NCcCmLZAsUnxVh0QEKCqKq0D1V7iRYCAVl302gJpRBp6IQKkfy2KVQcsBOjxWhSrnAT3AHYPdhKA7gHkzQ/ci7GsOtw0Aaxph9fthXHd1cpxtoqwSLfRUrduxubJvwVSejReiZEfAK/nAP2W/3EQoPJKO/R5DuDfKsoiS96jmiTp07wHRkKA+EJPgkGAzqLUChMBbEDnLk6gs4gIQASABDqLsBIANBeyAREgHugsQkmAiJxHB4gAkUBnEUoCUKGCpUB5PycAoEQCARGIAIS7BhGAcNf4D0kOHyVhPJNhAAAAAElFTkSuQmCC" alt="" />

上图:rows表示客户是第几次购买。

思路:利用临时表来执行这一操作

1.先按客户进行分组,然后按客户的下单的时间进行排序,并进行编号。

2.然后利用子查询查找出每一个客户购买时的最小价格。

3.根据查找出每一个客户的最小价格来查找相应的记录。

代码如下:

 with tabs as
 (
   select ROW_NUMBER() over(partition by customerID  order by insDT) as rows,customerID,totalPrice, DID from OP_Order
 )
 select * from tabs
 where totalPrice in 
    `(
            select MIN(totalPrice)from tabs group by customerID
    )
sql ROW_NUMBER() 排序函数 (转)
1 with tabs as
2 (
3 select ROW_NUMBER() over(partition by customerID  order by insDT) as rows,customerID,totalPrice, DID from OP_Order
4 )
5 select * from tabs
6  where totalPrice in 
7            (
8            select MIN(totalPrice)from tabs group by customerID
9            )
sql ROW_NUMBER() 排序函数 (转)

5.筛选出客户第一次下的订单。

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAx8AAABNCAIAAABIaE6TAAAYjElEQVR4nO1du27ksM72Ww2QFJty3iFFTpo8gas0WaTbF0jlkz5VyvxI42qBLYOD9IM8if/CtqwLSVEee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hj0np1aSiLO6csM6fqEOorhPMrnAbJjxewKnlg68RTY+i7Eqfu9o9u/o5Nb+O9cP437/NEfpxXFl0gaQRai7QLDB8orD0c1eMr3/crdstzPFf8wrHBUOcJSsgYgNmV59Ph+rupZzYVd+PE0WsyN8MYrKRMQn0xMfxtd5HK6f1f/03ihPmAH15slsoLHbFsSEnjphS6wyNoPPH8EQTrc4WiYayqzzBoQtd91ZbP4gbDr/6P7UPVf0GkwPJdIGkEWou0CwQQqeQcMAC7QkJXg9oJ9y6icJAnwsXRuhjeI6JuePZsSvzvqt5gavs2FVnPXFVHZvW+Vmd4byuKJHxaGe9SqS1nvuK5rh8EM4xdAsj74Wxqy7RhvPGgtYvSSNinlgn+3Z/keozoOwqT/Dows+p+TW9bqD7a/0sYszsX0kwbWRvQM7WJGAhGqHmAs0CAHQKnI0RyXHbjHg9uJ1wyyUK97D8F7VPphe2c4bPtMVC50s7Vn1Xu0Lf1Z4McRqJE9hAruQcoHRBE0ojthcjt1T2GpELZVcKZVfJEKeROIEN5ErOgdKFdBqxvRi5pbLXiFwou1Iou0qGOI3ECWwgV3IOlC6k04jtxcgtlb1G5GJiV79XgLIrEVB2lQpxGokT2ECu5BwoXUinEduLkVsqe43IxcCublaDsqv8oewqFeI0EiewgVzJOVC6kE4jthcjt1T2GpGLgV1p0qRJkyZNmjRpWiRVNzc3WzM8hUKhUCgUinKg7EqhUCgUCoViSfw//wWZjpO+gCkAAAAASUVORK5CYII=" alt="" />

思路。利用rows=1来查询客户第一次下的订单记录。

代码如下:

 with tabs as
 (
 select ROW_NUMBER() over(partition by customerID  order by insDT) as rows,* from OP_Order
 )
 select * from tabs where rows = 1
   
 select * from OP_Order
sql ROW_NUMBER() 排序函数 (转)
1 with tabs as
2 (
3 select ROW_NUMBER() over(partition by customerID  order by insDT) as rows,* from OP_Order
4 )
5 select * from tabs where rows = 1
6   
7 select * from OP_Order
sql ROW_NUMBER() 排序函数 (转)

6.rows_number()可用于分页

思路:先把所有的产品筛选出来,然后对这些产品进行编号。然后在where子句中进行过滤。

7.注意:在使用over等开窗函数时,over里头的分组及排序的执行晚于“where,group by,order by”的执行。

如下代码:

1  select 
2  ROW_NUMBER() over(partition by customerID  order by insDT) as rows,
3  customerID,totalPrice, DID
4   from OP_Order where insDT>'2011-07-22'
1  select 
2  ROW_NUMBER() over(partition by customerID  order by insDT) as rows,
3  customerID,totalPrice, DID
4   from OP_Order where insDT>'2011-07-22'

以上代码是先执行where子句,执行完后,再给每一条记录进行编号。

上一篇:Android 开发之Matrix图片处理类的使用


下一篇:shell实现go环境的部署搭建