| Author |
Topic |
|
ayamas
Aged Yak Warrior
552 Posts |
Posted - 2006-06-07 : 01:58:58
|
| Hi Guys,I want to display a report like this Date Balance on Date Credit Debit Current Balance 02/05/06 0 1200 0 1200 05/05/06 1200 500 0 1700 10/05/06 1700 0 200 1500 15/05/06 1500 200 0 1700I mean for the new date the balance of the previous day transaction should be displayed. How can it be done?Any help is greately appreciated.Thanks in advance |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-06-07 : 02:11:35
|
Is it like this ?declare @table table( [Date] datetime, [Balance on Date] int, [Credit] int, [Debit] int, [Current Balance] int)set dateformat dmyinsert into @tableselect '02/05/06', 0, 1200, 0, 1200 union allselect '05/05/06', 1200, 500, 0, 1700 union allselect '10/05/06', 1700, 0, 200, 1500 union allselect '15/05/06', 1500, 200, 0, 1700select *, [Previous Balance] = (select top 1 [Current Balance] from @table x where x.[Date] < t.[Date] order by x.[Date] desc)from @table torder by [Date] KH |
 |
|
|
ayamas
Aged Yak Warrior
552 Posts |
Posted - 2006-06-07 : 02:17:40
|
| Thanks for ur replyBut khtan I cannot hard code the values in my query.I want it to be dynamic. |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-06-07 : 02:18:19
|
"I cannot hard code the values in my query.I want it to be dynamic."if you are referring to the "insert into .. select union ....", these are only for testing of the query KH |
 |
|
|
ayamas
Aged Yak Warrior
552 Posts |
Posted - 2006-06-07 : 02:23:07
|
| Yes khtan the output is perfect.But how can it be done dynamically? |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-06-07 : 02:23:47
|
what do you mean dynamically ? can you explain more ? KH |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-06-07 : 02:26:50
|
just use the query and replace your table name and column name with the actual name.select *, [Previous Balance] = (select top 1 [Current Balance] from @table x where x.[Date] < t.[Date] order by x.[Date] desc)from yourtablenamehere torder by [Date] KH |
 |
|
|
ayamas
Aged Yak Warrior
552 Posts |
Posted - 2006-06-07 : 02:27:14
|
| What I meant was i have the records in a table.there are about 2000 transactions like this in it.But in that table the Balance of the last transaction date is not stored.So I want to display what was the balance on the last transaction date for every new transaction. |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-06-07 : 02:28:40
|
can you post your table structure, some sample data and the result that you want ? KH |
 |
|
|
ayamas
Aged Yak Warrior
552 Posts |
Posted - 2006-06-07 : 02:38:39
|
| I am posting just 4 columns from the table.The data is stored this wayDate Credit Debit Curr Balance02/05/06 1200 0 1200 05/05/06 500 0 1700 10/05/06 0 200 1500 15/05/06 0 200 1300I mean the date when the transaction was done & whether that transaction was of credit or debit.But I want the output like this.I mean for each new transaction the cur balance of previous transaction should be displayed in bal on dt column.Date Bal on Dt Credit Debit Cur Bal02/05/06 0 1200 0 120005/05/06 1200 500 0 1700 10/05/06 1700 0 200 150015/05/06 1500 200 0 1700While the current balance is calculated depending upon whether the transaction was of debit or credit. |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-06-07 : 02:50:11
|
[code]declare @table table( [Date] datetime, [Credit] int, [Debit] int)set dateformat dmyinsert into @tableselect '02/05/06', 1200, 0 union allselect '05/05/06', 500, 0 union allselect '10/05/06', 0, 200 union allselect '15/05/06', 0, 200 select [Date], [Prev Balance] = isnull((select sum(Credit - Debit) from @table x where x.[Date] < t.[Date]), 0), [Credit], [Debit], [Curr Balance] = isnull((select sum(Credit - Debit) from @table x where x.[Date] <= t.[Date]), 0)from @table torder by [Date]/*Date Prev Balance Credit Debit Curr Balance ----------- ------------ ----------- ----------- ------------ 2006-05-02 0 1200 0 12002006-05-05 1200 500 0 17002006-05-10 1700 0 200 15002006-05-15 1500 0 200 1300*/[/code] KH |
 |
|
|
ayamas
Aged Yak Warrior
552 Posts |
Posted - 2006-06-07 : 03:03:37
|
| U know what khtan you are a GENIUS.U know I tried this thing yesterday the whole day but I could not get the output but you solved it in jst few minutes.I tried using cursors I tried to use loops but with no success.Thank you very very much.Thanks a lot once again. |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-06-07 : 03:10:37
|
You are welcome  KH |
 |
|
|
RyanRandall
Master Smack Fu Yak Hacker
1074 Posts |
Posted - 2006-06-07 : 06:14:10
|
Hi all,ayamas - Khtan's suggestion is great, but beware of an issue if you can have duplicate dates...declare @table table( [Date] datetime, [Credit] int, [Debit] int)set dateformat dmyinsert into @tableselect '02/05/06', 1200, 0 union allselect '05/05/06', 500, 0 union allselect '05/05/06', 100, 0 union allselect '10/05/06', 0, 200 union allselect '15/05/06', 0, 200 select [Date], [Prev Balance] = isnull((select sum(Credit - Debit) from @table x where x.[Date] < t.[Date]), 0), [Credit], [Debit], [Curr Balance] = isnull((select sum(Credit - Debit) from @table x where x.[Date] <= t.[Date]), 0)from @table torder by [Date]/*Date Prev Balance Credit Debit Curr Balance ------------------------------------------------------ ------------ ----------- ----------- ------------ 2006-05-02 00:00:00.000 0 1200 0 12002006-05-05 00:00:00.000 1200 500 0 18002006-05-05 00:00:00.000 1200 100 0 18002006-05-10 00:00:00.000 1800 0 200 16002006-05-15 00:00:00.000 1600 0 200 1400/* Ryan Randallwww.monsoonmalabar.com London-based IT consultancy Solutions are easy. Understanding the problem, now, that's the hard part. |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-06-07 : 06:22:58
|
"beware of an issue if you can have duplicate dates..."Ryan, you are correct. Hopefully, date is the PK of the table  KH |
 |
|
|
RyanRandall
Master Smack Fu Yak Hacker
1074 Posts |
Posted - 2006-06-07 : 06:30:01
|
Yeah, I hope so too! Otherwise it might be better to resort to Madhivanan's standard "do it at the front-end" response Ryan Randallwww.monsoonmalabar.com London-based IT consultancy Solutions are easy. Understanding the problem, now, that's the hard part. |
 |
|
|
mmarovic
Aged Yak Warrior
518 Posts |
Posted - 2006-06-07 : 06:57:55
|
quote: Originally posted by RyanRandall Yeah, I hope so too! Otherwise it might be better to resort to Madhivanan's standard "do it at the front-end" response Ryan Randallwww.monsoonmalabar.com London-based IT consultancy Solutions are easy. Understanding the problem, now, that's the hard part.
It might be better anyway. |
 |
|
|
ayamas
Aged Yak Warrior
552 Posts |
Posted - 2006-06-07 : 07:29:20
|
| Hi guys,thanks for all of your valuable suggestions.But unfortunately date is not the PK in the table because on one date there can be more than one transaction. |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-06-07 : 07:46:46
|
"date is not the PK in the table because on one date there can be more than one transaction."In that case, is this acceptable ?declare @table table( [Date] datetime, [Credit] int, [Debit] int)set dateformat dmyinsert into @tableselect '02/05/06', 1200, 0 union allselect '05/05/06', 500, 0 union allselect '05/05/06', 100, 0 union allselect '10/05/06', 0, 200 union allselect '15/05/06', 0, 200 select [Date], [Prev Balance] = isnull((select sum(Credit - Debit) from @table x where x.[Date] < t.[Date]), 0), [Credit] = sum([Credit]), [Debit] = sum([Debit]), [Curr Balance] = isnull((select sum(Credit - Debit) from @table x where x.[Date] <= t.[Date]), 0)from @table tgroup by [Date]order by [Date]/* RESULT : Date Prev Balance Credit Debit Curr Balance ----------------------- ------------ ----------- ----------- ------------ 2006-05-02 00:00:00.000 0 1200 0 12002006-05-05 00:00:00.000 1200 600 0 18002006-05-10 00:00:00.000 1800 0 200 16002006-05-15 00:00:00.000 1600 0 200 1400*/ KH |
 |
|
|
ayamas
Aged Yak Warrior
552 Posts |
Posted - 2006-06-07 : 08:00:27
|
| No khtan I tried with this dataselect '02/05/06', 1200, 0 union allselect '05/05/06', 500, 0 union allselect '05/05/06', 100, 0 union allselect '05/05/06', 0, 550 union allselect '10/05/06', 0, 200 union allselect '12/05/06', 1000, 0 union allselect '15/05/06', 0, 200 it shows the output as followsDate Prev Bal Credit debit Curr Balance 2006-05-02 0 1200 0 12002006-05-05 1200 600 550 12502006-05-10 1250 0 200 1050Guys I cant do it from the front end because I want to display it as a report in Crystal Reports.Is there anyway it can be done in CR because I thought that I will make a stored proc & then execute it in the report. |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-06-07 : 08:04:30
|
What is the PK of your table ? Can you post your table structure ? KH |
 |
|
|
Next Page
|