SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


End Date Issue


End Date Issue

Author
Message
kiran.rajenimbalkar
kiran.rajenimbalkar
SSC Veteran
SSC Veteran (241 reputation)SSC Veteran (241 reputation)SSC Veteran (241 reputation)SSC Veteran (241 reputation)SSC Veteran (241 reputation)SSC Veteran (241 reputation)SSC Veteran (241 reputation)SSC Veteran (241 reputation)

Group: General Forum Members
Points: 241 Visits: 331
Hi Professional,

I need your help for solving below issue,


CREATE TABLE tmp_end_date
(ID INT,
tgs_rec_eff_dt DATE,
tgs_rec_end_dt DATE,
SEQNUM INT )

GO



insert into tmp_end_date values(12345,'15-Mar-2012',NULL,4)
insert into tmp_end_date values(12345,'10-Feb-2012',NULL,3)
insert into tmp_end_date values(12345,'2-Feb-2012',NULL,2)
insert into tmp_end_date values(12345,'05-Jan-2012',NULL,1)

GO




I want to show end date as like below example.

12345       15-Mar-2012          NULL
12345 10-Feb-2012 14-Mar-2012 (one day less than 15-Mar-2012)
12345 2-Feb-2012 9-Feb-2012 (one day less than 10-Feb-2012)
12345 05-Jan-2012 1-Feb-2012 (one day less than 2-Feb-2012)




But In some cases I have two dates which are same but differ with time then in that case the enddate shoud be as like below example

12345 '2012-10-08 12:59:36'
12345 '2012-10-08 12:59:03' '2012-10-08 12:59:36'


Please suggest me how to do this
ChrisM@home
ChrisM@home
SSCrazy
SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)

Group: General Forum Members
Points: 2737 Visits: 10546
You have two sequence columns in your table - do either of them have gaps? If one of them is guaranteed to have no gaps, then join the table to itself, like this;

SELECT *
FROM Table a
LEFT JOIN Table b ON b.ID = a.ID + 1

Have a look at the results. If you are not sure how to proceed, post back.


Low-hanging fruit picker and defender of the moggies





For better assistance in answering your questions, please read this.




Understanding and using APPLY, (I) and (II) Paul White

Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
kiran.rajenimbalkar
kiran.rajenimbalkar
SSC Veteran
SSC Veteran (241 reputation)SSC Veteran (241 reputation)SSC Veteran (241 reputation)SSC Veteran (241 reputation)SSC Veteran (241 reputation)SSC Veteran (241 reputation)SSC Veteran (241 reputation)SSC Veteran (241 reputation)

Group: General Forum Members
Points: 241 Visits: 331
Hi Thanks for your reply Smile
But its not giving me proper result.
kiran.rajenimbalkar
kiran.rajenimbalkar
SSC Veteran
SSC Veteran (241 reputation)SSC Veteran (241 reputation)SSC Veteran (241 reputation)SSC Veteran (241 reputation)SSC Veteran (241 reputation)SSC Veteran (241 reputation)SSC Veteran (241 reputation)SSC Veteran (241 reputation)

Group: General Forum Members
Points: 241 Visits: 331
I have to update my end date column as like above sample example.
:-)
subbu1
subbu1
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1081 Visits: 695
PLS TRY BELOW CODE:
CREATE TABLE #tmp_end_date
(ID INT,
tgs_rec_eff_dt DATE,
tgs_rec_end_dt DATE,
SEQNUM INT )

GO


insert into #tmp_end_date values(12345,'15-Mar-2012',NULL,4)
insert into #tmp_end_date values(12345,'10-Feb-2012',NULL,3)
insert into #tmp_end_date values(12345,'2-Feb-2012',NULL,2)
insert into #tmp_end_date values(12345,'05-Jan-2012',NULL,1)
;WITH A
AS(SELECT *,DENSE_RANK()OVER(PARTITION BY ID ORDER BY tgs_rec_eff_dt DESC ) RANK FROM #tmp_end_date)
--SELECT * FROM A
SELECT T.ID,T.tgs_rec_eff_dt,(SELECT DATEADD(DD,-1,T1.tgs_rec_eff_dt) FROM A T1
WHERE T1.RANK+1=T.RANK),SEQNUM FROM A T
ORDER BY 2 DESC
kiran.rajenimbalkar
kiran.rajenimbalkar
SSC Veteran
SSC Veteran (241 reputation)SSC Veteran (241 reputation)SSC Veteran (241 reputation)SSC Veteran (241 reputation)SSC Veteran (241 reputation)SSC Veteran (241 reputation)SSC Veteran (241 reputation)SSC Veteran (241 reputation)

Group: General Forum Members
Points: 241 Visits: 331
Hi Expert,

Its worked but failes for below data....when i was inserting and testing

CREATE TABLE #tmp_end_date
(ID INT,
tgs_rec_eff_dt DATE,
tgs_rec_end_dt DATE,
SEQNUM INT )

GO



insert into #tmp_end_date values(12345,'15-Mar-2012',NULL,4)
insert into #tmp_end_date values(12345,'10-Feb-2012',NULL,3)
insert into #tmp_end_date values(12345,'2-Feb-2012',NULL,2)
insert into #tmp_end_date values(12345,'05-Jan-2012',NULL,1)
insert into #tmp_end_date values(33333,'31-Jan-2012',NULL,2)
insert into #tmp_end_date values(33333,'12-Dec-2011',NULL,1)
insert into #tmp_end_date values(1300470,'2012-10-08 12:59:36',NULL,3)
insert into #tmp_end_date values(1300470,'2012-10-08 12:59:03',NULL,2)
insert into #tmp_end_date values(1300470,'1905-07-01',NULL,1)


WITH A
AS(SELECT *,DENSE_RANK()OVER(PARTITION BY ID ORDER BY tgs_rec_eff_dt) RANK FROM #tmp_end_date)
SELECT T.ID,T.tgs_rec_eff_dt,(SELECT DATEADD(DD,-1,T1.tgs_rec_eff_dt) FROM A T1
WHERE T1.RANK+1=T.RANK),SEQNUM FROM A T




Want a cool Sig
Want a cool Sig
Mr or Mrs. 500
Mr or Mrs. 500 (504 reputation)Mr or Mrs. 500 (504 reputation)Mr or Mrs. 500 (504 reputation)Mr or Mrs. 500 (504 reputation)Mr or Mrs. 500 (504 reputation)Mr or Mrs. 500 (504 reputation)Mr or Mrs. 500 (504 reputation)Mr or Mrs. 500 (504 reputation)

Group: General Forum Members
Points: 504 Visits: 705
Here's a tweak to the code above to get rid of the error...
still not sure how to get your 2nd requirement to concatenate the 2 dates if they're on the same day but different timestamp without doing it through an sp... or a table function...


CREATE TABLE #tmp_end_date
(ID INT,
tgs_rec_eff_dt DATETIME,
tgs_rec_end_dt DATETIME,
SEQNUM INT )

GO


insert into #tmp_end_date values(12345,'15-Mar-2012',NULL,4)
insert into #tmp_end_date values(12345,'10-Feb-2012',NULL,3)
insert into #tmp_end_date values(12345,'2-Feb-2012',NULL,2)
insert into #tmp_end_date values(12345,'05-Jan-2012',NULL,1)
insert into #tmp_end_date values(33333,'31-Jan-2012',NULL,2)
insert into #tmp_end_date values(33333,'12-Dec-2011',NULL,1)
insert into #tmp_end_date values(1300470,'2012-10-08 12:59:36',NULL,3)
insert into #tmp_end_date values(1300470,'2012-10-08 12:59:03',NULL,2)
insert into #tmp_end_date values(1300470,'1905-07-01',NULL,1);

WITH A AS(
SELECT *,dense_rank()OVER(partition BY ID ORDER BY tgs_rec_eff_dt DESC ) RANK
FROM #tmp_end_date)
SELECT T.ID
,T.tgs_rec_eff_dt
,(SELECT DATEADD(DD,-1,T1.tgs_rec_eff_dt)
FROM A T1
WHERE T1.RANK+1=T.RANK
and T.ID = T1.ID) End_Date
,SEQNUM
FROM A T
ORDER BY 1,2 DESC

drop table #tmp_end_date



---------------------------------------------------------------
Mike Hahn - MCSomething someday:-)
Right way to ask for help!!
http://www.sqlservercentral.com/articles/Best+Practices/61537/
I post so I can see my avatar Hehe
I want a personal webpage Cool
I want to win the lotto :-D
I want a gf like Tiffa w00t Oh wait I'm married!:-D
kiran.rajenimbalkar
kiran.rajenimbalkar
SSC Veteran
SSC Veteran (241 reputation)SSC Veteran (241 reputation)SSC Veteran (241 reputation)SSC Veteran (241 reputation)SSC Veteran (241 reputation)SSC Veteran (241 reputation)SSC Veteran (241 reputation)SSC Veteran (241 reputation)

Group: General Forum Members
Points: 241 Visits: 331
Thanks Expert..Its working as expected Smile
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search