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


SQL question


SQL question

Author
Message
SQLMAIN
SQLMAIN
SSC-Enthusiastic
SSC-Enthusiastic (160 reputation)SSC-Enthusiastic (160 reputation)SSC-Enthusiastic (160 reputation)SSC-Enthusiastic (160 reputation)SSC-Enthusiastic (160 reputation)SSC-Enthusiastic (160 reputation)SSC-Enthusiastic (160 reputation)SSC-Enthusiastic (160 reputation)

Group: General Forum Members
Points: 160 Visits: 111
I am on SQL 2000 -
I have a table that carries ID and values by change dates (Start date and end date).
Of the many values that can change I just want to pick start and end date for only one of those.
If I pick records for just that one VALUE the records look like this - it can have more than one records where the value does not change because may be some other value column was changing ( which I do not care about)

id----start date---------end date-----------Value
3 2012-04-03 00:00:00 2012-04-06 00:00:00 1
3 2012-04-07 00:00:00 2012-04-10 00:00:00 0
3 2012-04-11 00:00:00 2012-04-13 00:00:00 1
3 2012-04-14 00:00:00 2012-04-14 00:00:00 1 --these are not reqd
3 2012-04-15 00:00:00 2012-05-01 00:00:00 1 --these are not reqd
3 2012-05-02 00:00:00 2012-07-30 00:00:00 0
3 2012-07-31 00:00:00 2012-07-31 00:00:00 1
3 2012-08-01 00:00:00 2012-10-26 00:00:00 1 --these are not reqd
3 2012-10-27 00:00:00 2012-11-19 00:00:00 1 --these are not reqd
3 2012-11-20 00:00:00 2012-11-25 00:00:00 1 --these are not reqd

I have this above data in a table and want to run a SQL that can give me an output as below - giving me only those recs where TYPE is changing along with a new end date for that change.
id----start date---------end date----------Value--new_end_date
3 2012-04-03 00:00:00 2012-04-06 00:00:00 1 2012-04-06 00:00:00
3 2012-04-07 00:00:00 2012-04-10 00:00:00 0 2012-04-10 00:00:00
3 2012-04-11 00:00:00 2012-04-13 00:00:00 1 2012-05-01 00:00:00
3 2012-05-02 00:00:00 2012-07-30 00:00:00 0 2012-07-30 00:00:00
3 2012-07-31 00:00:00 2012-07-31 00:00:00 1 2012-11-25 00:00:00

Here is SQL to create data


drop table test_data
create table test_data (
id int,
start_dt smalldatetime,
end_dt smalldatetime,
type bit
)

insert into test_data values(1, '2012-01-01', '2012-01-15', 0)
insert into test_data values(1, '2012-01-16', '2012-01-20', 0)
insert into test_data values(1, '2012-01-21', '2012-01-25', 1)
insert into test_data values(1, '2012-01-26', '2012-01-31', 0)
insert into test_data values(1, '2012-02-01', '2012-02-15', 1)
insert into test_data values(1, '2012-02-16', '2012-02-25', 1)
insert into test_data values(1, '2012-02-26', '2012-02-26', 1)
insert into test_data values(1, '2012-02-27', '2012-03-05', 0)
insert into test_data values(1, '2012-03-06', '2012-03-15', 0)
insert into test_data values(1, '2012-03-06', '2012-03-17', 0)
insert into test_data values(1, '2012-03-18', '2012-03-18', 1)


insert into test_data values(2, '2012-01-01', '2012-01-09', 0)
insert into test_data values(2, '2012-01-10', '2012-01-11', 0)
insert into test_data values(2, '2012-01-12', '2012-01-17', 1)
insert into test_data values(2, '2012-01-18', '2012-01-20', 1)
insert into test_data values(2, '2012-01-21', '2012-02-09', 1)
insert into test_data values(2, '2012-02-10', '2012-02-19', 0)
insert into test_data values(2, '2012-02-20', '2012-02-21', 1)
insert into test_data values(2, '2012-02-22', '2012-03-03', 1)
insert into test_data values(2, '2012-03-04', '2012-03-19', 1)

insert into test_data values(3, '2012-04-03', '2012-04-06', 1)
insert into test_data values(3, '2012-04-07', '2012-04-10', 0)
insert into test_data values(3, '2012-04-11', '2012-04-13', 1)
insert into test_data values(3, '2012-04-14', '2012-04-14', 1)
insert into test_data values(3, '2012-04-15', '2012-05-01', 1)
insert into test_data values(3, '2012-05-02', '2012-07-30', 0)
insert into test_data values(3, '2012-07-31', '2012-07-31', 1)
insert into test_data values(3, '2012-08-01', '2012-10-26', 1)
insert into test_data values(3, '2012-10-27', '2012-11-19', 1)
insert into test_data values(3, '2012-11-20', '2012-11-25', 1)

I would like the output to look like this below

id----start date----------end date---------Value--new_end_date
1 2012-01-01 00:00:00 2012-01-15 00:00:00 0 2012-01-20 00:00:00
1 2012-01-21 00:00:00 2012-01-25 00:00:00 1 2012-01-25 00:00:00
1 2012-01-26 00:00:00 2012-01-31 00:00:00 0 2012-01-31 00:00:00
1 2012-02-01 00:00:00 2012-02-15 00:00:00 1 2012-02-26 00:00:00
1 2012-02-27 00:00:00 2012-03-05 00:00:00 0 2012-03-17 00:00:00
1 2012-03-18 00:00:00 2012-03-18 00:00:00 1 2012-03-18 00:00:00

2 2012-01-01 00:00:00 2012-01-09 00:00:00 0 2012-01-11 00:00:00
2 2012-01-12 00:00:00 2012-01-17 00:00:00 1 2012-02-09 00:00:00
2 2012-02-10 00:00:00 2012-02-19 00:00:00 0 2012-02-19 00:00:00
2 2012-02-20 00:00:00 2012-02-21 00:00:00 1 2012-03-19 00:00:00

3 2012-04-03 00:00:00 2012-04-06 00:00:00 1 2012-04-06 00:00:00
3 2012-04-07 00:00:00 2012-04-10 00:00:00 0 2012-04-10 00:00:00
3 2012-04-11 00:00:00 2012-04-13 00:00:00 1 2012-05-01 00:00:00
3 2012-05-02 00:00:00 2012-07-30 00:00:00 0 2012-07-30 00:00:00
3 2012-07-31 00:00:00 2012-07-31 00:00:00 1 2012-11-25 00:00:00

Any help will be really appreciated.

Thanks
SQLMAIN
SQLMAIN
SSC-Enthusiastic
SSC-Enthusiastic (160 reputation)SSC-Enthusiastic (160 reputation)SSC-Enthusiastic (160 reputation)SSC-Enthusiastic (160 reputation)SSC-Enthusiastic (160 reputation)SSC-Enthusiastic (160 reputation)SSC-Enthusiastic (160 reputation)SSC-Enthusiastic (160 reputation)

Group: General Forum Members
Points: 160 Visits: 111
Looks like I made the some part of it work.
This SQL will drop the unwanted records and now I have to somehow add the new end dates.

delete A
from test_data A
join test_data b
on a.id = b.id
and a.type = b.type
and a.start_dt = b.end_dt + 1
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