Quantcast
Channel: SCN: Message List
Viewing all articles
Browse latest Browse all 9159

Issue in Month Wise Stock Query

$
0
0

Dear Experts,

I have a Query which gives month wise Stock report for last 12 months but the issue with this query is that whenever the month cross the year it does not take closing of Dec 2013 as Opening of next year month, it start the opening of year with 0 balance.

for Example : Item Code is "A0001" and parameter date is '20140301'

 

Month:Year

Item Name

Opening

Receipt

Issue

Closing

Nov-2013

A0001

15

10

5

20

Dec-2013

A0001

20

20

10

30

Jan-2014

A0001

0

50

15

35

Feb-2014

A0001

35

10

5

40

 

so in the above example the Opening of for the month of Jan-2014 should be 30, this is the issue am facing in my query.

below is my query you can check , may be i have done something wrong.

 

So far I have achieved this Stock Query

 

SELECT

Distinct MONTH(X.DocDate)As'Month',DATENAME(Month,(X.DocDate))AS'MonthName',DATENAME(Year,(X.DocDate))AS'Year',X.ItemCode,X.Dscription,

(SELECT avg(A.Price) FROM OINM A Where A.ItemCode=X.Itemcode And MONTH(A.DocDate)=MONTH(X.DocDate) ) as 'Price',

Case When YEAR(DocDate)=-1 Then

IsNull(((SELECT Sum(A.InQty) FROM OINM A Where A.ItemCode=X.Itemcode And DATENAME(MONTH,(A.DocDate))=DATENAME(MONTH,(X.DocDate)) AND DATENAME(YEAR,(A.DocDate))=DATENAME(YEAR,(X.DocDate))-1) -

(SELECT Sum(A.OutQty) FROM OINM A Where A.ItemCode=X.Itemcode And DATENAME(Year,(A.DocDate))=DATENAME(Year,(X.DocDate))-1)),0) Else

IsNull(((SELECT Sum(A.InQty) FROM OINM A Where A.ItemCode=X.Itemcode And MONTH(A.DocDate)<(MONTH(X.DocDate)) And DATENAME(Year,(A.DocDate))=DATENAME(Year,(X.DocDate))) -

(SELECT Sum(A.OutQty) FROM OINM A Where A.ItemCode=X.Itemcode And MONTH(A.DocDate)<(MONTH(X.DocDate))And DATENAME(Year,(A.DocDate))=DATENAME(Year,(X.DocDate)))),0)End As 'Opening Balance',

 

 

(SELECT Sum(A.InQty) FROM OINM A Where A.ItemCode=X.Itemcode And MONTH(A.DocDate)=MONTH(X.DocDate)And DATENAME(Year,(A.DocDate))=DATENAME(Year,(X.DocDate))) AS 'Receipt',

(SELECT Sum(A.OutQty) FROM OINM A Where A.ItemCode=X.Itemcode And MONTH(A.DocDate)=MONTH(X.DocDate)And DATENAME(Year,(A.DocDate))=DATENAME(Year,(X.DocDate))) as 'Issue',

 

 

(Case When YEAR(DocDate)=-1 Then

IsNull(((SELECT Sum(A.InQty) FROM OINM A Where A.ItemCode=X.Itemcode And DATENAME(Year,(A.DocDate))=DATENAME(Year,(X.DocDate))-1) -

(SELECT Sum(A.OutQty) FROM OINM A Where A.ItemCode=X.Itemcode And DATENAME(Year,(A.DocDate))=DATENAME(Year,(X.DocDate))-1)),0) Else

IsNull(((SELECT Sum(A.InQty) FROM OINM A Where A.ItemCode=X.Itemcode And MONTH(A.DocDate)<(MONTH(X.DocDate)) And DATENAME(Year,(A.DocDate))=DATENAME(Year,(X.DocDate))) -

(SELECT Sum(A.OutQty) FROM OINM A Where A.ItemCode=X.Itemcode And MONTH(A.DocDate)<(MONTH(X.DocDate))And DATENAME(Year,(A.DocDate))=DATENAME(Year,(X.DocDate)))),0)End+

((SELECT Sum(A.InQty) FROM OINM A Where A.ItemCode=X.Itemcode And MONTH(A.DocDate)=MONTH(X.DocDate)And DATENAME(Year,(A.DocDate))=DATENAME(Year,(X.DocDate))) -

(SELECT Sum(A.OutQty) FROM OINM A Where A.ItemCode=X.Itemcode And MONTH(A.DocDate)=MONTH(X.DocDate)And DATENAME(Year,(A.DocDate))=DATENAME(Year,(X.DocDate))))) as 'Closing Balance'

FROM OINM X

 

WHERE X.ItemCode ='A0001' And X.DocDate >  DATEADD(yyyy,-1,'20140530') And X.DocDate < '201400301'

Group By DocDate,ItemCode,Dscription

Order by DATENAME(Year,(X.DocDate))

 

 

Can any one help??

 

 

Thanks in Advance!

Regards

Manish


Viewing all articles
Browse latest Browse all 9159

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>