Oracle数据库row_number() over统计前15名企业, wm_concat(case when then)行转列
1.
ROW_NUMBER() OVER函数的基本用法
SELECT product_type 产品类别, prickle 计量单位, production_name 企业名称, row_number() over(partition by product_type, prickle order by sum(pur.purchase_num) desc) 名次 from t_purchase_info pur group by production_name, product_type, prickle
参考:http://www.cnblogs.com/fxgachiever/archive/2010/09/15/1826792.html
2.列转行 wm_concat(case when then)
select product_type 产品类别, prickle 计量单位, wm_concat(case when r = 1 then production_name end) 第一名, wm_concat(case when r = 1 then value1 end) 申报量, wm_concat(case when r = 2 then production_name end) 第二名, wm_concat(case when r = 2 then value1 end) 申报量, wm_concat(case when r = 3 then production_name end) 第三名, wm_concat(case when r = 3 then value1 end) 申报量, wm_concat(case when r = 4 then production_name end) 第四名, wm_concat(case when r = 4 then value1 end) 申报量, wm_concat(case when r = 5 then production_name end) 第五名, wm_concat(case when r = 5 then value1 end) 申报量, wm_concat(case when r = 6 then production_name end) 第六名, wm_concat(case when r = 6 then value1 end) 申报量, wm_concat(case when r = 7 then production_name end) 第七名, wm_concat(case when r = 7 then value1 end) 申报量, wm_concat(case when r = 8 then production_name end) 第八名, wm_concat(case when r = 8 then value1 end) 申报量, wm_concat(case when r = 9 then production_name end) 第九名, wm_concat(case when r = 9 then value1 end) 申报量, wm_concat(case when r = 10 then production_name end) 第十名, wm_concat(case when r = 10 then value1 end) 申报量, wm_concat(case when r = 11 then production_name end) 第十一名, wm_concat(case when r = 11 then value1 end) 申报量, wm_concat(case when r = 12 then production_name end) 第十二名, wm_concat(case when r = 12 then value1 end) 申报量, wm_concat(case when r = 13 then production_name end) 第十三名, wm_concat(case when r = 13 then value1 end) 申报量, wm_concat(case when r = 14 then production_name end) 第十四名, wm_concat(case when r = 14 then value1 end) 申报量, wm_concat(case when r = 15 then production_name end) 第十五名, wm_concat(case when r = 15 then value1 end) 申报量 from (SELECT r, production_name, product_type, prickle, value1 FROM (SELECT row_number() over(partition by product_type, prickle order by sum(pur.purchase_num) desc) r, pur.production_name, pur.product_type, prickle, to_char(round(sum(pur.purchase_num), 2), '9999999999999999999.99') value1 FROM t_purchase_info pur, t_sgproject_info pro WHERE 1 = 1 AND pro.id = pur.project_id AND (pro.gclb = '房屋建筑工程' OR pro.gclb IS NULL) AND pro.status != 9 AND product_regdate >= to_date('2014-01-01', 'yyyy-mm-dd hh24:mi:ss') AND product_regdate <= to_date('2014-12-31 23:59:59', 'yyyy-mm-dd hh24:mi:ss') AND sgproject_type = 1 GROUP BY production_name, product_type, prickle) WHERE r <= 15 ORDER BY product_type, prickle, r) group by product_type, prickle
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。