每月的月初量都是存在上月的最后一天么?
select * from table1 
where year(操作时间)=year(getdate()) and month(操作时间)=month(getdate()) and 地点<>'月初量' 
or (year(操作时间)=year(getdate()) and month(操作时间)=month(getdate())-1 and 地点='月初量')

解决方案 »

  1.   

    CREATE TABLE Eh
    (
    /*ID*/ EhID INT IDENTITY(1,1) PRIMARY KEY,
    /*操作日期*/ OperateDate DATETIME NOT NULL,
    /*作业单编号*/ JobID_FK INT,
    /*目的地*/ Destination NVARCHAR(30) NOT NULL,
    /*货物*/ CargoName NVARCHAR(30) NOT NULL,
    /*货物数量*/ CargoNumber INT
    )
    INSERT Eh VALUES (CAST('2004-9-1' AS DATETIME),833,N'东方路',N'胶管VA-P12',185)
    INSERT Eh VALUES (CAST('2004-9-1' AS DATETIME),833,N'东方路',N'螺杆M12*160',185)
    INSERT Eh VALUES (CAST('2004-9-1' AS DATETIME),833,N'东方路',N'内六角M10*25',10)
    INSERT Eh VALUES (CAST('2004-9-2' AS DATETIME),836,N'海口',N'进口胶枪VM-P345',2)
    INSERT Eh VALUES (CAST('2004-9-2' AS DATETIME),843,N'天山路',N'螺杆M12*160',16)
    INSERT Eh VALUES (CAST('2004-9-2' AS DATETIME),843,N'天山路',N'螺杆M20*240',8)
    INSERT Eh VALUES (CAST('2004-9-2' AS DATETIME),843,N'天山路',N'螺杆M16*190',10)
    INSERT Eh VALUES (CAST('2004-9-2' AS DATETIME),843,N'天山路',N'螺杆M12*160',16)
    INSERT Eh VALUES (CAST('2004-10-1' AS DATETIME),833,N'东方路',N'内六角M10*25',10)
    INSERT Eh VALUES (CAST('2004-10-2' AS DATETIME),836,N'海口',N'进口胶枪VM-P345',2)
    INSERT Eh VALUES (CAST('2004-10-2' AS DATETIME),843,N'天山路',N'螺杆M12*160',16)create table test2 (
    year int, /*年*/
    month int, /*月*/
    CargoName nvarchar(50), /*货物名称*/
    total int /*该年月的库存*/
    )
    insert test2 values (2004,9,N'进口胶枪VM-P345',18000)
    insert test2 values (2004,9,N'胶管VA-P12',2560)
    insert test2 values (2004,10,N'进口胶枪VM-P345',16000)
    insert test2 values (2004,10,N'胶管VA-P12',2000)
    go--处理的存储过程
    create proc p_process
    @dt datetime=null,--要统计数据的截止日期,为null则取当前日期
    @bz bit=0         --如果为1,则把今天的库存数量写入月初数表test2
                      --用标识来处理是为了处理的控制上方便,而不是限死了只有最后一天才处理
    as
    set nocount on 
    --规范参数
    if @dt is null 
    set @dt=convert(char(10),getdate(),120)
    else
    set @dt=convert(char(10),@dt,120)declare @dt1 datetime,@dt2 datetime,@dt3 char(10)
    select @dt1=convert(char(7),@dt,120)+'-01',@dt2=@dt+1
    ,@dt3=convert(char(10),@dt1-1,120)
    select OperateDate=convert(char(10),OperateDate,120),JobID_FK,Destination
    ,CargoName,CargoNumber=sum(CargoNumber),bz=-1
    into #t from Eh
    where OperateDate>=@dt1 and OperateDate<@dt2
    group by convert(char(10),OperateDate,120),JobID_FK,Destination
    ,CargoName
    union all
    select a.*,isnull(b.CargoName,''),isnull(b.total,0),bz=1
    from(select OperateDate=@dt3,JobID_FK=0,Destination='月初量')a
    left join(  --无初始化数据时,也要显示月初那行
    select CargoName,total
    from test2
    where [year]=year(@dt3) and [month]=month(@dt3)
    )b on 1=1declare @s1 varchar(8000),@s2 varchar(8000),@s3 varchar(8000)
    select @s1='',@s2=''''+convert(char(10),@dt,120)+''',0,''剩余数量'''
    ,@s3=case when @bz=0 then '' else '
    set xact_abort on
    begin tran
    insert test2([year],[month],[CargoName],[total])
    select '''+datename(year,@dt)+''','''+datename(month,@dt)
    +''',CargoName,sum(CargoNumber*bz)
    from #t
    group by CargoName
    commit tran' end
    select @s1=@s1+',['+rtrim(CargoName)+']=sum(case CargoName when '''+rtrim(CargoName)+''' then CargoNumber else 0 end)'
    ,@s2=@s2+',['+rtrim(CargoName)+']=sum(case CargoName when '''+rtrim(CargoName)+''' then CargoNumber*bz else 0 end)'
    from #t where CargoName>'' group by CargoName
    exec('
    select * from(
    select top 100 percent
    OperateDate,JobID_FK,Destination'+@s1+'
    from #t 
    group by OperateDate,JobID_FK,Destination
    order by OperateDate,JobID_FK)a
    union all
    select '+@s2+'
    from #t
    '+@s3+'
    ')
    go--调用
    exec p_process '2004-9-11'
    exec p_process '2004-10-11'
    exec p_process '2004-11-30',1
    select * from test2
    go--删除测试
    drop proc p_process
    drop table test2,Eh/*--测试结果(自己看)--*/