oracle sql 问题

fjmwish 2013-02-05 03:12:51
1.这是 user_name 表
year  month amount
1991   1     1.1
1991   2     1.2
1991   3     1.3
1991   4     1.4
1992   1     2.1
1992   2     2.2
1992   3     2.3
1992   4     2.4


写一个sql (要求 用 lead over 这些 分析 函数 为了 提高 效率)

实现 下面的 效果

year m1  m2  m3  m4
1991 1.1 1.2 1.3 1.4
1992 2.1 2.2 2.3 2.4

...全文
364 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
pangzengji 2013-02-13
  • 打赏
  • 举报
回复
11G的行转列可以用这个方式 如果是10g还是老实的用decode吧 为啥要用分析函数,谁告诉你用了就能提高效率?
東吳悍將 2013-02-08
  • 打赏
  • 举报
回复
http://wenku.baidu.com/view/6694a7225901020207409c5d.htmlhttp://wenku.baidu.com/view/6694a7225901020207409c5d.html自己研究语法去
twain09 2013-02-08
  • 打赏
  • 举报
回复
楼上的说的非常好,学习了,哈哈
fjmwish 2013-02-06
  • 打赏
  • 举报
回复
各路 好汉 能不能 给我 用 分析 函数 写出 来啊,在此谢过 了。
blliy117 2013-02-06
  • 打赏
  • 举报
回复
非要用sql其实不是很好的方法, 可以先用 group by 月份, 然后程序里就好做了! 这种问题我们都碰到 过!
東吳悍將 2013-02-06
  • 打赏
  • 举报
回复
select E.*,F.acount AS m4 from (select C.*,D.acount AS m3 from (select A.*,B.acount AS m2 from (SELECT year,acount as m1 from tes where month='1') AS A JOIN (select * from tes WHERE month='2') AS B on A.YEAR=B.YEAR) AS C JOIN (select * from tes WHERE month='3')AS D ON C.year=D.year) AS E JOIN (select * from tes WHERE month='4')AS F ON E.year=F.year 一句sql解决问题
東吳悍將 2013-02-06
  • 打赏
  • 举报
回复
让你优化数据还不如让你优化你的表呢 话说你们公司的这个表建立的也太蛋疼了吧? 如果嫌弃数据速度太慢你可以在页面加载的时候启动一个线程把你的数据插入到你自定义的datatable中这个datatable就可以用刚刚2楼的说法去做就好了
fjmwish 2013-02-05
  • 打赏
  • 举报
回复
不行啊 ,我用的 是 10g 的 我都试过 了 我以前的sql 语句是 select distinct year, (select amount from user_name where amount=1 and user_name.year=b.name ) m1, (select amount from user_name where amount=2 and user_name.year=b.name ) m2, (select amount from user_name where amount=3 and user_name.year=b.name ) m3, (select amount from user_name where amount=4 and user_name.year=b.name ) m4, from user_name b 老板要我优化 这个 sql 了 所以 我就 想的 用 分析函数 了 子查询 效率 确实 不怎么 效率 高 ,大数据量的 话 这个 太影响 性能 了。
loveofmylife 2013-02-05
  • 打赏
  • 举报
回复
with temp as
(
select 1991 year,1 month,1.1 amount from dual 
union all
select 1991 year,2 month,1.2 amount from dual
union all
select 1991 year,3 month,1.3 amount from dual
union all
select 1991 year,4 month,1.4 amount from dual
union all
select 1992 year,1 month,2.1 amount from dual
union all
select 1992 year,2 month,2.2 amount from dual
union all
select 1992 year,3 month,2.3 amount from dual
union all
select 1992 year,4 month,2.4 amount from dual
) 
select * from temp
PIVOT (SUM(amount) FOR month IN  (1 as m1,2 as m2,3 as m3,4 as m4) )
YEAR M1 M2 M3 M4 ---------- ---------- ---------- ---------- ---------- 1991 1.1 1.2 1.3 1.4 1992 2.1 2.2 2.3 2.4 11G的行转列可以用这个方式 如果是10g还是老实的用decode吧 为啥要用分析函数,谁告诉你用了就能提高效率?
  • 打赏
  • 举报
回复
select * from (select year,lead(amount) over (partition by year order by amount ) from user_name )这样试下。

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧