|
|
 |
| SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the SQL Server 2000 section of the Wrox p2p Programmer to Programmer discussion community. This is a community of more than 40,000 computer programmers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining our free Wrox p2p community you can post your own programming questions and respond to other programmers’ questions. Registered users also don't have to see the ads that are displayed to guests. Registration is fast, simple and absolutely free so please, join today!
Join today and post to win prizes! Post more to increase your chances of being Wrox’s top poster of the month.
|
 |

March 9th, 2007, 05:03 PM
|
|
Registered User
|
|
Join Date: Mar 2007
Location: Dunmore, Pa, USA.
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
compare values in one row to the next
help, help!!! I am trying to write sql script that will get the datediff from the bl_term_dt of the 1st row and the bl_eff_dt of the 2nd row. then get the datediff of the bl_term_dt of the 2nd row and bl_eff_dt of the 3rd row and so on. Any suggestions?
bl_ck bl_eff_dt bl_term_dt
52790 2006-01-01 00:00:00.000 2006-03-31 00:00:00.000
52790 2006-04-01 00:00:00.000 2006-06-30 00:00:00.000
52790 2006-07-01 00:00:00.000 2007-01-31 00:00:00.000
52790 2007-02-01 00:00:00.000 9999-12-31 00:00:00.000
|

March 12th, 2007, 11:34 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Location: Bangalore, KA, India.
Posts: 2,477
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
How about a Cursor...?
_________________________
- Vijay G
Strive for Perfection
|

March 14th, 2007, 02:45 PM
|
|
Friend of Wrox
|
|
Join Date: Aug 2004
Location: Orange County, CA, USA.
Posts: 385
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Happyqv has the right idea. Select the data into a cursor in the particular order your looking for. Then step through the data row by row. Saving the previous rows data into variables. Then compare if the variable (previous row value) is the same as the current row value.
(Just expanding on happyqv's comment)
|

March 14th, 2007, 05:46 PM
|
|
Friend of Wrox
|
|
Join Date: Aug 2004
Location: Orange County, CA, USA.
Posts: 385
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I just posted a similar cursor related question. If you want take a look at that code in the other post.
|

March 15th, 2007, 08:13 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Location: Bangalore, KA, India.
Posts: 2,477
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Rob, it would help him better if you posted the link of that thread here...
In fact it is not happyQv, it is happyGv stands for "G Vijay" ;) no harm in that being mispelt
Cheers.
_________________________
- Vijay G
Strive for Perfection
|

March 20th, 2007, 09:47 AM
|
|
Registered User
|
|
Join Date: Mar 2007
Location: Dunmore, Pa, USA.
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thanks Guys I appreciate you help....
|

March 20th, 2007, 10:14 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Location: Bangalore, KA, India.
Posts: 2,477
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
So, did you manage to get it solved? If so what is that you did to accomplish that? How about sharing it here.
cheers.
_________________________
- Vijay G
Strive for Perfection
|

March 22nd, 2007, 05:45 PM
|
|
Friend of Wrox
|
|
Join Date: Aug 2004
Location: Orange County, CA, USA.
Posts: 385
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
-- Try this logic.
Create table datecompare (bl_ck int, bl_eff_dt datetime, bl_term_dt datetime)
go
insert into datecompare (bl_ck, bl_eff_dt, bl_term_dt) values (52790, convert(datetime,'2006-01-01 00:00:00.000'), convert(datetime,'2006-03-31 00:00:00.000'))
insert into datecompare (bl_ck, bl_eff_dt, bl_term_dt) values (52790, convert(datetime,'2006-04-01 00:00:00.000'), convert(datetime,'2006-06-30 00:00:00.000'))
insert into datecompare (bl_ck, bl_eff_dt, bl_term_dt) values (52790, convert(datetime,'2006-07-01 00:00:00.000'), convert(datetime,'2007-01-31 00:00:00.000'))
insert into datecompare (bl_ck, bl_eff_dt, bl_term_dt) values (52790, convert(datetime,'2007-02-01 00:00:00.000'), convert(datetime,'9999-12-31 00:00:00.000'))
go
Declare @Prev_bl_eff_dt datetime
Declare @Prev_bl_term_dt datetime
Declare @Cur_bl_ck int
Declare @Cur_bl_eff_dt datetime
Declare @Cur_bl_term_dt datetime
Declare @PrintMe Varchar(1200)
DECLARE X_Cursor CURSOR FOR
select bl_ck, bl_eff_dt, bl_term_dt from datecompare order by bl_ck, bl_eff_dt, bl_term_dt
OPEN X_Cursor
FETCH NEXT FROM X_Cursor INTO @Cur_bl_ck, @Cur_bl_eff_dt, @Cur_bl_term_dt
set @Prev_bl_eff_dt = @Cur_bl_term_dt
-- set @Prev_bl_term_dt = @Cur_bl_term_dt
WHILE @@FETCH_STATUS = 0
BEGIN
set @PrintMe = 'Current bl_ck = ' + convert(char(24),@Cur_bl_ck) + '||'
set @PrintMe = @PrintMe + 'Current bl_eff_dt = ' + convert(char(12),@Cur_bl_eff_dt,101) + '||'
set @PrintMe = @PrintMe + 'Current bl_eff_dt = ' + convert(char(12),@Cur_bl_eff_dt,101) + '||'
set @PrintMe = @PrintMe + 'Current bl_eff_dt = ' + convert(char(12),@Cur_bl_term_dt,101) + '||'
set @PrintMe = @PrintMe + 'Date Delta = ' + convert(char(12),dateDiff(day,@Prev_bl_term_dt,isn ull(@Cur_bl_eff_dt,@Prev_bl_term_dt))) + '||'
set @Prev_bl_eff_dt = @Cur_bl_eff_dt
set @Prev_bl_term_dt = @Cur_bl_term_dt
Print @Printme
FETCH NEXT FROM X_Cursor INTO @Cur_bl_ck, @Cur_bl_eff_dt, @Cur_bl_term_dt
END
CLOSE X_Cursor
DEALLOCATE X_Cursor
go
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
 |