本来的表是这样的
库存表 goods
       id 货物编号,货物名称,货物库存量  
出入库表 dan
货物编号,数量,日期,出入库标记
--------------------------我如果做成三个表可以吗库存表 goods
       id 货物编号,货物名称,货物库存量  
出货表 dan1
       货物编号,数量,日期
入货表 dan2
      货物编号,数量,日期
-------------------如果可以,在查询某货物在某时间段内的进出情况的语句该
怎样修改
本来的SQL语句是
query.sql.add('select * from dan where 日期>:rq1 and 日期<:rq2 and
货物编号 = 指定货物');
query.parambynaem('rq1').asdatetime:= strtodate(edit1.text)
query.parambynaem('rq2').asdatetime:= strtodate(edit2.text)
 

解决方案 »

  1.   

    select * from 
    (select * from dan1
    union
    select * from dan2) a
    where 日期>:rq1 and 日期<:rq2 and
    货物编号 = 指定货物'
      

  2.   

    select * from goods
    where 货物编号 in
    (select * from dan1
    union
    select * from dan2) a
    where a.日期>:rq1 and a.日期<:rq2 and
    货物编号 = 指定货物'
      

  3.   

    有个例子,我写的,你自己从里面找吧.
    if exists(select 1 from sysobjects where name='saletotal_sp' and type='P')
    drop proc saletotal_sp
    go
    CREATE PROC saletotal_sp
    @customerno CHAR(8),
    @customertypename char(20),
    @startdate datetime,
    @enddate datetime
    as
    BEGINif ltrim(rtrim(@customertypename))='' or ltrim(rtrim(@customertypename)) is null
    begin
      set @customertypename='%'
    end
    else 
    begin
      set @customertypename=ltrim(rtrim(@customertypename))+'%'
    endif @startdate='' or @startdate is null
    begin
      set @startdate='1900-01-01'
    end
    if @enddate='' or @enddate is null
    begin
      set @enddate=getdate()
    end----建立出货统计结果表--------------------
    create table #saletotalresult(
    saleinvno char(10) null,                 --发票号码 
    customerno char(8) null ,                --客户编号 
    customername char(60) null ,             --客户名称 
    customertypename char(20) null,          --客户类别名称
    date datetime null,                      --出货日期
    totalmoney money not null default 0,     --出货金额 
    curno char(3) null                       --货币编号 
    )
    insert #saletotalresult
    select
    saleinvno,
    customerno,
    (select top 1 customername from customer where customerno=a.customerno) as customername,
    (select top 1 customertypename from customertype where customertypeno=(select top 1 customertypeno from customer where customerno=a.customerno))  as customertypename,
    date ,
    sum(b.qty*b.price) as totalmoney,
    b.curno
    from
    mstc a,dstc b
    where (valider is NOT null)
       and b.invno=a.invno
    group by a.saleinvno,a.customerno,a.date,b.curno
    order by a.customerno,a.date
    if (ltrim(rtrim(@CUSTOMERNO))='') or (ltrim(rtrim(@CUSTOMERNO)) is null) 
    begin
      select * from #saletotalresult 
       where customertypename like ltrim(rtrim(@customertypename))
         and date between @startdate and @enddate
       order by saleinvno,date
    end
    else
    begin
      select * from #saletotalresult  
       where customerno=@customerno
         and customertypename like ltrim(rtrim(@customertypename ))
         and date between @startdate and @enddate
       order by saleinvno,date
    end