做个存储过程或函数来解决:create proc s_order
@sOrder char(8)--定单号
@sType char(2)--类型编号
@sGoodsID cahr(8)--货品编号if sType ='11'
begin
select ListTime.时间,ListTime.单据号,ListTime.单据类型, InList.数量
from ListTime ,InList
where ListTime.单据号=InList.单据号
and ListTime.单据号=@sOrder
and InList.货品编号=@sGoodsID
end
else if sType ='21'
begin
select ListTime.时间,ListTime.单据号,ListTime.单据类型, OutList.数量
from ListTime ,InList
where ListTime.单据号=OutList.单据号
and ListTime.单据号=@sOrder
and OutList.货品编号=@sGoodsID
end
@sOrder char(8)--定单号
@sType char(2)--类型编号
@sGoodsID cahr(8)--货品编号if sType ='11'
begin
select ListTime.时间,ListTime.单据号,ListTime.单据类型, InList.数量
from ListTime ,InList
where ListTime.单据号=InList.单据号
and ListTime.单据号=@sOrder
and InList.货品编号=@sGoodsID
end
else if sType ='21'
begin
select ListTime.时间,ListTime.单据号,ListTime.单据类型, OutList.数量
from ListTime ,InList
where ListTime.单据号=OutList.单据号
and ListTime.单据号=@sOrder
and OutList.货品编号=@sGoodsID
end
declare @tbl table(IOrder char(8),GoodsID char(8),iCount numberic,Order_type char(2))
intser @tbl
select *,'11' from InList where GoodsID=@sGoodID(给定的一个货品编号)and IdOrder=@sOrder(给定的单据号)
intser @tbl
select *,'21' from OutList where GoodsID=@sGoodID(给定的一个货品编号)and IdOrder=@sOrder(给定的单据号)
select t1.*,t2.iCount
(select time,IdOder,Order_type
from ListTime where Order_type=@sType(给定的单据类型)
) t1
left join
@tbl on t1.IdOrder=tbl.IOrder
因为临时表中的数据已进行表件的限制,即使数据库中有很多的记录,此查询也是很高效的
Where 类型编号 = '11' and 单据号 = '09014'Select ListTime.时间,ListTime.单据号,'11' as单据类型,
InList.数量 From ListTime, InList
Where ListTime.单据号 = '11'
and InList.货品编号 = '501014'
and ListTime.时间 > @qtime
and ListTime.单据号 = InList.单据号
Union AllSelect ListTime.时间,ListTime.单据号,'21' as 单据类型,
OutList.数量 From ListTime, OutList
Where ListTime.单据号 = '21'
and OutList.货品编号 = '501014'
and ListTime.时间 > @qtime
and ListTime.单据号 = OutList.单据号
就在个查询中加个条件: 时间>@qtime
@qtime 就象 foolflyfish(progame) 的那样了!