之前公司的前輩寫了一段這樣的語法
想要先依p.bnd_ctrl排序(先Y再N)
然後抓第一筆的資料 結果卻不如所料
SELECT rownum,a.Prt_No, a.b_Beg_Qty, a.b_Ttl_In,p.bnd_ctrl
FROM Bnd_Phy_Analysis a, bnd_prtmst p
WHERE a.prt_no = p.prt_no
and a.Bg_Group_Prt = 'C2PAM HEDCB-05'
and a.Phy_No = '10303'
and Rownum = 1
ORDER BY p.bnd_ctrl desc, (a.b_Beg_Qty + a.b_Ttl_In) desc;
Oracle 的 rownum 是一個 pseudocolumn,意思是不存在的 column,是經由計算得來的
RowNum 是先經過編號後,才再 order by name 的
如果要先 order by name 才編號,就必須下這樣的sql script
SELECT t.* FROM (
SELECT rownum,a.Prt_No, a.b_Beg_Qty, a.b_Ttl_In,p.bnd_ctrl
FROM Bnd_Phy_Analysis a, bnd_prtmst p
WHERE a.prt_no = p.prt_no
and a.Bg_Group_Prt = 'C2PAM HEDCB-05'
and a.Phy_No = '10303'
ORDER BY p.bnd_ctrl desc, (a.b_Beg_Qty + a.b_Ttl_In) desc) t
WHERE Rownum = 1
Oracle官網的解說 http://www.oracle.com/technology/oramag/oracle/06-sep/o56asktom.html
沒有留言:
張貼留言