日期:2014-05-18  浏览次数:20603 次

初始期数,月结,库存
原表
期初 收 发 期末
2010/7/18 1257 0 0 1257 0.5mmx8-3/4吋x不锈钢卷片
2010/9/8 0 0 1257 0 0.5mmx8-3/4吋x不锈钢卷片
2010/9/28 0 3694 0 3694 0.5mmx8-3/4吋x不锈钢卷片
2010/10/19 0 0 949 2745 0.5mmx8-3/4吋x不锈钢卷片
2010/10/23 0 0 850 1895 0.5mmx8-3/4吋x不锈钢卷片
2010/11/1 0 0 0 943 952 0.5mmx8-3/4吋x不锈钢卷片
我想要的是 这样。。
期初 收 发 期末
2010/7/18 1257 0 0 1257 0.5mmx8-3/4吋x不锈钢卷片
2010/9/8 1257 0 1257 0 0.5mmx8-3/4吋x不锈钢卷片
2010/9/28 0 3694 0 3694 0.5mmx8-3/4吋x不锈钢卷片
2010/10/19 3697 0 949 2745 0.5mmx8-3/4吋x不锈钢卷片
2010/10/23 0 0 850 1895 0.5mmx8-3/4吋x不锈钢卷片
2010/11/1 0 1895 0 943 952 0.5mmx8-3/4吋x不锈钢卷片


2010/7/18 1257 0 0 1257 0.5mmx8-3/4吋x不锈钢卷片
2010/9/1 1257 0 1257 1257 0.5mmx8-3/4吋x不锈钢卷片2010/9/8 0 0 1257 0 0.5mmx8-3/4吋x不锈钢卷片
2010/9/28 0 3694 0 3694 0.5mmx8-3/4吋x不锈钢卷片
2010/10/19 3694 0 0 3694 0.5mmx8-3/4吋x不锈钢卷片
2010/10/19 0 0 949 2745 0.5mmx8-3/4吋x不锈钢卷片
2010/10/23 0 0 850 1895 0.5mmx8-3/4吋x不锈钢卷片
2010/11/01 1895 0 0 1895 0.5mmx8-3/4吋x不锈钢卷片2010/11/1 0 0 0 943 952 0.5mmx8-3/4吋x不锈钢卷片

------解决方案--------------------
SQL code


CREATE TABLE [dbo].[test](
    [id] [int] IDENTITY(1,1) NOT NULL,
    [riqi] [smalldatetime] NULL,
    [qichu] [int] NULL,
    [shou] [int] NULL,
    [fa] [int] NULL,
    [qimo] [int] NULL,
    [title] [varchar](50) NULL,
 CONSTRAINT [PK_test] PRIMARY KEY CLUSTERED 
(
    [id] ASC
)
) ON [PRIMARY]

--测试数据
INSERT INTO test(riqi, qichu, shou, fa, qimo, title)
VALUES (CONVERT(DATETIME, '2010/7/18', 102), 1257, 0, 0, 1257, '0.5mmx8-3/4吋x不锈钢卷片')
INSERT INTO test(riqi, qichu, shou, fa, qimo, title)
VALUES (CONVERT(DATETIME, '2010/9/8', 102), 0, 0, 1257, 0, '0.5mmx8-3/4吋x不锈钢卷片')
INSERT INTO test(riqi, qichu, shou, fa, qimo, title)
VALUES (CONVERT(DATETIME, '2010/9/28', 102), 0, 3694, 0, 3694, '0.5mmx8-3/4吋x不锈钢卷片')
INSERT INTO test(riqi, qichu, shou, fa, qimo, title)
VALUES (CONVERT(DATETIME, '2010/10/19', 102), 0, 0, 949, 2745, '0.5mmx8-3/4吋x不锈钢卷片')
INSERT INTO test(riqi, qichu, shou, fa, qimo, title)
VALUES (CONVERT(DATETIME, '2010/10/23', 102), 1, 0, 850, 1895, '0.5mmx8-3/4吋x不锈钢卷片')
INSERT INTO test(riqi, qichu, shou, fa, qimo, title)
VALUES (CONVERT(DATETIME, '2010/11/10', 102), 0, 0, 943, 952, '0.5mmx8-3/4吋x不锈钢卷片')

--更新每月第一条记录的期初数(即前一次收发的期末数)
--如果id排序与riqi排序不一致,可能会导致结果不正确。
update [test]
set qichu=(select top 1 qimo from [test] t2 where t2.id<test.id order by id desc)
where id in(
    select id from (
    SELECT [id],qimo,
          (select COUNT(1) from [test] t1 where t1.riqi<t.riqi and YEAR(t.riqi)=YEAR(t1.riqi) and MONTH(t.riqi)=MONTH(t1.riqi)) as st
      FROM [pay].[dbo].[test] t) b where st=0 and id>1
  )
  
--插入月初记录
--如果当月无任务记录则无法补充月初记录
--找出每月第一条记录,判断是否为月初,不为月初时则返回月初记录值
--将月初记录值插入到表中
INSERT INTO test(riqi, qichu, shou, fa, qimo, title)
select yuechu as riqi,
((select top 1 qimo from [test] t2 where t2.riqi<d.yuechu order by id desc)) as qichu,
0 as shou,
0 as fa, 
((select top 1 qimo from [test] t2 where t2.riqi<d.yuechu order by id desc))  as qimo,title 

from
(      
    select *,(select dateadd(d,-day(riqi)+1,riqi)) as yuechu from test
      where id in(
        select id from (
            SELECT [id],qimo,
              (select COUNT(1) from [test] t1 where t1.riqi<t.riqi and YEAR(t.riqi)=YEAR(t1.riqi) and MONTH(t.riqi)=MONTH(t1.riqi)) as st
            FROM [pay].[dbo].[test] t) b where st=0 and id>1
            )
) d where riqi<>yuechu
 
 
 select * from test order by riqi
 
 id          riqi                    qichu       shou        fa          qimo        title
----------- ----------------------- ----------- ----------- ----------- ----------- --------------------------------------------------
1           2010-07-18 00:00:00     1257        0           0           1257        0.5mmx8-3/4吋x不锈钢卷片
2           2010-09-08 00:00:00     1