因狗血的報表展現需求 需要根據每天的新增使用者數量 選取前5個渠道.然後把該渠道當月每一天的新增使用者資料繪製在圖表上
x軸是 日期 y軸是新增使用者數量 畫5條線. 這5條線是動態的,每天不同的線條.
資料經過後台統計後形成表 : statedate,channle_id,channle_name,new_user_num.
一般行轉列方法:
select statedate,
sum(case when channle_name='超市' then new_user_num end )as '超市',
sum(case when channle_name='專賣店' then new_user_num end )as '專賣店' ,
sum(case when channle_name='合作店' then new_user_num end )as '合作店' ,
sum(case when channle_name='網路** then new_user_num end) as '網路**',
sum(case when channle_name='直銷' then new_user_num end )as '直銷'
from table_name
group by staedate;
可channle_name='超市' 值是變動的呀!
具體這樣做:
with resu as
(select statedate,substr(to_char(statedate),7,2) as statedate_v, a.columnid,a.new_user_num,t.name,rownum as row_id
from oss_content_run_subs_colum_new a
left join ngoss_pdl.vw_dim_ss_column_base_info t on a.columnid=t.columnid
where a.statedate in
(select d.date_id from ngoss_edw.dim_date d where to_number(substr(to_char(d.date_id), 1, 6)) = to_number(to_char(sysdate-1,'yyyymm')))
and a.columnid in
(select columnid
from
(select columnid
from oss_colum_new
where statedate = 20120508
order by new_user_num desc
)where rownum<=5
)and a.statedate >=to_number(to_char(trunc(sysdate-1,'mm'),'yyyymmdd')) and a.statedate <= to_number(to_char(sysdate,'yyyymmdd'))
order by new_user_num desc
)select statedate_v,
sum( case when s.name=(select name from resu where row_id=5) then new_user_num end) as top1
sum ( case when s.name=(select name from resu where row_id=4) then new_user_num end) as top2,
sum ( case when s.name=(select name from resu where row_id=3) then new_user_num end) as top3,
sum ( case when s.name=(select name from resu where row_id=2) then new_user_num end) as top4,
sum ( case when s.name=(select name from resu where row_id=1) then new_user_num end) as top5
from resu s
group by statedate_v
---------------------------------- 改進----------------------
with resu as
(select statedate,substr(to_char(statedate),7,2) as statedate_v, a.columnid,a.new_user_num,t.name, rw as rankid
from oss_colum_new a
left join ngoss_pdl.vw_dim_ss_column_base_info t on a.columnid=t.columnid
inner join
(select columnid,rownum as rw
from
(select columnid
from oss_colum_new
where statedate = to_number(to_char(sysdate-1,'yyyymmdd'))
order by new_user_num desc
)where rownum<=5
)c on a.columnid=c.columnid
where a.statedate in
(select d.date_id from ngoss_edw.dim_date d
where to_number(substr(to_char(d.date_id), 1, 6)) = to_number(to_char(sysdate-1,'yyyymm'))
)
and a.statedate >=to_number(to_char(trunc(sysdate-1,'mm'),'yyyymmdd')) and a.statedate <= to_number(to_char(sysdate-1,'yyyymmdd'))
order by columnid,statedate
)select statedate_v,
sum ( case when s.name=(select name from resu r where rankid=1 and rownum=1) then new_user_num end) as top1,
sum ( case when s.name=(select name from resu r where rankid=2 and rownum=1) then new_user_num end) as top2,
sum ( case when s.name=(select name from resu r where rankid=3 and rownum=1) then new_user_num end) as top3,
sum ( case when s.name=(select name from resu r where rankid=4 and rownum=1) then new_user_num end) as top4,
sum ( case when s.name=(select name from resu r where rankid=5 and rownum=1) then new_user_num end) as top5
from resu s
group by statedate_v
order by statedate_v
解決返回多行的問題:select name from resu r where rankid=1 and rownum=1
動態行轉列
drop table test create table test name varchar 12 scores int insert into test select 周杰倫 230 union select 周星馳 100 union select 成龍 150 union select 李連杰...
mysql 動態行轉列 MySQL行轉列
比如乙個單子,多個收據單用逗號隔開,怎麼把這乙個單子所有收據單獨展示出來,行轉成列呢?方法一 這裡需要用到迴圈,首先建立乙個1 10的序列 select rownum rownum 1 as seq from select rownum 0 r,bills limit 0,10 其次依次運用 sub...
動態的行轉列
declare sqlone varchar 800 sqltwo varchar 800 select sqlone isnull sqlone tgcname from dbo.ttask group by tgcname select sqlone sqlone select sqltwo s...