Click here to Skip to main content
15,903,854 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I want to make the closing balance of the current month lets say of March to be the opening balance of April in sql or crystal reports how to i go about it. Help need the solution agently please. the script below is the one i have used to get the balance:

SQL
select StockNumber,InventoryName,Year1,Month1,RECEIVE_FROM_PO,RECEIVE_XFER,RECEIVE_ADJ,
_RETURN,ADJUSTMENT,ISSUE,PT_ISSUE,TRANSFER, SUM(ABS(RECEIVE_FROM_PO + RECEIVE_XFER + RECEIVE_ADJ + _RETURN))
- SUM(ABS(ADJUSTMENT + ISSUE + PT_ISSUE + TRANSFER))
 as BALANCE, BALANCE as OPENING_BAL
into final2
from dbo.drug_summary$
GROUP BY StockNumber,InventoryName,Year1,Month1,RECEIVE_FROM_PO,RECEIVE_XFER,RECEIVE_ADJ,
_RETURN,ADJUSTMENT,ISSUE,PT_ISSUE,TRANSFER,BALANCE
Posted

1 solution

example,
A table inv [Inventory]
----
VouNo int
VouDt datetime
InvId int
Qty Numeric(12,2)   [+ if inward... - if Outward]

so,
Query is like below
SQL
select VouDt, InvId, 
(select sum(Qty) from inv where VouDt< i.VouDt) as OpnQty,
sum(Qty) as Qty,
(select sum(Qty) from inv where VouDt< i.VouDt)- sum(Qty) as ClsQty
from Inv as i
where voudt >= '2012-04-01' and vouDt<='2012-04-30' -- filter date here it's for april-2012
group by InvId,VouDt


Happy Coding!
:)
 
Share this answer
 
Comments
vusamozi 10-Dec-12 9:26am    
thanx Aarti but the script is giving me errors and i have dibugged some and this error seems to pop up:- Msg 102, Level 15, State 1, Line 4
Incorrect syntax near '-'.

below is the code and the table:

USE [centraldb]
GO
/****** Object: Table [dbo].[Sheet2$] Script Date: 12/10/2012 16:16:25 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Sheet2$](
[StockNumber] [nvarchar](255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Description] [nvarchar](255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[InventoryName] [nvarchar](255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Mth/Yr] [datetime] NULL,
[ADJUSTMENT] [float] NULL,
[ISSUE] [float] NULL,
[PT_ISSUE] [float] NULL,
[RECEIVE_ADJ] [float] NULL,
[RECEIVE_FROM_PO] [float] NULL,
[RECEIVE_XFER] [float] NULL,
[_RETURN] [float] NULL,
[TRANSFER] [float] NULL
) ON [PRIMARY]


SELECT StockNumber,InventoryName,Mth/Yr,RECEIVE_FROM_PO,RECEIVE_XFER,RECEIVE_ADJ,_RETURN,ADJUSTMENT,ISSUE,PT_ISSUE,TRANSFER,
(SELECT SUM(RECEIVE_FROM_PO + RECEIVE_XFER + RECEIVE_ADJ + _RETURN + CLOSING_BAL) from dbo.Sheet2$ where Mth/Yr < i.Mth/Yr)
- SUM(ADJUSTMENT + ISSUE + PT_ISSUE + TRANSFER) FROM Sheet2$ AS OPENING_BAL,
(SELECT SUM(ADJUSTMENT + ISSUE + PT_ISSUE + TRANSFER) from dbo.Sheet2$ where Mth/Yr < i.Mth/Yr) - SUM(ADJUSTMENT + ISSUE + PT_ISSUE + TRANSFER)
AS CLOSING_BAL FROM dbo.Sheet2$ AS i
WHERE Mth/Yr >= '2004-01-01' AND Mth/Yr <= '2012-12-31'
GROUP BY Mth/Yr

please help agently
Aarti Meswania 10-Dec-12 10:28am    
you wrote it wrong
it should be like this,

select StockNumber, [Description], InventoryName, [Mth/Yr],Opn_Qty,Qty,Opn_Qty-Qty as Cls_Qty
from
(

select
StockNumber, [Description], InventoryName, [Mth/Yr],

(
SUM(RECEIVE_FROM_PO + RECEIVE_XFER + RECEIVE_ADJ + _RETURN)
-SUM(ADJUSTMENT + ISSUE + PT_ISSUE + [TRANSFER])
) as Qty,

(
SELECT SUM(RECEIVE_FROM_PO + RECEIVE_XFER + RECEIVE_ADJ + _RETURN)
-SUM(ADJUSTMENT + ISSUE + PT_ISSUE + [TRANSFER])
from dbo.Sheet2$ where [Mth/Yr] < i.[Mth/Yr]
)
AS Opn_Qty

from a.dbo.sheet2$ as i

WHERE [Mth/Yr] >= '2004-01-01' AND [Mth/Yr] <= '2012-12-31'
GROUP BY StockNumber, [Description], InventoryName, [Mth/Yr]

)
as a
vusamozi 11-Dec-12 9:24am    
thank you Aarti i have used the script and its working just fine but i have issues with the null values it brings after running the script, yet they is a qty value.
vusamozi 11-Dec-12 9:24am    
thank you Aarti i have used the script and its working just fine but i have issues with the null values it brings after running the script, yet they is a qty value.
Aarti Meswania 11-Dec-12 20:47pm    
okay
use isnull function

change like below,

SUM( Isnull(RECEIVE_FROM_PO,0) + Isnull(RECEIVE_XFER,0) + Isnull(RECEIVE_ADJ,0)...

this way put all qty fields in isnull function then test


Happy Coding!
:)

Glad to help you!
:)

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