有 两个表 product 和 store---商品表
Create table product(
id int identity(1,1) primary key not null, --主键 【商品ID】
Pno varchar(50) not null, --唯一约束 --【打印商品ID】
name varchar(50) not null,--【商品名】
price_buy money not null --【进购价】
)
--商品出入库记录表
Create table store(
store_no varchar(50) primary key not null, --【出入库编号】
s_no varchar(50) not null,--订单编号或是退货编号
store_id int not null, --【仓库ID】
productId int not null,--【商品ID】 对应product 的id
count int not null, --【出入库商品数量】(出库为负数/入库为正数)
storeType int not null,--【出入库类型】 (采购/销售/退货)
oTime datetime --【出入库时间】
)
--需要的报表为
商品打印ID[Pno],商品名[name],上月存量,本月进量,本月出量,本月存量,进购价[price_buy],资产价(进购*本月存量)
Create table product(
id int identity(1,1) primary key not null, --主键 【商品ID】
Pno varchar(50) not null, --唯一约束 --【打印商品ID】
name varchar(50) not null,--【商品名】
price_buy money not null --【进购价】
)
--商品出入库记录表
Create table store(
store_no varchar(50) primary key not null, --【出入库编号】
s_no varchar(50) not null,--订单编号或是退货编号
store_id int not null, --【仓库ID】
productId int not null,--【商品ID】 对应product 的id
count int not null, --【出入库商品数量】(出库为负数/入库为正数)
storeType int not null,--【出入库类型】 (采购/销售/退货)
oTime datetime --【出入库时间】
)
--需要的报表为
商品打印ID[Pno],商品名[name],上月存量,本月进量,本月出量,本月存量,进购价[price_buy],资产价(进购*本月存量)
declare @args ...declare cur_cursorname cursor for
select ..
from ..
where ..
order by ..open cur_cursorname
fetch next from cur_cursorname into @args..
while @@fetch_status!=-1 begin
-- do something by business logic here with @args..
fetch next from cur_cursorname into @args..
end
close cur_cursornamedeallocate cur_cursorname
declare @thismonth datetime
set @thismonth = dateadd(month, datediff(month, 0, getdate()), 0)select
ID = [Pno],商品名 = p.[name]
,上月存量 = isnull(lastcount,0)
,本月进量 = isnull(thisincome,0)
,本月出量 = isnull(thisoutgoing,0)
,本月存量
= isnull(lastcount,0)
+ isnull(thisincome,0)
- isnull(thisoutgoing,0)
,进购价 = [price_buy]
,资产价 -- (进购*本月存量)
=(isnull(lastcount,0)
+ isnull(thisincome,0)
- isnull(thisoutgoing,0)
)*[price_buy]
from product p
left join (
-- 上月存量
select productId, sum(count) as lastcount
from store
where oTime < @thismonth
group by productId
) l on l.productid = p.id
left join (
-- 本月进量
select productId, sum(count) as thisincome
from store
where oTime >= @thismonth
and storeType in (0,2) --【出入库类型】 (采购/销售/退货)
group by productId
) tin on tin.productid = p.id
left join (
-- 本月出量
select productId, sum(count) as thisoutgoing
from store
where oTime >= @thismonth
and storeType in (1) --【出入库类型】 (采购/销售/退货)
group by productId
) tou on tou.productid = p.id