SELECT NULLIF(A。sparecode  ,B。sparecode ) sparecode  ,累计入库, 累计出库
FROM (select checkoutlist.sparecode,sum(isnull(checkoutlist.amount,0)) as '累计出库' from checkout inner join checkoutlist on checkout.objid=checkoutlist.bindparent 
where checkoutlist.sparecode ='026100024W'and checkout.createdate between '2003-01-01'and '2003-05-31'
 group by checkoutlist.sparecode)A FULL JOIN (select planinlist.sparecode,sum(isnull(planinlist.factamount,0)) as '累计入库' from  planin inner join planinlist on planin.objid=planinlist.bindparent 
where  planinlist.sparecode='026100024W' and planin.finishdate between '2003-01-01'and '2003-05-31'
group by planinlist.sparecode) B ON A。sparecode  =B。sparecode

解决方案 »

  1.   

    select b.sparecode,b.累计入库,a.累计出库 from (
    select checkoutlist.sparecode,sum(isnull(checkoutlist.amount,0)) 累计出库 from checkout join checkoutlist on checkout.objid=checkoutlist.bindparent 
    where checkoutlist.sparecode ='026100024W'and checkout.createdate between '2003-01-01'and '2003-05-31'
     group by checkoutlist.sparecode) a left join (
    select planinlist.sparecode,sum(isnull(planinlist.factamount,0)) 累计入库 from  planin join planinlist on planin.objid=planinlist.bindparent 
    where  planinlist.sparecode='026100024W' and planin.finishdate between '2003-01-01'and '2003-05-31'
    group by planinlist.sparecode) b
    on a.sparecode=b.sparecode
      

  2.   

    SELECT NULLIF(A。sparecode  ,B。sparecode ) sparecode  ,ISNULL(累计入库,0) 累计入库, ISNULL(累计出库,0) 累计出库
    FROM (select checkoutlist.sparecode,sum(isnull(checkoutlist.amount,0)) as '累计出库' from checkout inner join checkoutlist on checkout.objid=checkoutlist.bindparent 
    where checkoutlist.sparecode ='026100024W'and checkout.createdate between '2003-01-01'and '2003-05-31'
     group by checkoutlist.sparecode)A FULL JOIN (select planinlist.sparecode,sum(isnull(planinlist.factamount,0)) as '累计入库' from  planin inner join planinlist on planin.objid=planinlist.bindparent 
    where  planinlist.sparecode='026100024W' and planin.finishdate between '2003-01-01'and '2003-05-31'
    group by planinlist.sparecode) B ON A。sparecode  =B。sparecode
      

  3.   

    pengdali(大力 V2.0) 
    left join 好象有点问题
    如果有累计入库 ,无累计出库 呢
      

  4.   

    写惯了left join:
    select b.sparecode,b.累计入库,a.累计出库 from (
    select checkoutlist.sparecode,sum(isnull(checkoutlist.amount,0)) 累计出库 from checkout join checkoutlist on checkout.objid=checkoutlist.bindparent 
    where checkoutlist.sparecode ='026100024W'and checkout.createdate between '2003-01-01'and '2003-05-31'
     group by checkoutlist.sparecode) a right join (
    select planinlist.sparecode,sum(isnull(planinlist.factamount,0)) 累计入库 from  planin join planinlist on planin.objid=planinlist.bindparent 
    where  planinlist.sparecode='026100024W' and planin.finishdate between '2003-01-01'and '2003-05-31'
    group by planinlist.sparecode) b
    on a.sparecode=b.sparecode
      

  5.   

    pengdali(大力 V2.0) 
    RIGHT join 好象有点问题
    如果本区间无累计入库 ,有累计出库 呢
      

  6.   

    TO: pengdali(大力 V2.0),caiyunxia(monkey) 
       
       非常感谢!
       问题暂时是解决了,可是我也不太确信计算的是否都准。由于我查的是某一时间段的累计,所以有不入库就出库的现象。:)
      

  7.   

    最好用产品表LEFT JOIN 入库 LEFT JOIN 出库
      

  8.   


    SELECT COALESCE(A.sparecode  ,B.sparecode ) sparecode  ,ISNULL(累计入库,0) 累计入库, ISNULL(累计出库,0) 累计出库
    FROM (select checkoutlist.sparecode,sum(isnull(checkoutlist.amount,0)) as '累计出库' from checkout inner join checkoutlist on checkout.objid=checkoutlist.bindparent 
    where checkoutlist.sparecode ='026100024W'and checkout.createdate between '2003-01-01'and '2003-05-31'
     group by checkoutlist.sparecode)A FULL JOIN (select planinlist.sparecode,sum(isnull(planinlist.factamount,0)) as '累计入库' from  planin inner join planinlist on planin.objid=planinlist.bindparent 
    where  planinlist.sparecode='026100024W' and planin.finishdate between '2003-01-01'and '2003-05-31'
    group by planinlist.sparecode) B ON A.sparecode  =B.sparecode
      

  9.   

    TO:  caiyunxia(monkey) 
       不行啊,你的执行报错呀.   假设我有一万个产品,那么是不是应该返回一万条呢?
      

  10.   

    错误如下: SqlDumpExceptionHandler: 进程 56 发生了严重的异常 c0000005 EXCEPTION_ACCESS_VIOLATION。SQL Server 将终止该进程。连接中断
      

  11.   

    SELECT COALESCE(A.sparecode ,B.sparecode ) sparecode ,ISNULL(累计入库,0) 累计入库, ISNULL(累计出库,0) 累计出库
    FROM (select checkoutlist.sparecode,sum(isnull(checkoutlist.amount,0)) as '累计出库' from checkout inner join checkoutlist on checkout.objid=checkoutlist.bindparent 
    where checkoutlist.sparecode ='026100024W'and checkout.createdate between '2003-01-01'and '2003-05-31'
     group by checkoutlist.sparecode)A FULL JOIN (select planinlist.sparecode,sum(isnull(planinlist.factamount,0)) as '累计入库' from  planin inner join planinlist on planin.objid=planinlist.bindparent 
    where  planinlist.sparecode='026100024W' and planin.finishdate between '2003-01-01'and '2003-05-31'
    group by planinlist.sparecode) B ON A.sparecode  =B.sparecode
    只出现本区间 有入库或有出库的记录
    我的输入法有问题,所有符号是全角,不能转换
    在试试
      

  12.   

    select a.sparecode,b.累计入库,a.累计出库 from 你的库存表 a 
    left join
     (
    select checkoutlist.sparecode,sum(isnull(checkoutlist.amount,0)) 累计出库 from checkout join checkoutlist on checkout.objid=checkoutlist.bindparent 
    where checkoutlist.sparecode ='026100024W'and checkout.createdate between '2003-01-01'and '2003-05-31'
     group by checkoutlist.sparecode
    ) b
    on a.sparecode=b.sparecode
    left join 
    (
    select planinlist.sparecode,sum(isnull(planinlist.factamount,0)) 累计入库 from  planin join planinlist on planin.objid=planinlist.bindparent 
    where  planinlist.sparecode='026100024W' and planin.finishdate between '2003-01-01'and '2003-05-31'
    group by planinlist.sparecode
    ) c
    on a.sparecode=c.sparecode
      

  13.   

    你的 你的库存表 有 sparecode 列吗?
      

  14.   

    select a.sparecode,a.累计出库,b.累计入库 from stored 
    left join
     (
    select checkoutlist.sparecode,sum(isnull(checkoutlist.amount,0)) as 累计出库 from checkout join checkoutlist on checkout.objid=checkoutlist.bindparent 
    where checkoutlist.sparecode ='026100024W'and checkout.createdate between '2003-01-01'and '2003-05-31'
     group by checkoutlist.sparecode
    ) a
    on a.sparecode=stored.sparecode
    left join 
    (
    select planinlist.sparecode,sum(isnull(planinlist.factamount,0))  as 累计入库 from  planin join planinlist on planin.objid=planinlist.bindparent 
    where  planinlist.sparecode='026100024W' and planin.finishdate between '2003-01-01'and '2003-05-31'
    group by planinlist.sparecode
    ) b
    on a.sparecode=b.sparecode
    我执行了.是返回stored 的所有条数了,但是为什么数据都是空的呀!
      

  15.   

    但是我一把条件 planinlist.sparecode='026100024W' 去掉就报错误:
    SqlDumpExceptionHandler: 进程 51 发生了严重的异常 c0000005 EXCEPTION_ACCESS_VIOLATION。SQL Server 将终止该进程。连接中断
      

  16.   

    select a.sparecode,isnull(a.累计出库,0),isnull(b.累计入库,0) from stored 
    left join
     (
    select checkoutlist.sparecode,sum(checkoutlist.amount) as 累计出库 from checkout join checkoutlist on checkout.objid=checkoutlist.bindparent 
    where checkoutlist.sparecode ='026100024W'and checkout.createdate between '2003-01-01'and '2003-05-31'
     group by checkoutlist.sparecode
    ) a
    on a.sparecode=stored.sparecode
    left join 
    (
    select planinlist.sparecode,sum(planinlist.factamount)  as 累计入库 from  planin join planinlist on planin.objid=planinlist.bindparent 
    where  planinlist.sparecode='026100024W' and planin.finishdate between '2003-01-01'and '2003-05-31'
    group by planinlist.sparecode
    ) b
    on a.sparecode=b.sparecode
    -----------------------------
    我执行了.是返回stored 的所有条数了,但是为什么数据都是空的呀!
    因为你在这个时间段没有这些产品
      

  17.   

    不是啊.那刚才的时候查出来还有数据呀.这时怎么一条都没有了.都是NULL
      

  18.   

    select a.sparecode,isnull(a.累计出库,0),isnull(b.累计入库,0) from stored 
    left join
     (
    select checkoutlist.sparecode,sum(checkoutlist.amount) as 累计出库 from checkout join checkoutlist on checkout.objid=checkoutlist.bindparent 
    where checkout.createdate between '2003-01-01'and '2003-05-31'
     group by checkoutlist.sparecode
    ) a
    on a.sparecode=stored.sparecode
    left join 
    (
    select planinlist.sparecode,sum(planinlist.factamount)  as 累计入库 from  planin join planinlist on planin.objid=planinlist.bindparent 
    where   planin.finishdate between '2003-01-01'and '2003-05-31'
    group by planinlist.sparecode
    ) b
    on a.sparecode=b.sparecode会没数据?
      

  19.   

    select a.sparecode,isnull(a.累计出库,0),isnull(b.累计入库,0) from stored a
    left join
     (
    select checkoutlist.sparecode,sum(checkoutlist.amount) as 累计出库 from checkout join checkoutlist on checkout.objid=checkoutlist.bindparent 
    where checkout.createdate between '2003-01-01'and '2003-05-31'
     group by checkoutlist.sparecode
    ) b
    on a.sparecode=b.sparecode
    left join 
    (
    select planinlist.sparecode,sum(planinlist.factamount)  as 累计入库 from  planin join planinlist on planin.objid=planinlist.bindparent 
    where   planin.finishdate between '2003-01-01'and '2003-05-31'
    group by planinlist.sparecode
    ) c
    on a.sparecode=c.sparecode你不要乱改,才发现被你改动了~~
      

  20.   


       a left join b
       a left join c 
    这样的结构
      

  21.   

    select a.sparecode,isnull(a.[累计出库],0),isnull(b.[累计入库],0) from stored a
    left join
     (
    select checkoutlist.sparecode,sum(checkoutlist.amount) [累计出库] from checkout join checkoutlist on checkout.objid=checkoutlist.bindparent 
    where checkout.createdate between cast('2003-01-01' as datetime) and cast('2003-05-31' as datetime)
     group by checkoutlist.sparecode
    ) b
    on a.sparecode=b.sparecode
    left join 
    (
    select planinlist.sparecode,sum(planinlist.factamount) [累计入库] from  planin join planinlist on planin.objid=planinlist.bindparent 
    where   planin.finishdate between cast('2003-01-01' as datetime) and cast('2003-05-31' as datetime)
    group by planinlist.sparecode
    ) c
    on a.sparecode=c.sparecode
      

  22.   

    select a.sparecode,isnull(b.[累计出库],0),isnull(c.[累计入库],0) from stored a
    left join
     (
    select checkoutlist.sparecode,sum(checkoutlist.amount) [累计出库] from checkout join checkoutlist on checkout.objid=checkoutlist.bindparent 
    where checkout.createdate between cast('2003-01-01' as datetime) and cast('2003-05-31' as datetime)
     group by checkoutlist.sparecode
    ) b
    on a.sparecode=b.sparecode
    left join 
    (
    select planinlist.sparecode,sum(planinlist.factamount) [累计入库] from  planin join planinlist on planin.objid=planinlist.bindparent 
    where   planin.finishdate between cast('2003-01-01' as datetime) and cast('2003-05-31' as datetime)
    group by planinlist.sparecode
    ) c
    on a.sparecode=c.sparecode
      

  23.   

    可以了呀.好高兴啊.:)))  大力真不亏是MVP 呀.真的好感激呀!!  吃饭没???晚饭我请了??
      

  24.   

        是啊 开心就好                祝 pengdali(大力 V2.0)  天天开心!永远开心!!