查询1、2、3、4、5前五大数据的id,并成横向显示
如下图:
现要查出每个Wafer_id的Bin_Qty最大的五个Bin_Id和Bin_QTY,且成横向显示。
想了好久了,还是不会,请大虾指教哈!
目前我只能查出一条Wafer_id的所需的前五位的值,如下图:
请问怎么样能够查出所有的Wafer_id的前5大Bin_id、Bin_qty啊?
老衲在此谢过了~
[最优解释]
WITH TEST AS (
SELECT '1' AS Wafer_id ,20 AS Bin_Id,1 as Bin_QTY FROM DUAL
UNION ALL
SELECT '1' AS Wafer_id ,15 AS Bin_Id,1 as Bin_QTY FROM DUAL
UNION ALL
SELECT '1' AS Wafer_id ,14 AS Bin_Id,1 as Bin_QTY FROM DUAL
UNION ALL
SELECT '1' AS Wafer_id ,13 AS Bin_Id,14 as Bin_QTY FROM DUAL
UNION ALL
SELECT '1' AS Wafer_id ,5 AS Bin_Id,2 as Bin_QTY FROM DUAL
UNION ALL
SELECT '1' AS Wafer_id ,10 AS Bin_Id,37 as Bin_QTY FROM DUAL
UNION ALL
SELECT '1' AS Wafer_id ,6 AS Bin_Id,13 as Bin_QTY FROM DUAL
UNION ALL
SELECT '1' AS Wafer_id ,0 AS Bin_Id,1067 as Bin_QTY FROM DUAL
UNION ALL
SELECT '1' AS Wafer_id ,12 AS Bin_Id,7 as Bin_QTY FROM DUAL
UNION ALL
SELECT '2' AS Wafer_id ,12 AS Bin_Id,4 as Bin_QTY FROM DUAL
)
select max(decode(rn,1,Bin_Id,'')) as ONE,
max(decode(rn,1,Bin_QTY,'')) as ONE_num,
max(decode(rn,2,Bin_Id,'')) as TWO,
max(decode(rn,2,Bin_QTY,'')) as TWO_num,
max(decode(rn,3,Bin_Id,'')) as THREE,
max(decode(rn,3,Bin_QTY,'')) as THREE_num,
max(decode(rn,4,Bin_Id,'')) as FOUR,
max(decode(rn,4,Bin_QTY,'')) as FOUR_num,
max(decode(rn,5,Bin_Id,'')) as FIVE,
max(decode(rn,5,Bin_QTY,'')) as FIVE_num
from (
select row_number()over(partition by Wafer_id order by Bin_QTY desc) as rn,Wafer_id,Bin_Id,Bin_QTY from test) where rn <6 GROUP BY Wafer_id
[其他解释]