生成数据仓库库存快照表的SQL语句
2006-07-24 01:04
381 查看
declare @begin_date as datetime select @begin_date = isNull((SELECT TOP 1 快照日期 FROM fact_t5 ORDER BY 快照日期 DESC), '11/13/2002')
declare @the_date as datetime select @the_date = dateadd(d,1,@begin_date)
declare @i as int select @i=0
while dateadd(d,@i,@the_date) < getdate()
Begin
insert into fact_t5
select * from
(
select
dateadd(d,@i,@the_date) as 快照日期
,a.仓库
,a.物料内码
,结存数量 = isnull((select 结存数量 from fact_t5 b where b.快照日期 = dateadd(d,-1,dateadd(d,@i,@the_date)) and a.物料内码=b.物料内码 and a.仓库=b.仓库),0)+isnull((select 入库数量 from fact_t4 c where c.单据日期 = dateadd(d,@i,@the_date) and a.物料内码=c.物料内码 and a.仓库=c.仓库),0)-isnull((select 出库数量 from fact_t4 d where d.单据日期 = dateadd(d,@i,@the_date) and a.物料内码=d.物料内码 and a.仓库=d.仓库 ),0)
,结存金额 = isnull((select 结存金额 from fact_t5 b where b.快照日期 = dateadd(d,-1,dateadd(d,@i,@the_date)) and a.物料内码=b.物料内码 and a.仓库=b.仓库),0)+isnull((select 入库金额 from fact_t4 c where c.单据日期 = dateadd(d,@i,@the_date) and a.物料内码=c.物料内码 and a.仓库=c.仓库),0)-isnull((select 出库金额 from fact_t4 d where d.单据日期 = dateadd(d,@i,@the_date) and a.物料内码=d.物料内码 and a.仓库=d.仓库 ),0)
from (
select distinct
仓库
,物料内码
from fact_t4 ) a
) s where s.结存数量 <> 0 and s.结存金额 <> 0
set @i=@i+1
End
declare @the_date as datetime select @the_date = dateadd(d,1,@begin_date)
declare @i as int select @i=0
while dateadd(d,@i,@the_date) < getdate()
Begin
insert into fact_t5
select * from
(
select
dateadd(d,@i,@the_date) as 快照日期
,a.仓库
,a.物料内码
,结存数量 = isnull((select 结存数量 from fact_t5 b where b.快照日期 = dateadd(d,-1,dateadd(d,@i,@the_date)) and a.物料内码=b.物料内码 and a.仓库=b.仓库),0)+isnull((select 入库数量 from fact_t4 c where c.单据日期 = dateadd(d,@i,@the_date) and a.物料内码=c.物料内码 and a.仓库=c.仓库),0)-isnull((select 出库数量 from fact_t4 d where d.单据日期 = dateadd(d,@i,@the_date) and a.物料内码=d.物料内码 and a.仓库=d.仓库 ),0)
,结存金额 = isnull((select 结存金额 from fact_t5 b where b.快照日期 = dateadd(d,-1,dateadd(d,@i,@the_date)) and a.物料内码=b.物料内码 and a.仓库=b.仓库),0)+isnull((select 入库金额 from fact_t4 c where c.单据日期 = dateadd(d,@i,@the_date) and a.物料内码=c.物料内码 and a.仓库=c.仓库),0)-isnull((select 出库金额 from fact_t4 d where d.单据日期 = dateadd(d,@i,@the_date) and a.物料内码=d.物料内码 and a.仓库=d.仓库 ),0)
from (
select distinct
仓库
,物料内码
from fact_t4 ) a
) s where s.结存数量 <> 0 and s.结存金额 <> 0
set @i=@i+1
End
相关文章推荐
- 将表数据生成Insert脚本 比较好用的生成插入语句的SQL脚本
- 【分享】通过Excel生成批量SQL语句,处理大量数据的好办法
- Sql数据字典生成语句
- C# 利用sql语句生成新的数据表
- PB 中用带参数的 SQL 语句字符串检索数据并动态生成Datawindow
- 如何将sqlserver表中的数据导出sql语句或生成insert into语句【转载】
- 把excel数据生成sql insert语句
- 写了一条Sybase SQL语句,用于生成一个表中所有数据的插入记录的生成
- 如何将sqlserver表中的数据导出sql语句或生成insert into语句
- 用Sql语句生成Sqlserver数据字典
- SQLServer2000、2005/2008 生成数据字典SQL语句
- 通过Excel生成批量SQL语句,处理大量数据的好办法
- Excel数据生成Sql语句的方法
- 把excel数据生成sql insert语句
- Oracle自动生成数据字典的SQL语句
- 用Sql语句生成Sqlserver数据字典
- SQLServer2008/2005 生成数据字典SQL语句
- 用sql语句生成 数据字典
- Excel数据生成SQL insert语句
- 生成数据表的SQL 语句