views:

49

answers:

5

I have a table of data. I have a field which shows date. I had set this column as Start Date. I want to create an additional column as End Date, where the End Date will be the Start Date of the next row. Can you give me a query of creating the End Date by taking the data of the Start Date in next row ?

+1  A: 

Depends on what you mean by "next" row.

Can you provide sample dataset, and specify how you determine what order the rows go in?

EDIT

Your record order really does matter -- you're going to have to determine what that is. For now, I'm working off of the assumption that ordering it by start_date is acceptable.

--GET the first relevant start date
declare @start datetime
set @start = select MIN(start_date) from table

declare @end datetime
set @end = @start

WHILE @end is not null

  --GET the next relevant end date
  SET @end = select MIN(start_date) from table where start_date > @start

  --Update the table with the end date
  UPDATE table
  SET end_date = @end
  WHERE start_date = @start

  --GET the next relevant start date
  SET @start = @end

END
dave
please check the first answer. The sample output is given.
Sreejesh Kumar
*first* answer, eh? :-) they change order all the time. Anyhoo -- i edited my answer above, based on the assumption that the "next" row is determined by start_date order.
dave
+2  A: 

First of all, you have to come up with a definition of "order", since rows in a table are stored without any order.

When you know what your order is, you can create a stored procedure that goes:

insert into the_table (new_id, start_date) values (@id, @start_date);

update the_table
set end_date = @start_date
where id = <the id determined by your sorting rule>;
GSerg
A: 

What about last row? The endDate will be blank for that?

Sachin Shanbhag
Thats ok......I want the result just like above in the First answer made to my question. What is the query to calculate the end date ?
Sreejesh Kumar
+1  A: 

Assuming you already have your columns and that you have an Auto-Incrementing Primary Key:

Update T1
Set T1.EndDate = T2.StartDate
From [Table] T1
Inner Join [Table] T2 on T1.Id = T2.Id - 1
AllenG
this does not consider the auto incremented key...I have to get the enddate from the start date of next row
Sreejesh Kumar
Well thanks....This worked out for me !!!!
Sreejesh Kumar
+1  A: 

I'm assuming that you currently have rows with values such as

StartDate
---------
1 Jan 1990
2 June 1998
4 September 2006

And you want to change to

StartDate              EndDate
---------              ------------
1 Jan 1990             2 June 1998
2 June 1998            4 September 2006
4 September 2006       NULL

Quite apart from the redundancy and maintenance issue that reminds me of this question where such a setup with correlated columns actually caused the original poster problems when querying data. (I prefer Unreason's answer to my own on that question!)

Why do you need to add the EndDate column? It will probably be possible to come up with a query that works without it.

Edit After much faffing about with row_number() I actually couldn't find a query with a better plan than this. (Assumes index on StartDate)

SELECT 
        id,
        StartDate, 
           (SELECT MIN(StartDate) 
            FROM  testTable t2 
            WHERE t2.StartDate > t1.StartDate) AS EndDate
FROM testTable t1
Martin Smith
Yes this is the output which I want.In my work, we need this end date.
Sreejesh Kumar
and how will get it ?
Sreejesh Kumar
@sreejesh - What type of query are you doing? Are you bringing back a range of results or looking up an individual record?
Martin Smith
results......just like the above posted by you
Sreejesh Kumar
Any solution for this ?
Sreejesh Kumar