Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Updating multiple records from the previous row

Posted on 2012-03-29
16
Medium Priority
?
288 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
  • 4
16 Comments
 
LVL 59

Expert Comment

by:HainKurt
ID: 37783868
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 59

Expert Comment

by:HainKurt
ID: 37783878
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
ID: 37783897
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:vikas_nm
ID: 37784016
i want a view for the same.
0
 
LVL 9

Expert Comment

by:rajeevnandanmishra
ID: 37784017
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
ID: 37784051
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
ID: 37784146
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
ID: 37784162
can you use a common table expression and make a view
0
 
LVL 9

Expert Comment

by:rajeevnandanmishra
ID: 37784221
I am not sure for the current case.
0
 

Author Comment

by:vikas_nm
ID: 37784232
can somebody please help me on this its very urgent...
0
 
LVL 59

Expert Comment

by:HainKurt
ID: 37784349
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
ID: 37784598
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
ID: 37785654
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
ID: 37786047
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 59

Accepted Solution

by:
HainKurt earned 2000 total points
ID: 37788072
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
ID: 37802858
Thanks a lot
0

Featured Post

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

722 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