把你的语句和你的表结构和数据贴出来

解决方案 »

  1.   

    select 0--X1.运单号,X1.后缀,始发站,目的站,在站件数,总件数,特货码,所在容器,位置,容器类别,装载货物件数,重量
    from
    (
    ----------------------------------begin---------------------------------------------------
    select
    T1.AWB_Prefix + '-' + T1.AWB_No 运单号,
    T1.AWB_Postfix 后缀,
    T5.Dep 始发站,
    T5.Dest 目的站,
    sum(T1.Piece) 在站件数,
    sum(T4.Piece) 总件数,
    T4.SpeCode 特货码
    from
    TBL_CargoBill T1
    inner join
    TBL_Cargo_Container T2
    on T1.Rec_ID = T2.Rec_ID
    inner join
    TBL_Container T3
    on T3.Container_Code = T2.Container_Code
    left outer join
    TBL_Cargo_PW T4
    on T1.AWB_Prefix = T4.AWB_Prefix and T1.AWB_No = T4.AWB_No and T1.AWB_Postfix = T4.AWB_Postfix
    left outer join
    TBL_AWB_Basic T5
    on T1.AWB_Prefix = T5.AWB_Prefix and T1.AWB_No = T5.AWB_No and T1.AWB_Postfix = T5.AWB_Postfix
    where
        T1.CargoArr_Flag = 0
    and T1.Finish_Flag = 0
    and T3.Location_Code != 'SKY'
    and T3.Location_Code != 'AWAY'
    group by
    T1.AWB_Prefix,
    T1.AWB_No,
    T1.AWB_Postfix,
    T5.Dep,
    T5.Dest,
    T4.SpeCode
    --------------------------------------end---------------------------------------------
    ) X1
    left outer join
    (
    ------------------------------------------------------------------------------------
    ------------------------------------begin--------------------------------------------
    select
    T1.AWB_Prefix + '-' + T1.AWB_No 运单号,
    T1.AWB_Postfix 后缀,
    T1.Rec_ID 记录号,
    T3.Container_Code 所在容器,
    T3.Location_Code 位置,
    T3.UCBFlag 容器类别,
    sum(T2.Piece)  - isnull((select sum(Piece)  From TBL_Cargo_Container  where Rec_ID = T1.Rec_ID  and Container_Code = 'AWAY'  group by Rec_ID), 0) 装载货物件数,
    sum(T2.Weight)  - isnull((select sum(Weight) From TBL_Cargo_Container where Rec_ID = T1.Rec_ID  and Container_Code = 'AWAY'  group by Rec_ID), 0) 重量
    from
    TBL_CargoBill T1
    inner join
    TBL_Cargo_Container T2
    on T1.Rec_ID = T2.Rec_ID
    inner join
    TBL_Container T3
    on T3.Container_Code = T2.Container_Code
    left outer join
    TBL_Cargo_PW T4
    on T1.AWB_Prefix = T4.AWB_Prefix and T1.AWB_No = T4.AWB_No and T1.AWB_Postfix = T4.AWB_Postfix
    left outer join
    TBL_AWB_Basic T5
    on T1.AWB_Prefix = T5.AWB_Prefix and T1.AWB_No = T5.AWB_No and T1.AWB_Postfix = T5.AWB_Postfix
    where
        T1.CargoArr_Flag = 0
    and T1.Finish_Flag = 0
    and T3.Location_Code != 'SKY'
    and T3.Location_Code != 'AWAY'
    group by
    T1.AWB_Prefix,
    T1.AWB_No,
    T1.AWB_Postfix,
    T1.Rec_ID,
    T3.Container_Code,
    T3.Location_Code,
    T3.UCBFlag
    --------------------------------------end----------------------------------------------
    --------------------------------------------------------------------------------
    ) X2
    on X1.运单号 = X2.运单号 and X1.后缀 = X2.后缀
    -----------begin-------------
    -----------end-------------

    -----------------------------
    -----------begin-------------
    -----------end-------------
    ------------------------------
    之间的语句都可以单独运行,可和在一起是就会显示出错,