Click here to Skip to main content
15,880,503 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi Experts,

I have data related to schedules in one of the table as following.


SQL
ScheduledID    StartTime  EndTime   ModifiedTimeStamp

1              10 AM      6 PM      01-01-2014 09:00
1              11 AM      6 PM      01-01-2014 09:30
1              11 AM      7 PM      01-01-2014 10:00
1              10 AM      6 PM      01-01-2014 10:30
1              11 AM      8 PM      01-01-2014 11:00


The schedule start and end time is modifies several times as above.

I need to display the above data as following.


SQL
ScheduledID    StartTime  EndTime   ModifiedTimeStamp   Action Type

1              10 AM      6 PM      01-01-2014 09:00    Schedule Created
1              11 AM      6 PM      01-01-2014 09:30    Start Time Modified from 10AM to 11AM
1              11 AM      7 PM      01-01-2014 10:00    End Time Modified from 6PM to 7PM
1              10 AM      6 PM      01-01-2014 10:30    Start Time Modified from 11AM to 10AM, End Time Modified from 7PM to 6PM
1              11 AM      8 PM      01-01-2014 11:00    Current Schedule




Please guide me how can i achieve this?

Thank you.
Posted
Updated 23-Nov-14 2:36am
v4
Comments
André Kraak 23-Nov-14 4:35am    
Are you sure the ModifiedTimeStamp of the last two records is correct?
Based on your required output it looks like the time of these records should be after 10:00.
Durgaprasad Guduri 23-Nov-14 8:37am    
@Andre Kraak, updated the Last Modified Timestamps to correct values.

1 solution

As Andre pointed out your input data seems to be incorrect. I took the liberty of changing the modified time stamps for the last two records. Now to get the results you need, first assign a sequence number based on the time stamp. Descending order will help you identify the current schedule easily. You the need to wrap it in a CTE (common table expression) and join to itself on the sequence to get the previous record:
SQL
with cte as
(
  select *, row_number() over(partition by scheduleid order by ModifiedTimeStamp desc) seq 
  from schedule
)
select curr.ScheduleID, curr.StartTime, curr.EndTime, curr.ModifiedTimeStamp,
  case
    when prev.seq is null then 'Schedule Created'
    when curr.seq = 1 then 'Current Schedule'
    else dbo.uf_GetModifiedMessage(curr.StartTime, prev.StartTime, curr.EndTime, prev.EndTime)
  end as ActionType
from cte curr
left join cte prev on curr.seq = prev.seq - 1
order by curr.ModifiedTimeStamp

To get the modified message I would prefer a function. You could do it inline but the query would be a mess:
SQL
create function dbo.uf_GetModifiedMessage(
  @currStartTime varchar(10), 
  @prevStartTime varchar(10),
  @currEndTime varchar(10), 
  @prevEndTime varchar(10)
)
returns varchar(1000)
as
begin
  declare @message varchar(1000) = NULL

  if @currStartTime <> @prevStartTime
    set @message = 'Start time modified from '+@prevStartTime+' to '+@currStartTime

  if @currEndTime <> @prevEndTime
    set @message = isnull(@message+', ','')+'End time modified from '+@prevEndTime+' to '+@currEndTime

  return @message
end

I created a fiddle[^] too, check it out.
 
Share this answer
 
Comments
Durgaprasad Guduri 23-Nov-14 8:48am    
@Tomas Takac, You are Awesome...!!! Thanks a Ton...!!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900