Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Manipulate End Dates in a Migration Plan like Simulation

Posted on 2013-11-22
3
Medium Priority
?
147 Views
Last Modified: 2014-05-12
Hello together,

let me borrow your brains for a minute - i would like to do the following Simulation in Sql.
Assume you have leased 5 cars to a company.
Now every month you would like to replace the oldest 2 cars with new ones.

Sample Current Data:
[ID]     [Booking Date]     [Older 36 Month Date]
1          2010-01-01           2013-01-01
2          2010-01-01           2013-01-01
3          2010-01-01           2013-01-01
4          2010-01-01           2013-01-01
5          2010-02-01           2013-02-01


Sample Output new Data:
[ID]     [Booking Date]     [Older 36 Month Date]
1          2010-01-01           2013-01-01
2          2010-01-01           2013-01-01
3          2010-01-01           2013-02-01
4          2010-01-01           2013-02-01
5          2010-02-01           2013-03-01

As you can see i would like to manipulate the [Older 36 Month Date] for 2 cars at a time.
How would i go about this in SQL?
What would be your solution?
0
Comment
Question by:T-Virus
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 50

Assisted Solution

by:Lowfatspread
Lowfatspread earned 1000 total points
ID: 39668619
you mean

update x
  set [older 36 month date] = dateadd(mm,2,[older 36 month date])
 from (select companyid , [older 36 month date]
              , row_number() over (partition by companyid
                            order by [older 36 month date],[id]) as rn) as x
 where companyid=?
   and rn in (1,2)
or
Select x.*
  , [new older 36 month date] = dateadd(mm,2,[older 36 month date])
 from (select companyid , [older 36 month date]
              , row_number() over (partition by companyid
                            order by [older 36 month date],[id]) as rn) as x
 where companyid=?
   and rn in (1,2)
 order by companyid,id
0
 
LVL 34

Accepted Solution

by:
Brian Crowe earned 1000 total points
ID: 39669203
Personally I prefer common table expressions but it boils down to the same logic...

WITH cte (ID, [Booking Date], [Older 36 Month Date], RowNumber
AS
(
   SELECT ID, [Booking Date], [Older 36 Month Date],
      ROW_NUMBER() OVER(ORDER BY [Older 36 Month Date] DESC) AS RowNumber
)
UPDATE cte
SET [Older 36 Month Date] = ???
WHERE RowNumber IN (1, 2)
0
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 39669359
@bricrowe

  ROW_NUMBER() OVER(ORDER BY [Older 36 Month Date] DESC)

shouldn't that be asc?

he wants to update the "oldest" , rather than "latest"?

but yes i agree simple CTE can read better...
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question