參考了以下blog
http://xsb.itpub.net/post/419/33028
http://www.cnblogs.com/sunsonbaby/archive/2004/09/29/47876.aspx
這兩篇博客介紹得最詳細(xì)!!現(xiàn)學(xué)現(xiàn)用,感覺挺好
select E.地區(qū),E.彩種,E.時段, E.銷售票數(shù), E.銷售金額,E.銷售增長率,E.本次銷售排名,E.本次增長率排名,E.上次銷量,E.上次銷量排名,E.上上次銷量,E.上次增長率排名,(E.本次增長率排名-E.上次銷量排名)銷售排名走勢,(E.本次增長率排名-E.上次增長率排名)增長率走勢 from
(
Select A.地區(qū),
A.彩種,
A.時段,
A.銷售票數(shù),
A.銷售金額,
decode(A.上次銷量,0,'0%',round( ((A.銷售金額 - A.上次銷量) / A.上次銷量) * 100, 2) || '%' )銷售增長率,
rank() over(order by A.銷售金額 desc) 本次銷售排名,
rank() over(order by decode(A.上次銷量,0,0,round(((A.銷售金額 - A.上次銷量) / A.上次銷量) * 100, 2)) desc) 本次增長率排名,
A.上次銷量, --A.上次銷量,
rank() over(order by A.上次銷量 desc) 上次銷量排名,
A.上上次銷量,--上上次銷量
rank() over(order by decode(A.上上次銷量,0,0,round(((A.銷售金額 - A.上次銷量) / A.上次銷量) * 100, 2)) desc )上次增長率排名
from (select C.AREA_NAME 地區(qū),
D.Playtype_Name 彩種,
to_char(open_result_time, 'yyyy-mm') 時段,
sum(A.sell_count) 銷售票數(shù),
sum(A.sell_amount) 銷售金額,
lag(sum(A.Sell_Amount), 1, 0) over(partition by AREA_NAME,Playtype_Name order by to_char(open_result_time, 'yyyy-mm')) 上次銷量,
lag(sum(A.Sell_Amount), 2, 0) over(partition by AREA_NAME ,Playtype_Name order by to_char(open_result_time, 'yyyy-mm')) 上上次銷量
from terminal_sell A, SALES_OUTLETS_INFO B, area C, playtype D
where D.PLAYTYPE_ID(+) = A.Lottery_Type
and C.AREA_ID(+) = B.Area_Id
and B.Sell_Id(+) = A.Terminal_Sell_Id
group by AREA_NAME,Playtype_Name ,to_char(open_result_time, 'yyyy-mm')
) A
) E
(
Select A.地區(qū),
A.彩種,
A.時段,
A.銷售票數(shù),
A.銷售金額,
decode(A.上次銷量,0,'0%',round( ((A.銷售金額 - A.上次銷量) / A.上次銷量) * 100, 2) || '%' )銷售增長率,
rank() over(order by A.銷售金額 desc) 本次銷售排名,
rank() over(order by decode(A.上次銷量,0,0,round(((A.銷售金額 - A.上次銷量) / A.上次銷量) * 100, 2)) desc) 本次增長率排名,
A.上次銷量, --A.上次銷量,
rank() over(order by A.上次銷量 desc) 上次銷量排名,
A.上上次銷量,--上上次銷量
rank() over(order by decode(A.上上次銷量,0,0,round(((A.銷售金額 - A.上次銷量) / A.上次銷量) * 100, 2)) desc )上次增長率排名
from (select C.AREA_NAME 地區(qū),
D.Playtype_Name 彩種,
to_char(open_result_time, 'yyyy-mm') 時段,
sum(A.sell_count) 銷售票數(shù),
sum(A.sell_amount) 銷售金額,
lag(sum(A.Sell_Amount), 1, 0) over(partition by AREA_NAME,Playtype_Name order by to_char(open_result_time, 'yyyy-mm')) 上次銷量,
lag(sum(A.Sell_Amount), 2, 0) over(partition by AREA_NAME ,Playtype_Name order by to_char(open_result_time, 'yyyy-mm')) 上上次銷量
from terminal_sell A, SALES_OUTLETS_INFO B, area C, playtype D
where D.PLAYTYPE_ID(+) = A.Lottery_Type
and C.AREA_ID(+) = B.Area_Id
and B.Sell_Id(+) = A.Terminal_Sell_Id
group by AREA_NAME,Playtype_Name ,to_char(open_result_time, 'yyyy-mm')
) A
) E
imp omas/omas@omas fromuser=omas touser=omas IGNORE=Y file=c:\\anhui_sellData080520.dmp