Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 need script in MySql whose script is in sql 2005

Author  Topic 

Ronesh
Starting Member

33 Posts

Posted - 2009-09-02 : 01:06:15

declare @table1 table
(
[date] datetime,
particular varchar(15),
debit money,
credit money
)
insert into @table1
values('2001/01/02','test',null,1000)

insert into @table1
values('2001/01/02','test',100,null)

insert into @table1
values('2001/01/02','test',null,500)

insert into @table1
values('2001/01/03','test',1000,null);



WITH temp
AS
(
SELECT row_number() OVER (ORDER BY [date]) AS [ID], * FROM @table1
)
SELECT [date] AS 'Trandate', particular, debit, credit,
(SELECT SUM( -ISNULL(t2.debit,0) + ISNULL(t2.credit,0)) AS 'Balance'
FROM temp t2
WHERE
t2.[ID] <= t1.[ID]) AS Balance
FROM temp t1

TranDate particular debit credit Balance
2001-01-02 00:00:00.000 test NULL 1000.00 1000.00
2001-01-02 00:00:00.000 test 100.00 NULL 900.00
2001-01-02 00:00:00.000 test NULL 500.00 1400.00
2001-01-03 00:00:00.000 test 1000.00 NULL 400.00


looking forward for positive response.

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-09-02 : 07:03:20
Post your question in www.mysql.com

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -