工单明细排程
单号 排程序号 开工日期 完工日期 数量
M01 1 2009-3-17 2009-3-18 2000
2 2009-3-17 2009-3-19 2000
3 2009-3-17 2009-3-20 2000

工单入库明细
单号 入库日期
M01 2009-3-18 1000
2009-3-19 3000
2009-3-20 2000


单号 排程序号 开工日期 完工日期 数量 入库数量 状态
M01 1 2009-3-17 2009-3-18 2000 1000 延期
2 2009-3-17 2009-3-19 2000 3000 正常
3 2009-3-17 2009-3-20 2000 2000 正常如何根据前两张表得出第三张生产延期明细?

解决方案 »

  1.   

    select m.*,状态='延期' from 工单明细排程 m,工单入库明细 n where m.单号 = n.单号 and m.完工日期 = n.入库日期 and m.数量>n.数量
    union all
    select m.*,状态='正常' from 工单明细排程 m,工单入库明细 n where m.单号 = n.单号 and m.完工日期 = n.入库日期 and m.数量=n.数量
      

  2.   


        select a.*,b.入庫數量,
        (case when b.入庫數量<a.數量  then '延期' 
             else '正常' end ) as 狀態
        From 工单明细排程  a,工单入库明细 b
        where  a.單號=b.單號 and a.完工日期=b.入庫日期
      

  3.   

    可能我没说清楚
    第三第表的入库取值为
    排程序号1 取1000,即18号前入的1000
    排程序号2 取3000,即18~25号前入的合计
    排程序号3 取2000,即25以后入的合计工单明细排程
    单号 排程序号 开工日期 完工日期 数量
    M01 1 2009-3-17 2009-3-18 2000
    2 2009-3-17 2009-3-25 2000
    3 2009-3-17 2009-3-29 2000

    工单入库明细
    单号 入库流水 入库日期
    M01 1 2009-3-18 1000
    2 2009-3-19 1000
    3 2009-3-20 2000
    4 2009-3-26 2000

    单号 排程序号 开工日期 完工日期 数量 入库数量 状态     
    M01 1 2009-3-17 2009-3-18 2000 1000 延期
    2 2009-3-17 2009-3-25 2000 3000 正常
    3 2009-3-17 2009-3-29 2000 2000 正常
      

  4.   

    if object_id('[工单明细排程]') is not null drop table [工单明细排程]
    go
    create table [工单明细排程]([单号] varchar(3),[排程序号] int,[开工日期] datetime,[完工日期] datetime,[数量] int)
    insert [工单明细排程]
    select 'M01',1,'2009-3-17','2009-3-18',2000 union all
    select 'M01',2,'2009-3-17','2009-3-25',2000 union all
    select 'M01',3,'2009-3-17','2009-3-29',2000
    if object_id('[工单入库明细]') is not null drop table [工单入库明细]
    go
    create table [工单入库明细]([单号] varchar(3),[入库流水] int,[入库日期] datetime,[数量] int)
    insert [工单入库明细]
    select 'M01',1,'2009-3-18',1000 union all
    select 'M01',2,'2009-3-19',1000 union all
    select 'M01',3,'2009-3-20',2000 union all
    select 'M01',4,'2009-3-26',2000--select * from [工单明细排程]
    --select * from [工单入库明细]select *,[状态]=case when [数量]>[入库数量] then '延期' else '正常' end
    from
    (
    select  *
    ,[入库数量]=(select sum([数量]) from [工单入库明细] 
    where [入库日期]>isnull((select [完工日期] from [工单明细排程] where [排程序号]=t.[排程序号]-1),t.[开工日期]) 
    and [入库日期]<=t.[完工日期])
    from [工单明细排程] t
    ) t
    --测试结果:
    /*
    单号   排程序号  开工日期                完工日期                数量        入库数量    状态
    ---- ----------- ----------------------- ----------------------- ----------- ----------- ----
    M01  1           2009-03-17 00:00:00.000 2009-03-18 00:00:00.000 2000        1000        延期
    M01  2           2009-03-17 00:00:00.000 2009-03-25 00:00:00.000 2000        3000        正常
    M01  3           2009-03-17 00:00:00.000 2009-03-29 00:00:00.000 2000        2000        正常(3 行受影响)
    */
      

  5.   

    select *,[状态]=case when [数量]>[入库数量] then '延期' else '正常' end
    from
    (
    select  *
    ,[入库数量]=(select sum([数量]) from [工单入库明细] 
    where [入库日期]>isnull((select [完工日期] from [工单明细排程] where [单号]=t.[单号] and [排程序号]=t.[排程序号]-1),t.[开工日期]) 
    and [入库日期]<=t.[完工日期])
    from [工单明细排程] t
    ) t