里面的Amount表示数量(吨)   计算单位为 每天每吨20元

解决方案 »

  1.   

    日期是 DateTime 类型的 
    Optype  是 int 类型的 为 0,1,2 表示入库  3,4,5 表示入库
      

  2.   

    不好意思上面写错了 应该是
    Optype  是 int 类型的 为 0,1,2 表示入库  3,4,5 表示出库
      

  3.   

    To  ReViSion(和尚)  是的 上个月没有出完的,又要算1号进的 
    有人能做吗 ???谢谢
      

  4.   

    我把记录列一下 (Optype是nt 类型的 为 0,1,2 表示入库  3,4,5 表示出库)
    ID    prodID    batchNO     opDate      opType     amount
    1       1        NO001     2005-4-1       1         10
    2       1        NO001     2005-4-1       3         2
    3       1        NO001     2005-4-3       3         2
    4       1        NO001     2005-5-3       4         2如果统计4月份的费用应该是   去掉1号出库的2吨  (10-4)吨*(4月份的天数-1)天*20元  + 2吨*2天*20元   5月份类推(必须考虑4月份留下的库存)
      

  5.   

    按我的理解写了一个,你看合不合用SELECT batchno, SUM(20 * (CASE WHEN optype < 3 THEN 1 ELSE - 1 END) 
          * amount * (CASE WHEN month(getdate()) > month(opdate) THEN day(getdate()) 
          ELSE datediff(day, opdate, getdate()) END))
    FROM csdn_test
    WHERE opdate < getdate()
    GROUP BY batchno
      

  6.   

    如果要统计到哪一天的,将getdate()改成你要统计的日期
      

  7.   

    create table tb1(ID int identity(1,1),   prodID varchar(10),   batchNO varchar(10),     opDate datetime,      opType int,   amount int)
    insert into tb1(prodid,batchno,opdate,optype,amount)
    select 1   ,     'NO001',     '2005-3-10'   ,    1 ,        10
    union all select 1  ,      'NO001' ,    '2005-4-1'  ,     3  ,       2
    union all select 1 ,       'NO001'  ,   '2005-4-3' ,      3   ,      2
    union all select 1,        'NO001'   ,  '2005-5-3',       4    ,     2
    union all select 1  ,      'NO001' ,    '2005-5-1'  ,     3  ,       2
    union all select 1 ,       'NO002'  ,   '2005-5-3' ,      1   ,      2
    union all select 1,        'NO002'   ,  '2005-5-4',       4    ,     2
    union all select 2  ,      'NO001' ,    '2005-4-1'  ,     1  ,       20
    union all select 2 ,       'NO001'  ,   '2005-4-3' ,      3   ,      2
    union all select 2,        'NO001'   ,  '2005-5-3',       4    ,     2declare @month varchar(7),@date datetime
    select @month='2005-03',@date=dateadd(m,1,cast((@month+'-01') as datetime))-1select sum(datediff(day,b.opdate,a.opdate)*a.amount*20) from 
    (
    select prodid,batchno,opdate,amount from tb1 where convert(char(7),opDate,120)=@month and optype>2 union all 
    select prodid,batchno,@date as opdate,sum(case when optype>2 then 0 else amount end)-sum(case when optype>2 then amount else 0 end) as amount
    from  tb1 where convert(char(7),opDate,120)<=@month group by prodid,batchno
    ) a,(
    select prodid,batchno,(case when convert(char(7),opDate,120)<@month then cast((@month+'-01') as datetime) else opdate end) as opDate from tb1 where optype<=2
    ) b 
    where a.prodid=b.prodid and a.batchno=b.batchnodrop table tb1
      

  8.   

    To   xiaomeixiang(Little Sheep) 请问一下 您考虑了上个月留下的库存了嘛 ???
    如果 用select @month='2005-09' 这样好像得到的结果是错误的
      

  9.   

    select prodid,batchno,@date as opdate,sum(case when optype>2 then 0 else amount end)-sum(case when optype>2 then amount else 0 end) as amount
    from  tb1 where convert(char(7),opDate,120)<=@month group by prodid,batchno
    該語句就是算本月留下的庫存,
      

  10.   

    --要不你用這條語句查一下數據是不是正確,
    --type是我分的類型,begindate/enddate分別代表計算費用的開始/結束日期,amount為數量,days為費用天數,money為費用
    select a.prodid,a.batchno,type,b.opdate as begindate,a.opdate as enddate,amount,datediff(day,b.opdate,a.opdate) as days,datediff(day,b.opdate,a.opdate)*a.amount*20 as money from 
    (
    select prodid,batchno,opdate,amount,'Normal' as type from tb1 where convert(char(7),opDate,120)=@month and optype>2 union all 
    select prodid,batchno,@date as opdate,sum(case when optype>2 then 0 else amount end)-sum(case when optype>2 then amount else 0 end) as amount,'結存' as type
    from  tb1 where convert(char(7),opDate,120)<=@month group by prodid,batchno
    ) a,(
    select prodid,batchno,(case when convert(char(7),opDate,120)<@month then cast((@month+'-01') as datetime) else opdate end) as opDate from tb1 where optype<=2
    ) b 
    where a.prodid=b.prodid and a.batchno=b.batchno
      

  11.   

    To   xiaomeixiang(Little Sheep)  
    谢谢   
      

  12.   

    To   xiaomeixiang(Little Sheep)  
    为什么select @month='2005-1'这个也有费用呀 .应该是没有得 因为这个时候没库存呀
      

  13.   

    哦,我用的convert(char(7),opDate,120)這個函數返回的格式是“2005-01”這樣的,所以你把它改成這樣就可以啦,select @month='2005-01'