日期:2014-05-17  浏览次数:20811 次

笨猫(*),笨猫(*),笨猫(*),笨猫(*)进,关于上次的sql语句!急..在线等!
select   *   from
(select   dy_dcsw,lj_dcsw,dy_zr,lj_zr,dy_zr_qz,lj_zr_qz,dy_zc,
lj_zc,dy_sdl,lj_sdl,dy_sdl_qz,lj_sdl_qz,dy_xsdl,lj_xsdl,
dy_xsl,lj_xsl,dy_xsl_ys,lj_xsl_ys,tab_year,   lpad(tab_month,   2,   '0 ')   tab_month,xm,  
--要求显示上个月的字段
lag(lj_dcsw   ,   1,   0)   over(partition   by   xm   order   by   tab_year||lpad(tab_month,   2,   '0 '))   as   lj_dcsw1   ,   --加上partition   by
lag(lj_zr   ,   1,   0)   over(partition   by   xm   order   by   tab_year||lpad(tab_month,   2,   '0 '))   as   lj_zr1   ,
lag(lj_zr_qz   ,   1,   0)   over(partition   by   xm   order   by   tab_year||lpad(tab_month,   2,   '0 '))   as   lj_zr_qz1   ,
lag(lj_zc   ,   1,   0)   over(partition   by   xm   order   by   tab_year||lpad(tab_month,   2,   '0 '))   as   lj_zc1   ,
lag(lj_sdl   ,   1,   0)   over(partition   by   xm   order   by   tab_year||lpad(tab_month,   2,   '0 '))   as   lj_sdl1   ,
lag(lj_sdl_qz,   1,   0)   over(partition   by   xm   order   by   tab_year||lpad(tab_month,   2,   '0 '))   as   lj_sdl_qz1,
lag(lj_xsdl   ,   1,   0)   over(partition   by   xm   order   by   tab_year||lpad(tab_month,   2,   '0 '))   as   lj_xsdl1   ,
lag(lj_xsl   ,   1,   0)   over(partition   by   xm   order   by   tab_year||lpad(tab_month,   2,   '0 '))   as   lj_xsl1   ,
lag(lj_xsl_ys,   1,   0)   over(partition   by   xm   order   by   tab_year||lpad(tab_month,   2,   '0 '))   as   lj_xsl_ys1,
lag(tab_year   ,   1,   0)   over(partition   by   xm   order   by   tab_year||lpad(tab_month,   2,   '0 '))   as   tab_year1   ,
lag(lpad(tab_month,   2,   '0 '),   1,   0)   over(partition   by   xm   order   by   tab_year||lpad(tab_month,   2,   '0 '))   as   tab_month1
--要求显示上上个月的字段
from   分压线损表
where   tab_year   ||   lpad(tab_month,   2,   '0 ')   > =   to_char(add_months(sysdate,-2), 'yyyymm ')
and   tab_year   ||   lpad(tab_month,   2,   '0 ')   <=   to_char(add_months(sysdate,-1), 'yyyymm ')   and   dw=?
)
where   tab_year   ||   lpad(tab_month,   2,   '0 ')   =   to_char(add_months(sysdate,-1), 'yyyymm ')   and   dw=?
union   all
--处理上月不存在纪录而上上月存在纪录的情况
select     null,   null,null,null,null,null,null,
null,null,null,null,null,null,null,
null,null,null,null,null,   null,xm,
--要求显示上个月的字段
lj_dcsw   as   lj_dcsw1,   lj_zr   as   lj_zr1   ,lj_zr_qz   as   lj_zr_qz1   ,lj_zc   as   lj_zc1   ,lj_sdl   as   lj_sdl1   ,
lj_sdl_qz   as   lj_sdl_qz1,   lj_xsdl   as   lj_xsdl1   ,   lj_xsl   as   lj_xsl1   ,lj_xsl_ys   as   lj_xsl_ys1,   tab_year,   lpad(