Solved

Updating multiple records from the previous row

Posted on 2012-03-29
16
287 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 56

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 56

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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 

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 56

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 56

Accepted Solution

by:
HainKurt earned 500 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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
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…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

630 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