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
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]