i am using sql server 2008
my table is
CREATE TABLE [dbo].[tbl_enventry](
[TransactionDate] [smalldatetime] NULL,
[Item] [char](1) NULL,
[RecQty] [int] NULL,
[IssueQty] [int] NULL
(
i have written query as
DECLARE @StartDate SMALLDATETIME = '2011-06-01 00:00:00', @EndDate SMALLDATETIME = '2011-06-02 00:00:00'; WITH opneningBalance(Item, prevR, prevI) AS ( SELECT Item, SUM(RecQty), SUM(IssueQty) FROM tbl_enventry WHERE TransactionDate < @EndDate GROUP BY Item ), CurrentBalance(Item, curR, curI) AS ( SELECT Item, SUM(RecQty), SUM(IssueQty) FROM tbl_enventry WHERE TransactionDate BETWEEN @StartDate AND @EndDate GROUP BY Item ) SELECT CurrentBalance.Item, Opening = ISNULL(prevR,0) - ISNULL(prevI,0), RecQty = curR, IssueQty = curI, BalanceQty = ISNULL(prevR,0) - ISNULL(prevI,0) + (curR - curI) FROM CurrentBalance inner JOIN opneningBalance ON CurrentBalance.Item = opneningBalance.Item ;
if opening balance is null then all data showing balank, if i used full outter joint or right out joint it give wrong data, please help me i want result asItem Opening RecQty IssueQty BalanceQty
---- --------------------------------------- ----------- ----------- ---------------------------------------
A 10 10 0 20
Thanks Best Regard Naweez