求助 。。。oracle 同期 同比 环比
有现成 sql server 的存储过程来实现,但是贴到oracle里面完全出错了,
oracle 里面 如何写 ?
这是 sql的:
DECLARE @var_sql1 VARCHAR(max),@var_sql2 VARCHAR(max),@var_sql3 VARCHAR(max),@var_sql4 VARCHAR(max),@var_group VARCHAR(200) ,
@var_parm1 VARCHAR(max),@var_parm2 VARCHAR(max),
@var_parm3 VARCHAR(max),@var_group1 VARCHAR(200),@date_lyearbegin DATETIME,
@date_lyearend DATETIME,@date_lmonthbegin DATETIME,@date_lmonthend DATETIME
--参数解析
--同期的时间
SET @date_lyearbegin = DATEADD(year,-1,CAST (SUBSTRING(@var_parm,charindex('a.dsate_out,120) >= ',@var_parm)+20,10) AS DATETIME))
SET @date_lyearend = DATEADD(year,-1,CAST (SUBSTRING(@var_parm,charindex('a.dsate_out,120) <= ',@var_parm)+20,10) AS DATETIME))
--环比的时间
SET @date_lmonthbegin = DATEADD(month,-1,CAST (SUBSTRING(@var_parm,charindex('a.dsate_out,120) >= ',@var_parm)+20,10) AS DATETIME))
SET @date_lmonthend = DATEADD(month,-1,CAST (SUBSTRING(@var_parm,charindex('a.dsate_out,120) <= ',@var_parm)+20,10) AS DATETIME))
--同期的参数
SET @var_parm2 =SUBSTRING(@var_parm,0,charindex('a.dsate_out,120) >= ',@var_parm)+20) +convert(varchar(10),@date_lyearbegin,120)+''' '
+ SUBSTRING(@var_parm,charindex('a.dsate_out,120) >= ',@var_parm)+32,LEN(' and convert(varchar(10),a.dsate_out,120) <= ')+1) +convert(varchar(10),@date_lyearend,120) + ''' ' + SUBSTRING(@var_parm,charindex('a.dsate_out,120) <= ',@var_parm)+32,999)
--环比的参数
SET @var_parm3 =SUBSTRING(@var_parm,0,charindex('a.dsate_out,120) >= ',@var_parm)+20) +convert(varchar(10),@date_lmonthbegin,120) +''' '
+ SUBSTRING(@var_parm,charindex('a.dsate_out,120) >= ',@var_parm)+32,LEN(' and convert(varchar(10),a.dsate_out,120) <= ')+1) +convert(varchar(10),@date_lmonthend,120) + ''' ' + SUBSTRING(@var_parm,charindex('a.dsate_out,120) <= ',@var_parm)+32,999)
------解决方案--------------------通过当前lag() over()函数 来实现当前值与一值的比较
同期lag() over(order by item)
环比lag() over(partition by 分组字段 order by item)
------解决方案--------------------写一个简单的例子 大致就这样了
with t1 as
(
select '001' item,2 qty,date'2012-01-01' dt from dual union all
select '002' item,2 qty,date'2012-02-01' dt from dual union all
select '003' item,3 qty,date'2012-03-01' dt from dual union all
select '001' item,5 qty,date'2013-01-01' dt from dual union all
select '002' item,4 qty,date'2013-02-01' dt from dual union all
select '003' item,6 qty,date'2013-03-01' dt from dual union all
select '001' item,1 qty,date'2014-01-01' dt from dual
)
select item,qty,
lag(qty) over(order by dt) s_qty,
&