Wednesday, 19 June 2013

DISPATCH QUERY FOR BRANCH RETURN (ORDER MANAGEMENT & INVENTORY)

Select   Typ, Machine1, Machine2, Machine3, Sum (Mach1) Mach1,
                         Sum (Mach2) Mach2, Sum (Mach3) Mach3
                    From (Select   Typ, Machine1, Machine2, Machine3,
                                   Sum (Mc1) Mach1, Sum (Mc2) Mach2,
                                   Sum (Mc3) Mach3
                              From (Select Ott.Name, 'Branch Return' Typ,
                                           0 Machine1, 0 Machine2, 0 Machine3,
                                           Abs
                                              ((Case
                                                   When Substr (Msi.Segment1,
                                                                2,
                                                                1
                                                               ) = '1'
                                                      Then (Nvl
                                                               (Rt.Primary_Quantity,
                                                                0
                                                               )
                                                           )
                                                   Else 0
                                                End
                                               )
                                              ) Mc1,
                                           Abs
                                              ((Case
                                                   When Substr (Msi.Segment1,
                                                                2,
                                                                1
                                                               ) = '2'
                                                      Then (Nvl
                                                               (Rt.Primary_Quantity,
                                                                0
                                                               )
                                                           )
                                                   Else 0
                                                End
                                               )
                                              ) Mc2,
                                           Abs
                                              ((Case
                                                   When Substr (Msi.Segment1,
                                                                2,
                                                                1
                                                               ) = '3'
                                                      Then (Nvl
                                                               (Rt.Primary_Quantity,
                                                                0
                                                               )
                                                           )
                                                   Else 0
                                                End
                                               )
                                              ) Mc3
                                      From Apps.Oe_Order_Headers_All Oh,
                                           Apps.Po_Requisition_Headers_All Prh,
                                           Apps.Po_Requisition_Lines_All Prl,
                                           Apps.Po_Req_Distributions_All Prd,
                                           Apps.Rcv_Transactions Rt,
                                           Apps.Rcv_Shipment_Headers Rsh,
                                           Apps.Rcv_Shipment_Lines Rsl,
                                           Apps.Mtl_System_Items_B Msi,
                                           Apps.Mtl_Item_Categories Mic,
                                           Apps.Mtl_Categories Mc,
                                           Apps.Oe_Transaction_Types_Tl Ott
                                     Where
                                           --Oh.Order_Number = 32012003611
                                           Oh.Orig_Sys_Document_Ref =
                                                                  Prh.Segment1
                                       And Prh.Requisition_Header_Id =
                                                     Prl.Requisition_Header_Id
--And Prd.Requistion_Header_Id = Prl.Requisition_Header_Id
                                       And Prd.Requisition_Line_Id =
                                                       Prl.Requisition_Line_Id
                                       And Rt.Req_Distribution_Id =
                                                           Prd.Distribution_Id
                                       And Rsh.Shipment_Header_Id =
                                                         Rt.Shipment_Header_Id
                                       And Rt.Transaction_Type = 'DELIVER'
                                       And Rsl.Shipment_Line_Id =
                                                           Rt.Shipment_Line_Id
                                       And Msi.Organization_Id =
                                                            Rt.Organization_Id
                                       And Msi.Inventory_Item_Id = Rsl.Item_Id
                                       And Mic.Inventory_Item_Id = Rsl.Item_Id
                                       And Mic.Organization_Id =
                                                            Rt.Organization_Id
                                       And Mic.Category_Set_Id = 5
                                       And Mic.Category_Id = Mc.Category_Id(+)
                                       And Ott.Transaction_Type_Id =
                                                              Oh.Order_Type_Id
                                       And Mc.Segment1 Not In
                                                          ('NB', 'NA', 'COM')
                                       And Substr (Msi.Segment1, 2, 1) In
                                                              ('1', '2', '3')
--And Upper(Flv.Lookup_Code) = Upper(Ott.Name)
                                       And Ott.Name Like 'Stock Transfer'
--                                       And Mc.Segment1 =
--                                                Nvl (:P_Category, Mc.Segment1)
                                       And Rsh.Creation_Date BETWEEN   TRUNC
                                       (TO_DATE
                                           (   '01'
                                            || '-'
                                            || 'APR'
                                            || '-'
                                            || TRUNC
                                                  (TO_NUMBER
                                                          (TO_CHAR
                                                                  (t_date,
                                                                   'YYYY'
                                                                  )
                                                          )
                                                  )
                                           ),
                                        'dd'
                                       )
                                  + (6 / 24)   --to_date (01, 'dd') + (6 / 24)
                              AND TRUNC (t_date, 'dd') + (6 / 24) + 1
                                       And Prl.Destination_Organization_Id =
                                                                       Orgn_Id)
                          Group By Typ, Machine1, Machine2, Machine3)
                Group By Typ, Machine1, Machine2, Machine3
                Union All
                Select 'Branch Return' Typ, 0 Machine1, 0 Machine2,
                       0 Machine3, 0 Mach1, 0 Mach2, 0 Mach3
                  From Dual)

No comments:

Post a Comment