Solved

Updating multiple records from the previous row

Posted on 2012-03-29
16
280 Views
Last Modified: 2012-04-03
Hello Experts,

I have attached an excel for the sample data and my requirements.

With Kind Regards
Book2.xlsx
0
Comment
Question by:vikas_nm
  • 7
  • 5
  • 4
16 Comments
 
LVL 51

Expert Comment

by:HainKurt
Comment Utility
What I want in the output is that if the closing balance then the op balance for the same date will become closing balance

having problems understanding this...
0
 
LVL 51

Expert Comment

by:HainKurt
Comment Utility
maybe this:

(case when closing is not null then closing else opening end) as closingBalance

just add this into your select...
0
 

Author Comment

by:vikas_nm
Comment Utility
What I want in the output is that if the there is no closing balance for the date then the op balance for the same date will become closing balance
0
 

Author Comment

by:vikas_nm
Comment Utility
i want a view for the same.
0
 
LVL 9

Expert Comment

by:rajeevnandanmishra
Comment Utility
Hi,
It might be a better idea to have the backup of your original table before trying to run the code.
Assumptions i have taken:
1) The DivisionID, Opening & Closing are Integer
2) The LedgerId is same in all the records
3) The data is clustered index on LedgerId, DivisionId, ClosingDate (This will make sure the ordering is proper during the calculation)

Now try the below code:

declare @DivisionId int, @Opening int, @Closing int
select  @DivisionId = 0, @Opening = 0, @Closing = 0 
update myTable  
set 	Opening = case when isnull(opening,0) = 0 then @opening else opening end,
	Closing = case when isnull(closing,0) = 0 then @closing else closing end, 
	@opening = case when DivisionId = @DivisionId then @closing else 0 end, 
	@opening = case when isnull(opening,0) = 0 then @opening else opening end, 
	@closing = case when isnull(closing,0) = 0 then @opening else closing end,
	@Divisionid = divisionid 
select * from myTable

Open in new window

0
 

Author Comment

by:vikas_nm
Comment Utility
I think you got it wrong. The table data will remain the same. I would like a view for the data from the table as mentioned in the xl output section.
0
 
LVL 9

Expert Comment

by:rajeevnandanmishra
Comment Utility
I am afraid, in a single direct SQL statement it is not possible as per my knowledge.
Yes you can create a multi-line table valued function. I have written the below code considering that you will call this function by passing the LedgerId.

create function dbo.retTable(@LedgerId Int) 
	returns @table table (LedgerId int, DivisionId int, ClosingDate date, opening int, closing int) 
as 
begin
	declare @DivisionId int, @Opening int, @Closing int
	select  @DivisionId = 0, @Opening = 0, @Closing = 0 
	insert into @table 
	select * from myTable where LedgerId = @LedgerId  
	update @table 
	set 	Opening = case when isnull(opening,0) = 0 then @opening else opening end,
		Closing = case when isnull(closing,0) = 0 then @closing else closing end, 
		@opening = case when DivisionId = @DivisionId then @closing else 0 end, 
		@opening = case when isnull(opening,0) = 0 then @opening else opening end, 
		@closing = case when isnull(closing,0) = 0 then @opening else closing end,
		@Divisionid = divisionid 
	return
end

Open in new window


And then call the function like this:

select * from dbo.retTable(101)

Open in new window

0
 

Author Comment

by:vikas_nm
Comment Utility
can you use a common table expression and make a view
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 9

Expert Comment

by:rajeevnandanmishra
Comment Utility
I am not sure for the current case.
0
 

Author Comment

by:vikas_nm
Comment Utility
can somebody please help me on this its very urgent...
0
 
LVL 51

Expert Comment

by:HainKurt
Comment Utility
is this what you want?

with b as (
select 101 ledgerid, 1 divisionid, '1-Apr-11' Closingdate, 10001 opening, null closing
union all select 101,	1,	'2-Apr-11', null, null		
union all select 101,	1,	'3-Apr-11', 2500, null		
union all select 101,	1,	'4-Apr-11', null, 4250	
union all select 101,	1,	'5-Apr-11', null, null
union all select 101,	1,	'6-Apr-11',null,60000
)
select ledgerid, divisionid, Closingdate, opening, closing,
ISNULL(opening, pre_closing) opening2, 
isnull(ISNULL(closing, opening),pre_closing) closing2
from (
select 
*,
(select top 1 isnull(closing, opening) from b b2 where b1.ledgerid=b2.ledgerid and b1.divisionid=b2.divisionid and b2.closingdate<b1.closingdate and isnull(b2.closing, b2.opening) is not null order by Closingdate desc) pre_closing
from b b1
) x

ledgerid	divisionid	Closingdate	opening	closing	opening2	closing2
101	1	1-Apr-11	10001	NULL	10001	10001
101	1	2-Apr-11	NULL	NULL	10001	10001
101	1	3-Apr-11	2500	NULL	2500	2500
101	1	4-Apr-11	NULL	4250	2500	4250
101	1	5-Apr-11	NULL	NULL	4250	4250
101	1	6-Apr-11	NULL	60000	4250	60000

Open in new window

0
 
LVL 9

Expert Comment

by:rajeevnandanmishra
Comment Utility
Good one HainCurt,
Just got reminded about the self joining and written below code to provide the result:
select x.ledgerid, x.divisionid, x.closingdate, isnull(x.opening,x.preclosing) new_opening, 
		isnull(isnull(x.closing,x.opening), preclosing) new_closing 
from (
select a.*, (select top 1 isnull(closing,opening) from t1 b 
		where a.ledgerid = b.ledgerid and a.divisionid = b.divisionid 
		and b.closingdate < a.closingdate and isnull(b.closing,b.opening) is not null 
		order by b.closingdate desc) preclosing 
from t1 a) x 

Open in new window

0
 

Author Comment

by:vikas_nm
Comment Utility
In the above code provide by kurt &  rajeevnandanmishra the criteria used is ISNULL. what if instead of ISNULL i want to use '0'

Sorry for the late reply.
0
 
LVL 9

Expert Comment

by:rajeevnandanmishra
Comment Utility
Hi,

Below code can be used (or some variation in the HainCurt code, i am not good at CTE):
select x.ledgerid, x.divisionid, x.closingdate, 
	(case when isnull(x.opening,0) <> 0 then x.opening else x.preclosing end ) new_opening, 
	(case when isnull(x.closing,0) <> 0 then x.closing 
		when isnull(x.opening,0) <> 0 then x.opening 
		else preclosing end) new_closing 
from (
select a.*, (select top 1 case when isnull(closing,0) = 0 then opening else closing end from t2 b 
		where a.ledgerid = b.ledgerid and a.divisionid = b.divisionid 
		and b.closingdate < a.closingdate and 
		(isnull(b.closing,0) <> 0 or isnull(b.opening,0)<>0)
		order by b.closingdate desc) preclosing 
from t2 a) x

Open in new window


But you might need to consider the case if, there is actually opening/closing is going to be zero.
0
 
LVL 51

Accepted Solution

by:
HainKurt earned 500 total points
Comment Utility
use case if you have 0 instead of nulls

with b as (
select 101 ledgerid, 1 divisionid, '1-Apr-11' Closingdate, 10001 opening, 0 closing
union all select 101,	1,	'2-Apr-11', 0, 0		
union all select 101,	1,	'3-Apr-11', 2500, 0		
union all select 101,	1,	'4-Apr-11', 0, 4250	
union all select 101,	1,	'5-Apr-11', 0, 0
union all select 101,	1,	'6-Apr-11',0,60000
)
select ledgerid, divisionid, Closingdate, opening, closing,
(case when opening != 0 then opening else pre_closing end) opening2, 
(case when closing != 0 then closing when opening != 0 then opening else pre_closing end) closing2
from (
select 
*,
(
select top 1 (case when closing!=0 then closing else opening end) 
from b b2 
where b1.ledgerid=b2.ledgerid and b1.divisionid=b2.divisionid and b2.closingdate<b1.closingdate and (b2.closing!=0 or b2.opening!=0) order by Closingdate desc
) pre_closing
from b b1
) x

ledgerid	divisionid	Closingdate	opening	closing	opening2	closing2
101	1	1-Apr-11	10001	0	10001	10001
101	1	2-Apr-11	0	0	10001	10001
101	1	3-Apr-11	2500	0	2500	2500
101	1	4-Apr-11	0	4250	2500	4250
101	1	5-Apr-11	0	0	4250	4250
101	1	6-Apr-11	0	60000	4250	60000

Open in new window

0
 

Author Closing Comment

by:vikas_nm
Comment Utility
Thanks a lot
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now