里面的Amount表示数量(吨) 计算单位为 每天每吨20元
解决方案 »
- ◎◎◎◎◎◎事务,锁◎◎◎◎◎◎
- 请大家帮忙,这个SQL语句该怎么写?
- select distinct A,B from Table1 order by C,排序字段不在select distinct选择字段中,如何做?
- 发布订阅问题,万分紧急~在线等
- 关于数据导入问题!如果将Excle表中指定字段(列)的数据导入到SQL Server中指定表的指定字段中去?
- 动态修改数据库 在线等 急
- “由于数据移动,未能继续以 NOLOCK 方式扫描”,如何解决啊?
- 请教用VB安装SQL SERVER的数据库的问题。
- 一个很菜的问题(数据库移植!!!)
- 为什么向sql添加数据就会出现等待LCK_M_IX
- 如果我想知道某个用户在某个表的使用权限,还有想知道某个表所有用户对这个表的权限用sql语句应该怎么办
- 为什么联合查询出来有4个记录?
Optype 是 int 类型的 为 0,1,2 表示入库 3,4,5 表示入库
Optype 是 int 类型的 为 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月份留下的库存)
* 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
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
如果 用select @month='2005-09' 这样好像得到的结果是错误的
from tb1 where convert(char(7),opDate,120)<=@month group by prodid,batchno
該語句就是算本月留下的庫存,
--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
谢谢
为什么select @month='2005-1'这个也有费用呀 .应该是没有得 因为这个时候没库存呀