Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
BendJoe
Posting Yak Master
128 Posts |
Posted - 2010-05-06 : 17:03:40
|
| I have a column of datetime type. eg:2010-03-30 08:17:51.4902010-03-30 08:18:26.110...................................2010-03-30 16:25:06.1802010-03-30 16:25:42.1302010-03-31 08:18:14.6702010-03-31 08:19:12.830..........................................2010-03-31 11:02:04.630.I need a way to find out the start times and end times of each day in the column.How can this be done. |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2010-05-06 : 17:10:04
|
| [code]-- Sample DataDECLARE @MyTable TABLE (MyDate DATETIME)INSERT @MyTableSELECT '2010-03-30 08:17:51.490'UNION ALL SELECT '2010-03-30 08:18:26.110'UNION ALL SELECT '2010-03-30 16:25:06.180'UNION ALL SELECT '2010-03-30 16:25:42.130'UNION ALL SELECT '2010-03-31 08:18:14.670'UNION ALL SELECT '2010-03-31 08:19:12.830'UNION ALL SELECT '2010-03-31 11:02:04.630'-- Run QuerySELECT DATEADD(DAY, DATEDIFF(DAY, 0, MyDate), 0) AS [Day], MIN(MyDate) AS MinDate, MAX(MYDate) AS MaxDateFROM @MyTableGROUP BY DATEADD(DAY, DATEDIFF(DAY, 0, MyDate), 0)[/code] |
 |
|
|
BendJoe
Posting Yak Master
128 Posts |
Posted - 2010-05-06 : 17:29:19
|
quote: Originally posted by Lamprey
-- Sample DataDECLARE @MyTable TABLE (MyDate DATETIME)INSERT @MyTableSELECT '2010-03-30 08:17:51.490'UNION ALL SELECT '2010-03-30 08:18:26.110'UNION ALL SELECT '2010-03-30 16:25:06.180'UNION ALL SELECT '2010-03-30 16:25:42.130'UNION ALL SELECT '2010-03-31 08:18:14.670'UNION ALL SELECT '2010-03-31 08:19:12.830'UNION ALL SELECT '2010-03-31 11:02:04.630'-- Run QuerySELECT DATEADD(DAY, DATEDIFF(DAY, 0, MyDate), 0) AS [Day], MIN(MyDate) AS MinDate, MAX(MYDate) AS MaxDateFROM @MyTableGROUP BY DATEADD(DAY, DATEDIFF(DAY, 0, MyDate), 0)
ThanksI need a bit more help is there a way to add the seconds in the following query in a single statement.SELECT DATEADD(DAY, DATEDIFF(DAY, 0, MyDate), 0) AS [Day], MIN(MyDate) AS MinDate, MAX(MYDate) AS MaxDate, DATEDIFF(ss,MIN(MyDate),Max(MyDate)) as seconds, FROM @MyTableGROUP BY DATEADD(DAY, DATEDIFF(DAY, 0, MyDate), 0) |
 |
|
|
vijayisonly
Master Smack Fu Yak Hacker
1836 Posts |
Posted - 2010-05-06 : 17:44:15
|
| This seems right. Are you getting an error? |
 |
|
|
BendJoe
Posting Yak Master
128 Posts |
Posted - 2010-05-06 : 18:01:23
|
quote: Originally posted by vijayisonly This seems right. Are you getting an error?
Sorry I did not explain properly. The above query runs fine and it gives the seconds in rows depending on the number of days spanned.I have a select statementselect a,b,dbo.func_ConvertSeconds(datediff(ss,MIN(MyDate),MAX(MyDate))) as Timegroup by a.The problem is between the min(mydate) and max(mydate) there are break in time. for example people start work in the morning and end in the evening and again start next morning and so on. I need to add all the time they were working not the break time.Hope I am made it clear. |
 |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2010-05-07 : 13:22:26
|
| How can you distinguish between a Start and an End date? |
 |
|
|
BendJoe
Posting Yak Master
128 Posts |
Posted - 2010-05-07 : 14:12:12
|
quote: Originally posted by Lamprey How can you distinguish between a Start and an End date?
start date would be the date on the min(mydate) end date will be from max(mydate). |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-05-07 : 14:18:40
|
| can you post your data as in table along with people info? then it will be much help to somebody trying to help you out------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2010-05-07 : 15:04:50
|
quote: Originally posted by BendJoe
quote: Originally posted by Lamprey How can you distinguish between a Start and an End date?
start date would be the date on the min(mydate) end date will be from max(mydate).
Ok, Then how do you determine which Seconds should be subtracted? Or should not be included in the difference between the start and end dates?Jsut a guess, but I suspect that you mean something like this, based on the sample data you provided. If you can clarify, we can probably help.2010-03-30 08:17:51.490 = Start - Difference between (08:18:26.110 & 08:18:26.110) = 35 Sec2010-03-30 08:18:26.110 = End - Do nothing2010-03-30 16:25:06.180 = Start - Difference between (16:25:06.180 & 16:25:42.130) = 36 Sec2010-03-30 16:25:42.130 = End - Do Nothing2010-03-31 08:18:14.670 = Start - Difference between (16:25:06.180 & 16:25:42.130) = 58 Sec2010-03-31 08:19:12.830 = End - Do Nothing2010-03-31 11:02:04.630 = Start.. No End.. so 0? * Just a guess, |
 |
|
|
BendJoe
Posting Yak Master
128 Posts |
Posted - 2010-05-08 : 12:05:39
|
| Table 1,ProcessID,ProcessName..Table 2,TaskID, ProcessID, TaskName,TaskDateTime..I want to find the amount of time to complete the Process. Process is made up of several tasks. eg: At times a process gets completed in a day so I can take the difference in Max(taskDateTime) and Min(taskDateTime).But when a task get spanned across several days I have to take into consideration the break time from evening to nextday morning.Process Table:1,Process1....2.Process2....Task Table1,1,Task1forProcess1,2010-05-05 8:30:00:0002,1,Task2forProcess1,2010-05-05 8:45:00:000...........................................n-1,1,taskn-1forProcess1,2010-05-05 4:50:00:000n,1,TasknforProcess1,2010-05-06 9:00:00:000.........................................m,1,TaskmforProcess1,2010-05-06 11:30:00:000m+1,2,task1forProcess2, 2010-05-06 12:30:00:000................................I am using this selecSelect p.ProcessID,p.ProcessName,datediff(ss,MIN(t.TaskDateTime),MAX(t.TaskdateTime))from process p inner join tasks t on p.processid=t.processidgroup by processidThis will not take into account the break between the last tasktime and the first task time next day. I am trying to find a way to make the query account for the break time. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-05-09 : 02:58:36
|
| you've only a single date value stored against each of tasks of process. then how will you know when that task got over? or is it that task ran all the way till start of next task? then question of break time wont appear at all.------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
BendJoe
Posting Yak Master
128 Posts |
Posted - 2010-05-09 : 13:48:43
|
quote: Originally posted by visakh16 you've only a single date value stored against each of tasks of process. then how will you know when that task got over? or is it that task ran all the way till start of next task? then question of break time wont appear at all.------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
The end time of a task is the start time of the following task.That is not very important. I want the time to finish a process. The start time of the process will be the datetime of the first task in that process and the finish time will be the datetime of the final task within that process. min(datetime) and max(datetime) can get those values. But need to eliminate the break time if a task spans over multiple days. Is there a way to split the grouping on processid into subgroups grouped by the day part of the datetime column. And then add the timeof those sub groups. |
 |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-05-10 : 13:09:07
|
quote: Originally posted by BendJoeThe end time of a task is the start time of the following task.
then what does below mean? I am trying to find a way to make the query account for the break time.how will break come if tasks are continuos?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
BendJoe
Posting Yak Master
128 Posts |
Posted - 2010-05-10 : 21:01:54
|
| CREATE TABLE [dbo].[Process]( [ProcessID] [int] NOT NULL, [Processname] [varchar](50) NOT NULL, CONSTRAINT [PK_Process] PRIMARY KEY CLUSTERED ( [ProcessID] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY]CREATE TABLE [dbo].[Tasks]( [TaskID] [int] NOT NULL, [ProcessID] [int] NOT NULL, [TaskName] [nvarchar](50) NOT NULL, [ProcessDateTime] [datetime] NOT NULL, CONSTRAINT [PK_Tasks] PRIMARY KEY CLUSTERED ( [TaskID] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY]GOALTER TABLE [dbo].[Tasks] WITH CHECK ADD CONSTRAINT [FK_Tasks_Tasks] FOREIGN KEY([ProcessID])REFERENCES [dbo].[Process] ([ProcessID])GOALTER TABLE [dbo].[Tasks] CHECK CONSTRAINT [FK_Tasks_Tasks]INSERT INTO [dbo].[Process] ([ProcessID] ,[Processname]) select 1,'Process1' union all select 2,'Process2' GOINSERT INTO [dbo].[Tasks] ([TaskID] ,[ProcessID] ,[TaskName] ,[ProcessDateTime]) select 1,1,Task1Process1,'2010-03-30 08:17:51.490' union all select 2,1,Task2Process1,'2010-03-30 08:18:26.110' union all select 3,1,Task3Process1,'2010-03-30 16:30:00:000' union all select 4,1,Task4Process1,'2010-03-31 08:00:00:000' union all select 5,1,Task5Process1,'2010-03-31 13:00:00:000' union all select 1,2,Task1Process2,'2010-03-31 14:17:51.490' union all select 2,2,Task2Process2,'2010-03-31 16:18:26.110' union all select 3,2,Task3Process2,'2010-03-31 16:30:00:000' union all select 4,2,Task4Process2,'2010-04-01 08:00:00:000' union all select 5,2,Task5Process2,'2010-04-01 13:00:00:000' GOI need the time yaken to complete a process.Assumptions1. the timestamp of the first task in a process is the start time of a process.2 the timestamp of the last task in a process is the finish time of a process.3.If a process spans more than one day then the time between the last task within that process on a given day and the first task that happens on the following day within that process needs to be omitted from the process completion time calculation.How can this be done in T-sql |
 |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2010-05-11 : 10:54:20
|
| What is your expected output? FYI, your test data produces several errors. |
 |
|
|
BendJoe
Posting Yak Master
128 Posts |
Posted - 2010-05-11 : 19:07:51
|
quote: Originally posted by Lamprey What is your expected output? FYI, your test data produces several errors.
ProcessID,ProcessName and time to complete the process. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-05-12 : 10:52:10
|
| [code]select Processname,CAST(LEFT(CONVERT(varchar(8),DATEDIFF(dd,0,DATEADD(ss,SUM([Time]),0)),108),2) *24+ LEFT(CONVERT(varchar(8),DATEADD(ss,SUM([Time]),0),108),2)*1 AS varchar(10)) + ':'+RIGHT(CONVERT(varchar(8),DATEADD(ss,SUM([Time]),0),108),5)from(select p.Processname,DATEADD(dd,DATEDIFF(dd,0,t.ProcessDateTime),0) AS [Date],DATEDIFF(ss,MIN(t.ProcessDateTime),MAX(t.ProcessDateTime)) AS [Time]from Process pJOIN Tasks tON t.ProcessID = p.ProcessIDGROUP BY p.Processname,DATEADD(dd,DATEDIFF(dd,0,t.ProcessDateTime),0))rGROUP BY Processname[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2010-05-12 : 11:06:56
|
quote: Originally posted by BendJoe
quote: Originally posted by Lamprey What is your expected output? FYI, your test data produces several errors.
ProcessID,ProcessName and time to complete the process.
By expected output, I mean what are the ACTUAL results you expect to get back given the sample data you provided?My guess would be the code that Visakh has provided, but it seems like you'd loose senconds when a task spans over the day boundry (midnight). |
 |
|
|
|
|
|
|
|