求oracle sql 语句
需求
M79B table
M79BLBLCOD M79BYR M79BPOSSNCOD M79BLINNUM M79BDISRTE M79BCMSRTE
XGS 2013 SS 1 2 3
XGS 2013 SS 2 4 5
AFA 2012 SS 1 0 0.65
AFA 2012 SS 2 0.2 0.6
AFA 2012 SS 3 0.3 0.55
AFA 2012 SS 4 0.5 0.5
AFA 2011 SS 4 0.5 0.4
AFA 2011 SS 1 0.1 0.45
AFA 2011 SS 2 0.2 0.4
AFA 2011 SS 3 0.3 0.45
根據用戶選擇的 Year 年份、Season 季度、Supplier 供應商,擷取ZTM79, ZTM79A, ZTM79B
首欄的內容為M79BDISRTE(以此欄排序)
之後的欄位按Year 年份、Season 季度而定,內容為M79BCMSRTE,
1.基本上一開始要找到所有需要的季度的discount rate,以discount rate排序在最左欄顯示
2.然後在右邊的欄位把所有需要的季度填上M79BCMSRTE
3. 如果個別季度沒有某個discount rate,該格為空白
想要得要下面效果
2011& 2012 M79BDISRTE 2011 SS M79BCMSRTE 2012 SS M79BCMSRTE
0 0.65
0.1 0.45
0.2 0.4 0.6
0.3 0.45 0.55
0.5 0.4 0.5
比如SELECT M79BLBLCOD,M79BYR,M79BPOSSNCOD,M79BDISRTE,M79BCMSRTE
FROM ZTM79B
WHERE M79BLBLCOD='AFA'
AND M79BYR IN ('2011', '2012')
AND M79BPOSSNCOD IN ('SS')
------解决方案--------------------select NVL(T1.M79BDISRTE, T2.M79BDISRTE) AS M79BDISRTE,
T1.M79BCMSRTE,
T2.M79BCMSRTE
from (select M79BDISRTE, M79BCMSRTE
from test
where M79BYR = '2011'
AND M79BLBLCOD = 'AFA'
and M79BPOSSNCOD = 'SS') t1
full join (select M79BDISRTE, M79BCMSRTE
from test
where M79BYR = '2012'
AND M79BLBLCOD = 'AFA'
and M79BPOSSNCOD = 'SS') t2 on t1.M79BDISRTE =
t2.M79BDISRTE
ORDER BY NVL(T1.M79BDISRTE, T2.M79BDISRTE)
=======================================================
1 0 0.65
2 0.1 0.45
3 0.2 0.4 0.6
4 0.3 0.45 0.55
5 0.5 0.4 0.5