Click here to Skip to main content
15,887,343 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi ,
Please help me with my problem

i have this query

SQL
DEclare @ID as int = '96';
DEclare @StoreID as int = '1';
DEclare @Add_Units as int = (SELECT [Add_Units] FROM Item WHERE [ID] = @ID);
DEclare @Units as int = (SELECT [Units] FROM Item WHERE [ID] = @ID);
DEclare @Add_Units_Count as int = (SELECT [Add_Units_Count] FROM Item WHERE [ID] = @ID);
IF OBJECT_ID('tempdb..#TempStore') IS NOT NULL DROP Table #TempStore;
WITH RET AS 
(
SELECT * FROM Store_Entery WHERE [ItemID] = @ID AND [StoreID] = @StoreID AND ([Type] = 'Begin' OR [Type] = 'Pay' OR [Type] = 'Refanded' OR [Type] = 'AzenEdafa' OR [Type] = 'AzenSarfRefand' OR [Type] = 'TaswiaStoresPlus')
Union ALL
SELECT * FROM Store_Entery WHERE [ItemID] = @ID AND [StoreID] = @StoreID AND ([Type] = 'Invoices' OR [Type] = 'RPay' OR [Type] = 'AzenEdafaRefand' OR [Type] = 'AzenSarf' OR [Type] = 'TaswiaStoresMins' OR [Type] = 'AzenEdafaRefand')
)
SELECT (CASE When ([Type] = 'Begin') THEN 0 ELSE Row_Number() OVER (ORDER BY @@Rowcount)END) AS IDO,* Into #TempStore FROM RET ORDER BY [Date],@@Rowcount ASC 

SELECT 
SS.IDO,
(Case When @Add_Units != 0 THEN 
(
(SELECT (SUM(ISNULL(SBO.[PayAmountTotal], 0)) + (SUM(ISNULL(SBO.[PayAmountTotalS], 0)))) AS Expr1 FROM #TempStore SBO WHERE (SBO.IDO <= SS.IDO) AND (SBO.[ItemID] = SS.ItemID) AND (SBO.[Type] = 'Begin' OR SBO.[Type] = 'Pay' OR SBO.[Type] = 'Refanded' OR SBO.[Type] = 'AzenEdafa' OR SBO.[Type] = 'AzenSarfRefand' OR SBO.[Type] = 'TaswiaStoresPlus') AND SBO.StoreID = SS.StoreID) 
-
(((SELECT (SUM(ISNULL(SBO.[PayAmountTotal], 0)) + (SUM(ISNULL(SBO.[PayAmountTotalS], 0)))) AS Expr1 FROM #TempStore SBO WHERE (SBO.IDO <= SS.IDO) AND (SBO.[ItemID] = SS.ItemID) AND (SBO.[Type] = 'Begin' OR SBO.[Type] = 'Pay' OR SBO.[Type] = 'Refanded' OR SBO.[Type] = 'AzenEdafa' OR SBO.[Type] = 'AzenSarfRefand' OR SBO.[Type] = 'TaswiaStoresPlus') AND SBO.StoreID = SS.StoreID) 
/ (SELECT (((SUM(ISNULL(SBO.Plus,0)) + SUM(ISNULL(SBO.PlusT,0)))) / Cast(@Add_Units_Count AS int))
FROM #TempStore SBO WHERE (SBO.IDO <= SS.IDO) AND (SBO.[ItemID] = SS.ItemID) AND SBO.StoreID = SS.StoreID)) 
*
(ISNULL((SELECT (((SUM(ISNULL(SBO.Mins,0)) + SUM(ISNULL(SBO.MinsT,0)))) / Cast(@Add_Units_Count AS int))
FROM #TempStore SBO WHERE (SBO.IDO <= SS.IDO) AND (SBO.[ItemID] = SS.ItemID) AND SBO.StoreID = SS.StoreID) ,0)))
)
/ 
(ISNULL((SELECT (((SUM(ISNULL(SBO.Plus,0)) + SUM(ISNULL(SBO.PlusT,0))) - (SUM(ISNULL(SBO.Mins,0)) + SUM(ISNULL(SBO.MinsT,0)))) / Cast(@Add_Units_Count AS int))
FROM #TempStore SBO WHERE (SBO.IDO <= SS.IDO) AND (SBO.[ItemID] = SS.ItemID) AND SBO.StoreID = SS.StoreID) ,0))
END) AS Expr22,
*
FROM #TempStore SS LEFT OUTER JOIN
Item AS IT ON IT.ID = SS.ItemID LEFT OUTER JOIN
Units AS UI ON IT.Units = UI.ID LEFT OUTER JOIN
Units AS US ON IT.Add_Units = US.ID 
ORDER BY SS.IDO ASC


i'm try to get weighted average can't fug er out how i do this prefect
i need to get all Purchases cost
And which must decrease the cost of sales
and get the last cost to Divide by your balance
to get the last average
but i don't save the cost of sales
i need to get cost from last average
my problem now is
after item stock is non
the averag is non or null
this is now problem but the real problem after i make a new Purchases
he give me wrong averag

this is photo from query execute

[^]

last row in expr22 must be 130 not 120
please help me

What I have tried:

big problem please help me
my problem with weighted average sql query :(
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900