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


How can I create intervals fro start-dates only


How can I create intervals fro start-dates only

Author
Message
Peter Pirker
Peter Pirker
SSC Journeyman
SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)

Group: General Forum Members
Points: 97 Visits: 221
Let's assume we have

1) A key value, like a product number
2) and a ValidFrom date, like this

MyProductKey, 20100111
MyProductKey, 20110205
MyProductKey, 20120101

What we want is the following

MyProductKey, 20100111, 20110204
MyProductKey, 20110205, 20111231
MyProductKey, 20120101, NULL

What would be the simplest way to do this?

TIA

Peter
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)

Group: General Forum Members
Points: 25988 Visits: 17528
Can you post ddl and sample data so we don't have to guess on what your tables look like? Please take a few minutes and read the first article in my signature for best practices when posting questions.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Erin Ramsay
Erin Ramsay
SSC Eights!
SSC Eights! (817 reputation)SSC Eights! (817 reputation)SSC Eights! (817 reputation)SSC Eights! (817 reputation)SSC Eights! (817 reputation)SSC Eights! (817 reputation)SSC Eights! (817 reputation)SSC Eights! (817 reputation)

Group: General Forum Members
Points: 817 Visits: 1099
with much information I had to make some guesses.. something like this?

CREATE TABLE #temp 
(
id INT,
fromdate DATE
)

INSERT INTO #temp
VALUES (1,
'20100111'),
(1,
'20110205'),
(1,
'20120101');

WITH cte
AS (SELECT id,
fromdate,
Row_number()
OVER (
partition BY id
ORDER BY fromdate ASC) rownum
FROM #temp)
SELECT cte.id,
cte.fromdate,
Dateadd(d, -1, cte2.fromdate)
FROM cte
LEFT JOIN cte cte2
ON cte.rownum = cte2.rownum - 1

DROP TABLE #temp


Peter Pirker
Peter Pirker
SSC Journeyman
SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)

Group: General Forum Members
Points: 97 Visits: 221
Yes, that's exactly what I was trying to achieve.
Thank you very much for your help.
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