SQL Query - SUM and JOIN multiple tables but return missing record

glennyboy 121 Reputation points
2020-09-27T09:17:51.367+00:00

SQL Query - SUM and JOIN multiple tables but return missing record;

Good day!

here's the sample data link : sample data table.xlsx

  • Barcodes Table
  • Item Ledger Entry Table
  • Trans_ Sales Entry Table
  • Transfer Line Table
  • Query result

Need help!

  • when i JOIN and SUM the Trans_ Sales Entry Table and Transfer Line Table , but the query result return missing record

28460-image.png

Thank you!

here's my sql query code;

;WITH Barcodes AS  
(  
SELECT  
 [BBI$Barcodes].[Item No_]  
 ,[BBI$Barcodes].[Description]  
 ,[BBI$Barcodes].[Variant Code]  
FROM [BBI$Barcodes]  
),  
ILEtransfer AS  
(  
SELECT  
 [BBI$Item Ledger Entry].[Entry Type]  
 ,[BBI$Item Ledger Entry].[Location Code] AS [Location]  
 ,[BBI$Item Ledger Entry].[Item No_]  
 ,MAX([BBI$Item Ledger Entry].[Description]) AS [Description]  
 ,[BBI$Item Ledger Entry].[Variant Code]  
 ,SUM([BBI$Item Ledger Entry].[Quantity]) AS [PDel]  
  
FROM [BBI$Item Ledger Entry]  
WHERE  
 [BBI$Item Ledger Entry].[Location Code]='HPGW'  
 AND [BBI$Item Ledger Entry].[Entry Type] = '4'  
GROUP BY  
 [BBI$Item Ledger Entry].[Location Code]  
 ,[BBI$Item Ledger Entry].[Entry Type]  
 ,[BBI$Item Ledger Entry].[Item No_]  
 ,[BBI$Item Ledger Entry].[Variant Code]  
),  
ILEmsales AS  
(  
SELECT  
 [BBI$Item Ledger Entry].[Entry Type]  
 ,[BBI$Item Ledger Entry].[Location Code] AS [Location]  
 ,[BBI$Item Ledger Entry].[Item No_]  
 ,MAX([BBI$Item Ledger Entry].[Description]) AS [Description]  
 ,[BBI$Item Ledger Entry].[Variant Code]  
 ,SUM([BBI$Item Ledger Entry].[Quantity]) AS [MSales]  
  
FROM [BBI$Item Ledger Entry]  
WHERE  
 [BBI$Item Ledger Entry].[Location Code]='HPGW'  
 AND [BBI$Item Ledger Entry].[Entry Type] = '1'  
 AND [BBI$Item Ledger Entry].[Document No_] NOT LIKE 'HP%'  
GROUP BY  
 [BBI$Item Ledger Entry].[Location Code]  
 ,[BBI$Item Ledger Entry].[Entry Type]  
 ,[BBI$Item Ledger Entry].[Item No_]  
 ,[BBI$Item Ledger Entry].[Variant Code]  
),  
ILEpadj AS  
(  
SELECT  
 [BBI$Item Ledger Entry].[Entry Type]  
 ,[BBI$Item Ledger Entry].[Location Code] AS [Location]  
 ,[BBI$Item Ledger Entry].[Item No_]  
 ,MAX([BBI$Item Ledger Entry].[Description]) AS [Description]  
 ,[BBI$Item Ledger Entry].[Variant Code]  
 ,SUM([BBI$Item Ledger Entry].[Quantity]) AS [PAdj]  
  
FROM [BBI$Item Ledger Entry]  
WHERE  
 [BBI$Item Ledger Entry].[Location Code]='HPGW'  
 AND [BBI$Item Ledger Entry].[Entry Type] = '2'  
GROUP BY  
 [BBI$Item Ledger Entry].[Location Code]  
 ,[BBI$Item Ledger Entry].[Entry Type]  
 ,[BBI$Item Ledger Entry].[Item No_]  
 ,[BBI$Item Ledger Entry].[Variant Code]  
),  
ILEnadj AS  
(  
SELECT  
 [BBI$Item Ledger Entry].[Entry Type]  
 ,[BBI$Item Ledger Entry].[Location Code] AS [Location]  
 ,[BBI$Item Ledger Entry].[Item No_]  
 ,MAX([BBI$Item Ledger Entry].[Description]) AS [Description]  
 ,[BBI$Item Ledger Entry].[Variant Code]  
 ,SUM([BBI$Item Ledger Entry].[Quantity]) AS [NAdj]  
  
FROM [BBI$Item Ledger Entry]  
WHERE  
 [BBI$Item Ledger Entry].[Location Code]='HPGW'  
 AND [BBI$Item Ledger Entry].[Entry Type] = '3'  
GROUP BY  
 [BBI$Item Ledger Entry].[Location Code]  
 ,[BBI$Item Ledger Entry].[Entry Type]  
 ,[BBI$Item Ledger Entry].[Item No_]  
 ,[BBI$Item Ledger Entry].[Variant Code]  
),  
Transfeline  AS  
(  
SELECT  
    [BBI$Transfer Line].[Transfer-to Code]  
      ,[BBI$Transfer Line].[Item No_]  
      ,MAX([BBI$Transfer Line].[Description]) AS [Description]  
      ,MAX([BBI$Transfer Line].[Description 2]) AS [Description 2]  
      ,[BBI$Transfer Line].[Variant Code]  
      ,SUM([BBI$Transfer Line].[Quantity]) AS [UDel]  
  FROM [BBI$Transfer Line]  
  WHERE  
  [BBI$Transfer Line].[Transfer-to Code] = 'HPGW'  
  GROUP BY  
      [BBI$Transfer Line].[Transfer-to Code]  
      ,[BBI$Transfer Line].[Item No_]  
      ,[BBI$Transfer Line].[Variant Code]  
),  
TSEpsales AS  
(  
SELECT  
 [BBI$Trans_ Sales Entry].[Store No_]  
 ,[BBI$Trans_ Sales Entry].[Item No_]  
 ,[BBI$Trans_ Sales Entry].[Variant Code]  
 ,SUM([BBI$Trans_ Sales Entry].[Quantity]) AS [PSales]  
FROM [BBI$Trans_ Sales Entry]  
WHERE  
 [BBI$Trans_ Sales Entry].[Store No_]='HPGW'  
 AND [BBI$Trans_ Sales Entry].[Transaction No_] NOT IN (5271,5272,5273,5278,5279,5280,5281,5282,5283,5284,5285,5286,8530,8531,8532,8533,8534,8535,8536,8537,13133,13849)  
GROUP BY  
 [BBI$Trans_ Sales Entry].[Store No_]  
 ,[BBI$Trans_ Sales Entry].[Item No_]  
 ,[BBI$Trans_ Sales Entry].[Variant Code]  
)  
  
SELECT DISTINCT  
 BAR.[Item No_] AS [Item No_]  
 ,BAR.[Description] AS [Description]  
 ,BAR.[Variant Code] AS [Variant Code]  
 ,ISNULL(ILETR.[PDel],0) AS [PDel]  
 ,ISNULL(ILEMS.[MSales],0) AS [MSales]  
 ,ISNULL(ILEPA.[PAdj],0) AS [PAdj]  
 ,ISNULL(ILENA.[NAdj],0) AS [NAdj]  
 ,ISNULL(TL.[UDel],0) AS [UDel]  
 ,ISNULL(TSEPS.[PSales],0) AS [PSales]  
  ,ISNULL(ILETR.[PDel],0)+ISNULL(ILEPA.[PAdj],0)+ISNULL(TL.[UDel],0)+ISNULL(ILEMS.[MSales],0)+ISNULL(ILENA.[NAdj],0)+ISNULL(TSEPS.[PSales],0) AS Total  
  
FROM [BBI$Barcodes] BAR  
  
LEFT JOIN [ILEtransfer] ILETR  
ON ILETR.[Item No_]=BAR.[Item No_]  
AND ILETR.[Variant Code]=BAR.[Variant Code]  
  
LEFT JOIN [ILEmsales] ILEMS  
ON ILEMS.[Item No_]=BAR.[Item No_]  
AND ILEMS.[Variant Code]=BAR.[Variant Code]  
  
LEFT JOIN [ILEpadj] ILEPA  
ON ILEPA.[Item No_]=BAR.[Item No_]  
AND ILEPA.[Variant Code]=BAR.[Variant Code]  
  
LEFT JOIN [ILEnadj] ILENA  
ON ILENA.[Item No_]=BAR.[Item No_]  
AND ILENA.[Variant Code]=BAR.[Variant Code]  
  
FULL JOIN [Transfeline] TL  
ON TL.[Item No_]=BAR.[Item No_]  
AND TL.[Variant Code]=BAR.[Variant Code]  
  
FULL JOIN [TSEpsales] TSEPS  
ON TSEPS.[Item No_]=BAR.[Item No_]  
AND TSEPS.[Variant Code]=BAR.[Variant Code]  
  
WHERE  
ILETR.[Location]='HPGW'  
AND ILETR.[Entry Type]='4'  
OR ILEMS.[Entry Type]='1'  
OR ILEPA.[Entry Type]='2'  
OR ILENA.[Entry Type]='3'  

if i run this query alone [BBI$Trans_ Sales Entry], the return result SUM quantity are correct below.

but if i run the whole query with JOIN, the return result SUM quantity from [BBI$Trans_ Sales Entry] have missing record

     SELECT  
         [BBI$Trans_ Sales Entry].[Store No_]  
         ,[BBI$Trans_ Sales Entry].[Item No_]  
         ,[BBI$Trans_ Sales Entry].[Variant Code]  
         ,SUM([BBI$Trans_ Sales Entry].[Quantity]) AS [PSales]  
     FROM [BBI$Trans_ Sales Entry]  
     WHERE  
         [BBI$Trans_ Sales Entry].[Store No_]='HPGW'  
         AND [BBI$Trans_ Sales Entry].[Transaction No_] NOT IN (5271,5272,5273,5278,5279,5280,5281,5282,5283,5284,5285,5286,8530,8531,8532,8533,8534,8535,8536,8537,13133,13849)      
     GROUP BY  
         [BBI$Trans_ Sales Entry].[Store No_]  
         ,[BBI$Trans_ Sales Entry].[Item No_]  
         ,[BBI$Trans_ Sales Entry].[Variant Code]  
Developer technologies | Transact-SQL
Developer technologies | Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
0 comments No comments
{count} votes

Answer accepted by question author
  1. MelissaMa-msft 24,246 Reputation points Moderator
    2020-09-29T09:18:41.127+00:00

    Hi @glennyboy ,

    btw, i dont see in your query the Item table where to JOIN

    TSEpsales AS  
       (  
     SELECT  
               [BBI$Trans_ Sales Entry].[Store No_]  
               ,[BBI$Trans_ Sales Entry].[Item No_]  
               ,[BBI$Trans_ Sales Entry].[Variant Code]  
        ,[BBI$Item].[Description]  
               ,SUM([BBI$Trans_ Sales Entry].[Quantity]) AS [PSales]  
           FROM [BBI$Trans_ Sales Entry]  
        LEFT JOIN BBI$Item  
        ON CONVERT(NVARCHAR(255),BBI$Item.No_)=[BBI$Trans_ Sales Entry].[Item No_]  
           WHERE  
               [BBI$Trans_ Sales Entry].[Store No_]='HPGW'  
               AND [BBI$Trans_ Sales Entry].[Transaction No_] NOT IN (5271,5272,5273,5278,5279,5280,5281,5282,5283,5284,5285,5286,8530,8531,8532,8533,8534,8535,8536,8537,13133,13849)      
           GROUP BY  
               [BBI$Trans_ Sales Entry].[Store No_]  
               ,[BBI$Trans_ Sales Entry].[Item No_]  
               ,[BBI$Trans_ Sales Entry].[Variant Code]  
         ,[BBI$Item].[Description]  
       )  
    

    What is DDL of your item table? I named it as BBI$Item from my side.

    Msg 4104, Level 16, State 1, Line 178
    The multi-part identifier "A.Item No_" could not be bound.
    Msg 4104, Level 16, State 1, Line 179
    The multi-part identifier "A.Variant Code" could not be bound.

    This part is working from my side. Please provide your error snapshot(final part of query ,output and error message) if possible.

    Maybe:

       select isnull(a.[Item No_],TSEPS.[Item No_])[Item No_] ,  
       isnull(a.[Description],TSEPS.[Description]) [Description],  
       isnull(a.[Variant Code],TSEPS.[Variant Code]) [Variant Code],  
       isnull(a.[PDel],0) [PDel],  
       isnull(a.[MSales],0) [MSales],  
       isnull(a.[PAdj],0) [PAdj],  
       isnull(a.[NAdj],0) [NAdj],  
       isnull(TL.[UDel],0) [UDel],  
       ISNULL(TSEPS.[PSales],0) AS [PSales],  
       ISNULL(a.[PDel],0)+ISNULL(a.[PAdj],0)+ISNULL(TL.[UDel],0)+ISNULL(a.[MSales],0)+ISNULL(a.[NAdj],0)+ISNULL(TSEPS.[PSales],0) AS Total  
       from total a   
      FULL JOIN [TSEpsales] TSEPS  
       ON TSEPS.[Item No_]=a.[Item No_]  
       AND TSEPS.[Variant Code]=a.[Variant Code]  
       FULL JOIN [Transfeline] TL  
       ON TL.[Item No_]=a.[Item No_]  
       AND TL.[Variant Code]=a.[Variant Code]  
    

    Best regards
    Melissa


    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

8 additional answers

Sort by: Most helpful
  1. Erland Sommarskog 128.7K Reputation points MVP Volunteer Moderator
    2020-09-27T10:39:59.167+00:00

    I did not look into this in detail. However, I note that you have:

     LEFT JOIN [ILEtransfer] ILETR
     ON ILETR.[Item No_]=BAR.[Item No_]
     AND ILETR.[Variant Code]=BAR.[Variant Code]
    ...
     WHERE
     ILETR.[Location]='HPGW'
     AND ILETR.[Entry Type]='4'
     OR ILEMS.[Entry Type]='1'
     OR ILEPA.[Entry Type]='2'
     OR ILENA.[Entry Type]='3'
    

    The conditions on ILETR in the WHERE clause, effectively transform that left join to an inner join, and likewise the WHERE clause transforms the two full joins to left joins. Possibly, these conditions should be moved to the ON clause for the left join. If an outer join was what you intended in the first place. Maybe you actually wanted an inner join? But even in that case you have to be careful since you have those full outer joins. (Which, again, I don't know if you really need.)

    Also, the condition in the WHERE clause looks suspicious. As you have written it, you are saying "all entry types 1, 2 and 3. And entry type 4, but only if the location is HPGW". Is that what you want?


  2. MelissaMa-msft 24,246 Reputation points Moderator
    2020-09-28T05:16:02.623+00:00

    Hi @glennyboy ,

    I found that there was no data of Item No_ is '27019 000' or '271-3878' in Transfer Line Table while there were some data in Trans_ Sales Entry Table after checking your sample data excel.

    In your query, both of tables are join with Barcodes table using Item No_.

    Then of course there will be no data if you use join.

    Is there any possible that you could remove the join with Transfer Line Table or use exists in your where condition?

    Best regards
    Melissa


    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  3. MelissaMa-msft 24,246 Reputation points Moderator
    2020-09-29T07:38:19.167+00:00

    Hi @glennyboy ,

    BBI$Item.No_ was defined as float in my side when I imported it from excel. So I have to convert the date type of BBI$Item.No_ in order to have the same data type with Item No_.

    You could change this part if necessary from your side.

    Please find the updated query from below:

    ;WITH Barcodes AS  
      (  
      SELECT  
       [BBI$Barcodes].[Item No_]  
       ,[BBI$Barcodes].[Description]  
       ,[BBI$Barcodes].[Variant Code]  
      FROM [BBI$Barcodes]  
      ),  
      ILEtransfer AS  
      (  
      SELECT  
       [BBI$Item Ledger Entry].[Entry Type]  
       ,[BBI$Item Ledger Entry].[Location Code] AS [Location]  
       ,[BBI$Item Ledger Entry].[Item No_]  
       ,MAX([BBI$Item Ledger Entry].[Description]) AS [Description]  
       ,[BBI$Item Ledger Entry].[Variant Code]  
       ,SUM([BBI$Item Ledger Entry].[Quantity]) AS [PDel]  
              
      FROM [BBI$Item Ledger Entry]  
      WHERE  
       [BBI$Item Ledger Entry].[Location Code]='HPGW'  
       AND [BBI$Item Ledger Entry].[Entry Type] = '4'  
      GROUP BY  
       [BBI$Item Ledger Entry].[Location Code]  
       ,[BBI$Item Ledger Entry].[Entry Type]  
       ,[BBI$Item Ledger Entry].[Item No_]  
       ,[BBI$Item Ledger Entry].[Variant Code]  
      ),  
      ILEmsales AS  
      (  
      SELECT  
       [BBI$Item Ledger Entry].[Entry Type]  
       ,[BBI$Item Ledger Entry].[Location Code] AS [Location]  
       ,[BBI$Item Ledger Entry].[Item No_]  
       ,MAX([BBI$Item Ledger Entry].[Description]) AS [Description]  
       ,[BBI$Item Ledger Entry].[Variant Code]  
       ,SUM([BBI$Item Ledger Entry].[Quantity]) AS [MSales]  
              
      FROM [BBI$Item Ledger Entry]  
      WHERE  
       [BBI$Item Ledger Entry].[Location Code]='HPGW'  
       AND [BBI$Item Ledger Entry].[Entry Type] = '1'  
       AND [BBI$Item Ledger Entry].[Document No_] NOT LIKE 'HP%'  
      GROUP BY  
       [BBI$Item Ledger Entry].[Location Code]  
       ,[BBI$Item Ledger Entry].[Entry Type]  
       ,[BBI$Item Ledger Entry].[Item No_]  
       ,[BBI$Item Ledger Entry].[Variant Code]  
      ),  
      ILEpadj AS  
      (  
      SELECT  
       [BBI$Item Ledger Entry].[Entry Type]  
       ,[BBI$Item Ledger Entry].[Location Code] AS [Location]  
       ,[BBI$Item Ledger Entry].[Item No_]  
       ,MAX([BBI$Item Ledger Entry].[Description]) AS [Description]  
       ,[BBI$Item Ledger Entry].[Variant Code]  
       ,SUM([BBI$Item Ledger Entry].[Quantity]) AS [PAdj]  
              
      FROM [BBI$Item Ledger Entry]  
      WHERE  
       [BBI$Item Ledger Entry].[Location Code]='HPGW'  
       AND [BBI$Item Ledger Entry].[Entry Type] = '2'  
      GROUP BY  
       [BBI$Item Ledger Entry].[Location Code]  
       ,[BBI$Item Ledger Entry].[Entry Type]  
       ,[BBI$Item Ledger Entry].[Item No_]  
       ,[BBI$Item Ledger Entry].[Variant Code]  
      ),  
      ILEnadj AS  
      (  
      SELECT  
       [BBI$Item Ledger Entry].[Entry Type]  
       ,[BBI$Item Ledger Entry].[Location Code] AS [Location]  
       ,[BBI$Item Ledger Entry].[Item No_]  
       ,MAX([BBI$Item Ledger Entry].[Description]) AS [Description]  
       ,[BBI$Item Ledger Entry].[Variant Code]  
       ,SUM([BBI$Item Ledger Entry].[Quantity]) AS [NAdj]  
              
      FROM [BBI$Item Ledger Entry]  
      WHERE  
       [BBI$Item Ledger Entry].[Location Code]='HPGW'  
       AND [BBI$Item Ledger Entry].[Entry Type] = '3'  
      GROUP BY  
       [BBI$Item Ledger Entry].[Location Code]  
       ,[BBI$Item Ledger Entry].[Entry Type]  
       ,[BBI$Item Ledger Entry].[Item No_]  
       ,[BBI$Item Ledger Entry].[Variant Code]  
      ),  
      Transfeline  AS  
      (  
      SELECT  
          [BBI$Transfer Line].[Transfer-to Code]  
            ,[BBI$Transfer Line].[Item No_]  
            ,MAX([BBI$Transfer Line].[Description]) AS [Description]  
            ,MAX([BBI$Transfer Line].[Description 2]) AS [Description 2]  
            ,[BBI$Transfer Line].[Variant Code]  
            ,SUM([BBI$Transfer Line].[Quantity]) AS [UDel]  
        FROM [BBI$Transfer Line]  
        WHERE  
        [BBI$Transfer Line].[Transfer-to Code] = 'HPGW'  
        GROUP BY  
            [BBI$Transfer Line].[Transfer-to Code]  
            ,[BBI$Transfer Line].[Item No_]  
            ,[BBI$Transfer Line].[Variant Code]  
      ),  
      TSEpsales AS  
      (  
    SELECT  
              [BBI$Trans_ Sales Entry].[Store No_]  
              ,[BBI$Trans_ Sales Entry].[Item No_]  
              ,[BBI$Trans_ Sales Entry].[Variant Code]  
       ,[BBI$Item].[Description]  
              ,SUM([BBI$Trans_ Sales Entry].[Quantity]) AS [PSales]  
          FROM [BBI$Trans_ Sales Entry]  
       LEFT JOIN BBI$Item  
       ON CONVERT(NVARCHAR(255),BBI$Item.No_)=[BBI$Trans_ Sales Entry].[Item No_]  
          WHERE  
              [BBI$Trans_ Sales Entry].[Store No_]='HPGW'  
              AND [BBI$Trans_ Sales Entry].[Transaction No_] NOT IN (5271,5272,5273,5278,5279,5280,5281,5282,5283,5284,5285,5286,8530,8531,8532,8533,8534,8535,8536,8537,13133,13849)      
          GROUP BY  
              [BBI$Trans_ Sales Entry].[Store No_]  
              ,[BBI$Trans_ Sales Entry].[Item No_]  
              ,[BBI$Trans_ Sales Entry].[Variant Code]  
        ,[BBI$Item].[Description]  
      )  
      ,total as (     
      SELECT DISTINCT  
       BAR.[Item No_] AS [Item No_]  
       ,BAR.[Description] AS [Description]  
       ,BAR.[Variant Code] AS [Variant Code]  
       ,ISNULL(ILETR.[PDel],0) AS [PDel]  
       ,ISNULL(ILEMS.[MSales],0) AS [MSales]  
       ,ISNULL(ILEPA.[PAdj],0) AS [PAdj]  
       ,ISNULL(ILENA.[NAdj],0) AS [NAdj]  
        ,ISNULL(ILETR.[PDel],0)+ISNULL(ILEPA.[PAdj],0)+ISNULL(ILEMS.[MSales],0)+ISNULL(ILENA.[NAdj],0) AS Total  
              
      FROM [BBI$Barcodes] BAR  
           
      LEFT JOIN [ILEtransfer] ILETR  
      ON ILETR.[Item No_]=BAR.[Item No_]  
      AND ILETR.[Variant Code]=BAR.[Variant Code]  
              
      LEFT JOIN [ILEmsales] ILEMS  
      ON ILEMS.[Item No_]=BAR.[Item No_]  
      AND ILEMS.[Variant Code]=BAR.[Variant Code]  
              
      LEFT JOIN [ILEpadj] ILEPA  
      ON ILEPA.[Item No_]=BAR.[Item No_]  
      AND ILEPA.[Variant Code]=BAR.[Variant Code]  
              
      LEFT JOIN [ILEnadj] ILENA  
      ON ILENA.[Item No_]=BAR.[Item No_]  
      AND ILENA.[Variant Code]=BAR.[Variant Code]  
           
      WHERE  
      ILETR.[Location]='HPGW'  
      AND ILETR.[Entry Type]='4'  
      OR ILEMS.[Entry Type]='1'  
      OR ILEPA.[Entry Type]='2'  
      OR ILENA.[Entry Type]='3')  
          
      select isnull(a.[Item No_],TSEPS.[Item No_])[Item No_] ,  
      isnull(a.[Description],TSEPS.[Description]) [Description],  
      isnull(a.[Variant Code],TSEPS.[Variant Code]) [Variant Code],  
      isnull(a.[PDel],0) [PDel],  
      isnull(a.[MSales],0) [MSales],  
      isnull(a.[PAdj],0) [PAdj],  
      isnull(a.[NAdj],0) [NAdj],  
      isnull(TL.[UDel],0) [UDel],  
      ISNULL(TSEPS.[PSales],0) AS [PSales],  
      ISNULL(a.[PDel],0)+ISNULL(a.[PAdj],0)+ISNULL(TL.[UDel],0)+ISNULL(a.[MSales],0)+ISNULL(a.[NAdj],0)+ISNULL(TSEPS.[PSales],0) AS Total  
      from total a   
     FULL JOIN [TSEpsales] TSEPS  
      ON TSEPS.[Item No_]=a.[Item No_]  
      AND TSEPS.[Variant Code]=a.[Variant Code]  
      FULL JOIN [Transfeline] TL  
      ON TL.[Item No_]=A.[Item No_]  
      AND TL.[Variant Code]=A.[Variant Code]  
    

    Best regards
    Melissa


    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  4. glennyboy 121 Reputation points
    2020-10-12T06:46:57.807+00:00

    hi @MelissaMa-msft ,

    Good day!

    here's the full query, i comment some of the code;

    Line 135 --,ISNULL(TSEPS.[PSales],0) AS [PSales]

    Still getting wrong SUM qty for UDel

    • total SUM qty is 401, but in the query only 21

    Thank you!

         ;WITH Barcodes AS  
          (  
          SELECT  
           [BBI$Barcodes].[Item No_]  
           ,[BBI$Barcodes].[Variant Code]  
          FROM [BBI$Barcodes]  
          ),  
          ILEtransfer AS  
          (  
          SELECT  
           [BBI$Item Ledger Entry].[Entry Type]  
           ,[BBI$Item Ledger Entry].[Location Code] AS [Location]  
           ,[BBI$Item Ledger Entry].[Item No_]  
           ,MAX([BBI$Item Ledger Entry].[Description]) AS [Description]  
           ,[BBI$Item Ledger Entry].[Variant Code]  
           ,SUM([BBI$Item Ledger Entry].[Quantity]) AS [PDel]  
                  
          FROM [BBI$Item Ledger Entry]  
          WHERE  
           [BBI$Item Ledger Entry].[Location Code]='HPGW'  
           AND [BBI$Item Ledger Entry].[Entry Type] = '4'  
          GROUP BY  
           [BBI$Item Ledger Entry].[Location Code]  
           ,[BBI$Item Ledger Entry].[Entry Type]  
           ,[BBI$Item Ledger Entry].[Item No_]  
           ,[BBI$Item Ledger Entry].[Variant Code]  
          ),  
          ILEmsales AS  
          (  
          SELECT  
           [BBI$Item Ledger Entry].[Entry Type]  
           ,[BBI$Item Ledger Entry].[Location Code] AS [Location]  
           ,[BBI$Item Ledger Entry].[Item No_]  
           ,MAX([BBI$Item Ledger Entry].[Description]) AS [Description]  
           ,[BBI$Item Ledger Entry].[Variant Code]  
           ,SUM([BBI$Item Ledger Entry].[Quantity]) AS [MSales]  
                  
          FROM [BBI$Item Ledger Entry]  
          WHERE  
           [BBI$Item Ledger Entry].[Location Code]='HPGW'  
           AND [BBI$Item Ledger Entry].[Entry Type] = '1'  
           AND [BBI$Item Ledger Entry].[Document No_] NOT LIKE 'HP%'  
          GROUP BY  
           [BBI$Item Ledger Entry].[Location Code]  
           ,[BBI$Item Ledger Entry].[Entry Type]  
           ,[BBI$Item Ledger Entry].[Item No_]  
           ,[BBI$Item Ledger Entry].[Variant Code]  
          ),  
          ILEpadj AS  
          (  
          SELECT  
           [BBI$Item Ledger Entry].[Entry Type]  
           ,[BBI$Item Ledger Entry].[Location Code] AS [Location]  
           ,[BBI$Item Ledger Entry].[Item No_]  
           ,MAX([BBI$Item Ledger Entry].[Description]) AS [Description]  
           ,[BBI$Item Ledger Entry].[Variant Code]  
           ,SUM([BBI$Item Ledger Entry].[Quantity]) AS [PAdj]  
                  
          FROM [BBI$Item Ledger Entry]  
          WHERE  
           [BBI$Item Ledger Entry].[Location Code]='HPGW'  
           AND [BBI$Item Ledger Entry].[Entry Type] = '2'  
          GROUP BY  
           [BBI$Item Ledger Entry].[Location Code]  
           ,[BBI$Item Ledger Entry].[Entry Type]  
           ,[BBI$Item Ledger Entry].[Item No_]  
           ,[BBI$Item Ledger Entry].[Variant Code]  
          ),  
          ILEnadj AS  
          (  
          SELECT  
           [BBI$Item Ledger Entry].[Entry Type]  
           ,[BBI$Item Ledger Entry].[Location Code] AS [Location]  
           ,[BBI$Item Ledger Entry].[Item No_]  
           ,MAX([BBI$Item Ledger Entry].[Description]) AS [Description]  
           ,[BBI$Item Ledger Entry].[Variant Code]  
           ,SUM([BBI$Item Ledger Entry].[Quantity]) AS [NAdj]  
                  
          FROM [BBI$Item Ledger Entry]  
          WHERE  
           [BBI$Item Ledger Entry].[Location Code]='HPGW'  
           AND [BBI$Item Ledger Entry].[Entry Type] = '3'  
          GROUP BY  
           [BBI$Item Ledger Entry].[Location Code]  
           ,[BBI$Item Ledger Entry].[Entry Type]  
           ,[BBI$Item Ledger Entry].[Item No_]  
           ,[BBI$Item Ledger Entry].[Variant Code]  
          ),  
          Transfeline  AS  
          (  
          SELECT  
    			[BBI$Transfer Line].[Transfer-to Code]  
                ,[BBI$Transfer Line].[Item No_]  
    			,MAX([BBI$Transfer Line].[Description]) AS [Description]  
    			,MAX([BBI$Transfer Line].[Description 2]) AS [Description 2]  
                ,[BBI$Transfer Line].[Variant Code]  
                ,SUM([BBI$Transfer Line].[Quantity]) AS [UDel]  
            FROM [BBI$Transfer Line]  
            WHERE  
            [BBI$Transfer Line].[Transfer-to Code] = 'HPGW'  
            GROUP BY  
                [BBI$Transfer Line].[Transfer-to Code]  
                ,[BBI$Transfer Line].[Item No_]  
                ,[BBI$Transfer Line].[Variant Code]  
          ),  
          TSEpsales AS  
          (  
          SELECT  
           [BBI$Trans_ Sales Entry].[Store No_]  
           ,[BBI$Trans_ Sales Entry].[Item No_]  
           ,[BBI$Trans_ Sales Entry].[Variant Code]  
           ,SUM([BBI$Trans_ Sales Entry].[Quantity]) AS [PSales]  
          FROM [BBI$Trans_ Sales Entry]  
          WHERE  
           [BBI$Trans_ Sales Entry].[Store No_]='HPGW'  
           AND [BBI$Trans_ Sales Entry].[Transaction No_] NOT IN (5271,5272,5273,5278,5279,5280,5281,5282,5283,5284,5285,5286,8530,8531,8532,8533,8534,8535,8536,8537,13133,13849)  
          GROUP BY  
           [BBI$Trans_ Sales Entry].[Store No_]  
           ,[BBI$Trans_ Sales Entry].[Item No_]  
           ,[BBI$Trans_ Sales Entry].[Variant Code]  
          )  
          ,total as (     
          SELECT DISTINCT  
    	   ILETR.[Location]  
           ,BAR.[Item No_] AS [Item No_]  
           ,IT.[Description] AS [Description]  
    	   ,IT.[Description 2] AS [Description 2]  
    	   ,IT.[No_ 2] AS [No_ 2]  
           ,BAR.[Variant Code] AS [Variant Code]  
           ,ISNULL(ILETR.[PDel],0) AS [PDel]  
           ,ISNULL(ILEMS.[MSales],0) AS [MSales]  
           ,ISNULL(ILEPA.[PAdj],0) AS [PAdj]  
           ,ISNULL(ILENA.[NAdj],0) AS [NAdj]  
           ,ISNULL(TL.[UDel],0) AS [UDel]  
    --,ISNULL(TSEPS.[PSales],0) AS [PSales]  
            ,ISNULL(ILETR.[PDel],0)+ISNULL(ILEPA.[PAdj],0)+ISNULL(TL.[UDel],0)+ISNULL(ILEMS.[MSales],0)+ISNULL(ILENA.[NAdj],0) AS Total  
                  
          FROM [BBI$Barcodes] BAR  
               
          LEFT JOIN [ILEtransfer] ILETR  
          ON ILETR.[Item No_]=BAR.[Item No_]  
          AND ILETR.[Variant Code]=BAR.[Variant Code]  
                  
          LEFT JOIN [ILEmsales] ILEMS  
          ON ILEMS.[Item No_]=BAR.[Item No_]  
          AND ILEMS.[Variant Code]=BAR.[Variant Code]  
                  
          LEFT JOIN [ILEpadj] ILEPA  
          ON ILEPA.[Item No_]=BAR.[Item No_]  
          AND ILEPA.[Variant Code]=BAR.[Variant Code]  
                  
          LEFT JOIN [ILEnadj] ILENA  
          ON ILENA.[Item No_]=BAR.[Item No_]  
          AND ILENA.[Variant Code]=BAR.[Variant Code]  
                  
          LEFT JOIN [Transfeline] TL  
          ON TL.[Item No_]=BAR.[Item No_]  
          AND TL.[Variant Code]=BAR.[Variant Code]  
      
    	  LEFT JOIN [BBI$Item] IT  
    	  ON IT.[No_]=BAR.[Item No_]  
               
          WHERE  
          ILETR.[Location]='HPGW'  
          AND ILETR.[Entry Type]='4'  
          OR ILEMS.[Entry Type]='1'  
          OR ILEPA.[Entry Type]='2'  
          OR ILENA.[Entry Type]='3')  
              
          select   
    	  isnull(a.[Item No_],TSEPS.[Item No_])[Item No_] ,  
    	  isnull(a.[Description],[Description])[Description] ,  
    	  isnull(a.[Description],[Description 2])[Description 2] ,  
    	  isnull(a.[No_ 2],[No_ 2])[No2] ,  
          isnull(a.[Variant Code],TSEPS.[Variant Code]) [Variant Code],  
          isnull(a.[PDel],0) [PDel],  
          isnull(a.[MSales],0) [MSales],  
          isnull(a.[PAdj],0) [PAdj],  
          isnull(a.[NAdj],0) [NAdj],  
          isnull(a.[UDel],0) [UDel],  
          ISNULL(TSEPS.[PSales],0) AS [PSales],  
          ISNULL(a.[PDel],0)+ISNULL(a.[PAdj],0)+ISNULL(a.[UDel],0)+ISNULL(a.[MSales],0)+ISNULL(a.[NAdj],0)+ISNULL(TSEPS.[PSales],0) AS Total  
          from total a   
         FULL JOIN [TSEpsales] TSEPS  
          ON TSEPS.[Item No_]=a.[Item No_]  
          AND TSEPS.[Variant Code]=a.[Variant Code]  
    

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.