日期:2014-05-17  浏览次数:20420 次

求高手 一条 sql语句
我 有 三 个表 customs_CustomsDeclaration(报关),bus_BusinessDetail(业务),cost_FeeInfo(财务)

我想查询出 那票 报关单 没有 录入 费用 
如 

SELECT AdvanceNum,DeliveryListNum,Consignor,DeclareDate,Declare_Name,bbd.BusinessNum
                                 ,Flag_Customs  ,IsExists--表示 是否 存在费用 


(SELECT AdvanceNum,DeliveryListNum,Consignor,DeclareDate,Declare_Name,bbd.BusinessNum
                                 ,Flag_Customs                   
                        from  customs_CustomsDeclaration as ccd ,bus_BusinessDetail as bbd  

 where bbd.BusinessNum = ccd.BusinessNum and bbd.BusinessNum in  
                   (  select BusinessNum  from (select b.BusinessNum,count(*) as totalNum,  
                   SUM(case when f.Flag_ManagerCheckCost= '0' then 1 else 0 end) 
                   as checkNum 
           from bus_BusinessDetail b inner join cost_FeeInfo f on b.BusinessNum=f.BusinessNum
                    group by b.BusinessNum ) t 
                    where totalNum>0 and checkNum > 0 
                   UNION 
                  SELECT  BusinessNum  FROM    customs_CustomsDeclaration
            WHERE   BusinessNum NOT IN ( SELECT BusinessNum  FROM   cost_FeeInfo )
           and InputDate BETWEEN '2013-10-23 00:00:00' AND '2013-10-30 00:00:00'  group by BusinessNum ) 

  AND ccd.InputDate BETWEEN '2013-10-23 00:00:00' AND '2013-10-30 00:00:00' AND ccd.IsAvailable = '1' 

 group by AdvanceNum,DeliveryListNum,Consignor,DeclareDate,Declare_Name,bbd.BusinessNum,Flag_Customs) 

求高手 解答 ,我 写了 好久都没 写出来