Skip to Content

Duplicates in Query

Created a query based on what was asked for. It has duplicates I believe because of both warehouses. My Boss wants the items to be on the same line wants the quanities to be the sum of both warehouses. Is this possible ?

SELECT T2.ItemCode, T3.ItmsGrpNam,T2.ItemName, sum(case when T0.[Status] ='R' then (isnull(T1.PlannedQty,0)) else 0 End) as 'Released' ,T2.OnHand , sum(case when T0.[Status] ='P' then (isnull(T1.PlannedQty,0)) else 0 End) as 'Planned' , T2.OnHand - sum(case when T0.[Status] ='P' then (isnull(T1.PlannedQty,0)) else 0 End) AS 'Stock for Planned' ,T2.OnOrder AS 'On PO' ,(T2.OnHand - T2.IsCommited + T2.OnOrder) AS 'Available' ,T4.MinStock AS 'Minimum Qty' ,case when (T4.OnHand - T4.IsCommited+ T4.OnOrder ) < T4.MinStock Then 'Order' else ' ' End AS 'Status' FROM OWOR T0 INNER JOIN WOR1 T1 ON T0.[DocEntry] = T1.[DocEntry] LEFT OUTER JOIN OITM T2 ON T1.ItemCode=T2.ItemCode LEFT OUTER JOIN OITB T3 ON T2.ItmsGrpCod=T3.ItmsGrpCod LEFT OUTER JOIN OITW T4 ON T4.[ItemCode] = T1.[ItemCode] WHERE T0.[Status] <>'L' and T0.[Status]<>'C' and T1.ItemCode Like '1%' and T4.WhsCode in ('02') Group by T2.ItemCode, T3.ItmsGrpNam,T2.ItemName,T1.PlannedQty, T2.OnHand,T2.IsCommited,T2.OnOrder,T4.OnHand,T4.IsCommited,T4.OnOrder,T4.MinStock

querycapture.png (131.0 kB)
Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

1 Answer

  • Best Answer
    Posted on Jul 22, 2020 at 05:37 AM

    Hi Anthony,

    Please give this a try:

    SELECT T2.ItemCode, T3.ItmsGrpNam,T2.ItemName
    , sum(case 
           when T0.[Status] ='R' then (isnull(T1.PlannedQty,0)) 
           else 0 
          End) as 'Released' 
    ,sum(T2.OnHand) as [OnHand]
    ,sum(case 
          when T0.[Status] ='P' then (isnull(T1.PlannedQty,0)) 
          else 0 
         End) as 'Planned' 
    , sum(T2.OnHand - case 
                       when T0.[Status] ='P' then (isnull(T1.PlannedQty,0)) 
                       else 0 
                      End) AS 'Stock for Planned' 
    ,sum(T2.OnOrder) AS 'On PO' 
    ,sum(T2.OnHand - T2.IsCommited + T2.OnOrder) AS 'Available' 
    ,avg(T4.MinStock) AS 'Minimum Qty' 
    ,case 
      when sum(T4.OnHand - T4.IsCommited+ T4.OnOrder ) < avg(T4.MinStock) Then 'Order' 
      else ''
     End AS 'Status' 
    FROM OWOR T0 
         INNER JOIN WOR1 T1 ON T0.[DocEntry] = T1.[DocEntry] 
         LEFT OUTER JOIN OITM T2 ON T1.ItemCode=T2.ItemCode 
         LEFT OUTER JOIN OITB T3 ON T2.ItmsGrpCod=T3.ItmsGrpCod 
         LEFT OUTER JOIN OITW T4 ON T4.[ItemCode] = T1.[ItemCode] 
    WHERE T0.[Status] <>'L' 
    and T0.[Status]<>'C' 
    and T1.ItemCode Like '1%' 
    and T4.WhsCode in ('02') 
    Group by T2.ItemCode, T3.ItmsGrpNam,T2.ItemName
    

    Regards,

    Johan

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.