菜鸟SQL语句问题。请各位大侠帮忙(80分)

  • 主题发起人 主题发起人 me555555
  • 开始时间 开始时间
M

me555555

Unregistered / Unconfirmed
GUEST, unregistred user!
有一数据库登记内容如下
日期 商品 进出 数量 库存
2月6日 啤酒 进 50 50
2月8日 白酒 进 35 35
2月9日 白酒 出 15 20
2月10日 啤酒 出 20 30
3月5日 啤酒 进 60 90
3月8日 白酒 进 35 55
3月12日 白酒 出 15 40
3月15日 啤酒 出 20 70
求一SQL语句得下面要求
3月份进出商品月报表(是3月的库存表)
------------------------------------------------
商品名称 月初库存(既2月底) 月入库 月出库 月末库存(既3月底)
白酒 20 35 15 40
啤酒 30 60 20 70
 
DATEPART( )也行
 
select a.商品,sum(a.进出) AS 月入库,sum(b.进出) AS 月出库,c.库存 as 月末库存 from (select * from table where 进出 ='进'and 日期 like '3月%') a left join (select * from table where 进出 ='出' and 日期 like '3月%') b on a.商品=b.商品 left join (select * from table where 日期 =(SELECT MAX(日期) from table where 日期 like '3月%')) c on b.on b.商品=c.商品 group by a.商品
 
select distinct 商品,
月初库存=(select top 1 t1.库存 from 表名 t1 where t1.商品=t.商品 and t1.日期<'2006-3-1' order by t1.日期 desc),
月入库=(select sum(t2.数量) from 表名 t2 where t2.商品=t.商品 and t2.日期 between '2006-3-1' and '2006-3-31' and t2.进出='进'),
月出库=(select sum(t3.数量) from 表名 t3 where t3.商品=t.商品 and t3.日期 between '2006-3-1' and '2006-3-31' and t3.进出='出'),
月末库存=(select top 1 库存 from 表名 t4 where t4.商品=t.商品 and t4.日期<'2006-4-1' order by t4.日期 desc)
from 表名 t
 
都不行啊 中文对应的英文如下
表 : DEPOTTABLE
日期 : depotdate
商品 : fdcname
库存 : residue
进出 : inorout (进 = 0 ,出= 1)
数量 : depotnum

------------表DEPOTTABLE中实际数据如下-------------------------
depotdate fdcname inorout depotnum residue
'2006-2-6' 啤酒 0 50 50
'2006-2-8' 白酒 0 35 35
'2006-2-9' 白酒 1 15 20
'2006-2-10' 啤酒 1 20 30

'2006-3-5' 啤酒 0 60 90
'2006-3-8' 白酒 0 35 55
'2006-3-12' 白酒 1 15 40
'2006-3-15' 啤酒 1 20 70

求一SQL语句得下面要求
3月份进出商品月报表(是3月的库存表)
------------------------------------------------
商品名称 月初库存(既2月底) 月入库 月出库 月末库存(既3月底)
白酒 20 35 15 40
啤酒 30 60 20 70

测试时输入了2个商品。实际上有很多商品的。大侠帮帮才鸟啊
 
用存储过程很容易搞定啊。
 
接受答案了.
 

Similar threads

D
回复
0
查看
2K
DelphiTeacher的专栏
D
D
回复
0
查看
2K
DelphiTeacher的专栏
D
D
回复
0
查看
1K
DelphiTeacher的专栏
D
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
后退
顶部