USE [CRONUS]
drop table BIT_Temp_InvVal
drop table BIT_Temp_InvVal_CombA
drop table BIT_Temp_InvVal_CombB
DECLARE @D Date Set @D = '07/12/17'
DECLARE @L varchar(5) Set @L = 'WM'
--Precalc CombA
Select * Into BIT_Temp_InvVal_CombA from (
SELECT [Item No_], [Location Code], CAST(SUM(Q) + SUM(Q2) AS float) AS Qty, CAST(SUM(InventoryValue) AS float) AS InventoryValue
FROM (SELECT [Entry No_], [Item No_], [Location Code], Quantity AS Q,
(SELECT ISNULL(SUM(IAE.Quantity), 0) AS EXPR1
FROM [CRONUS$Item Application Entry] AS IAE INNER JOIN
[CRONUS$Item Ledger Entry] AS ILE2 ON IAE.[Inbound Item Entry No_] = ILE2.[Entry No_]
WHERE (IAE.[Posting Date] <= @D) AND (ILE2.[Posting Date] <= @D) AND (ILE.[Item No_] = ILE2.[Item No_]) AND
(IAE.[Outbound Item Entry No_] = ILE.[Entry No_]) AND (IAE.[Item Ledger Entry No_] = ILE.[Entry No_])AND [Location Code] = @L) AS Q2,
(SELECT SUM([Cost Amount (Expected)] + [Cost Amount (Actual)]) AS EXPR1
FROM [CRONUS$Value Entry] AS V
WHERE ([Posting Date] <= @D) AND ([Item Ledger Entry No_] = ILE.[Entry No_])AND [Location Code] = @L) AS InventoryValue
FROM [CRONUS$Item Ledger Entry] AS ILE
WHERE ([Posting Date] <= @D) AND (Positive = 0) AND [Location Code] = @L) AS OutBoundEntries
GROUP BY [Item No_], [Location Code]) as CombA
--DECLARE @D Date Set @D = '07/12/17'
--DECLARE @L varchar(5) Set @L = 'WM'
--Precalc CombB
Select * Into BIT_Temp_InvVal_CombB from (
SELECT [Item No_], [Location Code], CAST(SUM(Q) - SUM(Q2) AS float) AS Qty, CAST(SUM(InventoryValue) AS float) AS InventoryValue
FROM (SELECT [Item No_], [Location Code], Quantity AS Q,
(SELECT ISNULL(SUM(IAE.Quantity), 0) AS EXPR1
FROM [CRONUS$Item Application Entry] AS IAE INNER JOIN
[CRONUS$Item Ledger Entry] AS ILE2 ON IAE.[Item Ledger Entry No_] = ILE2.[Entry No_]
WHERE (IAE.[Posting Date] <= @D) AND (ILE2.[Posting Date] <= @D) AND (ILE.[Entry No_] = IAE.[Inbound Item Entry No_]) AND
(IAE.[Outbound Item Entry No_] <> 0) AND (IAE.[Item Ledger Entry No_] <> ILE.[Entry No_]) AND [Location Code] = @L) AS Q2,
(SELECT ISNULL(SUM([Cost Amount (Expected)] + [Cost Amount (Actual)]), 0) AS EXPR1
FROM [CRONUS$Value Entry] AS V
WHERE ([Posting Date] <= @D) AND ([Item Ledger Entry No_] = ILE.[Entry No_])AND [Location Code] = @L) AS InventoryValue
FROM [CRONUS$Item Ledger Entry] AS ILE
WHERE ([Posting Date] <= @D) AND (Positive = 1)AND [Location Code] = @L) AS InBoundEntries
GROUP BY [Item No_], [Location Code]) as CombB
--Combine Final Report
select * into BIT_Temp_InvVal from (
SELECT CRONUS$Item.No_ AS ItemNo, CRONUS$Item.[Vendor No_], CRONUS$Vendor.Name AS VendorName, CRONUS$Item.Description,
Detail.[Location Code], CRONUS$Item.[Division Code], CRONUS$Item.[Attrib 4 Code] AS CatReportingGrp,
CRONUS$Vendor.[Vendor Posting Group], Detail.Qty, Detail.InventoryValue, ISNULL(ROUND(Detail.InventoryValue / NULLIF (Detail.Qty, 0), 2), 0)
AS UnitCost
FROM CRONUS$Item INNER JOIN
(SELECT [Item No_], [Location Code], SUM(Qty) AS Qty, SUM(InventoryValue) AS InventoryValue
FROM (SELECT * from BIT_Temp_InvVal_CombA
UNION
SELECT * from BIT_Temp_InvVal_CombB) AS CombinedEntries
GROUP BY [Item No_], [Location Code]) AS Detail ON CRONUS$Item.No_ = Detail.[Item No_] LEFT OUTER JOIN
CRONUS$Vendor ON CRONUS$Item.[Vendor No_] = CRONUS$Vendor.No_
--ORDER BY ItemNo
) as a
GO
No comments:
Post a Comment