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 oraclesql

解决方案 »

  1.   

    select * from (select year,lead(amount)  over (partition by year order by amount ) from user_name )这样试下。
      

  2.   

    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.411G的行转列可以用这个方式
    如果是10g还是老实的用decode吧
    为啥要用分析函数,谁告诉你用了就能提高效率?
      

  3.   

     不行啊   ,我用的 是 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 了
    所以 我就 想的 用 分析函数 了
    子查询 效率  确实   不怎么 效率 高 ,大数据量的  话 这个 太影响 性能 了。
      

  4.   

    让你优化数据还不如让你优化你的表呢 话说你们公司的这个表建立的也太蛋疼了吧?
    如果嫌弃数据速度太慢你可以在页面加载的时候启动一个线程把你的数据插入到你自定义的datatable中这个datatable就可以用刚刚2楼的说法去做就好了
      

  5.   

    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解决问题
      

  6.   

    非要用sql其实不是很好的方法,
    可以先用 group by 月份,
    然后程序里就好做了!
    这种问题我们都碰到 过!
      

  7.   

    http://wenku.baidu.com/view/6694a7225901020207409c5d.htmlhttp://wenku.baidu.com/view/6694a7225901020207409c5d.html自己研究语法去
      

  8.   

    11G的行转列可以用这个方式
    如果是10g还是老实的用decode吧
    为啥要用分析函数,谁告诉你用了就能提高效率?