| Author |
Topic |
|
mickdunde
Starting Member
4 Posts |
Posted - 2007-11-01 : 03:05:51
|
Hi thereI have a table which looks like follows:Date | TimeStart | TimeEnd | Activity1.1.2007 | 07:30 | 09:00 | a1.1.2007 | 09:00 | 10:00 | b1.1.2007 | 10:15 | 12:00 | cI'm looking now for a query which outputs like the following:1.1.2007 | 07:30 | 10:001.1.2007 | 10:15 | 12:00First two rows are "grouped" together as b follows inmediately after a, but between b and c there is a break between, so new row.Min and Max, grouped by date does not work here, as then SQL does not recognize the break in between.Also the following does not work: select * from tbl_test twhere t.TimeEnd not in (select t2.TimeStart from tbl_test t2 where t2.Date = t.Date) Anyone can help? |
|
|
AndrewMurphy
Master Smack Fu Yak Hacker
2916 Posts |
Posted - 2007-11-01 : 05:08:07
|
| select * from tbl_test t1left join tbl_test t2 on t1.date = t2.date and t1.timeend = t2.timestartwhere t2.timestart is null--maybe with this as wellunion select * from tbl_test t1left join tbl_test t2 on t1.date = t2.date and t1.timestart = t2.timeendwhere t2.timestart is null |
 |
|
|
mickdunde
Starting Member
4 Posts |
Posted - 2007-11-01 : 06:17:44
|
| Thanks AndrewBut this only works partially, if I have like three or more in entries per period, say:Date | TimeStart | TimeEnd | Activity1.1.2007 | 07:30 | 09:00 | a1.1.2007 | 09:00 | 10:00 | b1.1.2007 | 10:00 | 11:00 | c1.1.2007 | 11:00 | 12:00 | d1.1.2007 | 12:30 | 14:00 | ethen only the first two are recognized, from 07:30 to 10:00 |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-11-01 : 06:20:42
|
quote: Originally posted by mickdunde Thanks AndrewBut this only works partially, if I have like three or more in entries per period, say:Date | TimeStart | TimeEnd | Activity1.1.2007 | 07:30 | 09:00 | a1.1.2007 | 09:00 | 10:00 | b1.1.2007 | 10:00 | 11:00 | c1.1.2007 | 11:00 | 12:00 | d1.1.2007 | 12:30 | 14:00 | ethen only the first two are recognized, from 07:30 to 10:00
please also post the corresponding required result KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
mickdunde
Starting Member
4 Posts |
Posted - 2007-11-01 : 06:44:55
|
| sureresult of the above1.1.2007 | 07:30 | 12:001.1.2007 | 12:30 | 14:00Because activity a to d are done straight without a break, but between end of activity d and start of e there is a break of 30 min. |
 |
|
|
AndrewMurphy
Master Smack Fu Yak Hacker
2916 Posts |
Posted - 2007-11-01 : 07:36:26
|
| I'd take a guess and say that implementing this in a "sophisticated" front-end rather than in raw T-SQL would be simpler. You're looking at a recursive query otherwise....which is difficult and resource intensive...and may not work. |
 |
|
|
mickdunde
Starting Member
4 Posts |
Posted - 2007-11-01 : 08:01:44
|
| I came to a similar conclusion after some hours without success. But thanks for your help! |
 |
|
|
PeterNeo
Constraint Violating Yak Guru
357 Posts |
Posted - 2007-11-01 : 09:57:36
|
| Hi Try this,DECLARE @T TABLE ( Date DATETIME, TimeStart DATETIME, TimeEnd DATETIME, Activity CHAR(1) )INSERT INTO @TSELECT '1.1.2007', '07:30', '09:00', 'a' UNION ALLSELECT '1.1.2007', '09:00', '10:00', 'b' UNION ALLSELECT '1.1.2007', '10:00', '11:00', 'c' UNION ALLSELECT '1.1.2007', '11:00', '12:00', 'd' UNION ALLSELECT '1.1.2007', '12:30', '14:00', 'e'--SELECT * FROM @TDECLARE @TimeEnd TABLE(ID INT IDENTITY(1,1), Date DATETIME, TimeEnd DATETIME)INSERT INTO @TimeEndSELECT DISTINCT T.Date, T.TimeEndFROM @T TLEFT JOIN @T T1 ON T1.TimeStart = T.TimeEndWHERE T1.TimeStart IS NULL--SELECT * FROM @TimeEndDECLARE @TimeFrom TABLE(ID INT IDENTITY(1,1), Date DATETIME, TimeStart DATETIME)INSERT INTO @TimeFromSELECT DISTINCT T.Date, T.TimeStartFROM @T TLEFT JOIN @T T1 ON T1.TimeEnd = T.TimeStartWHERE T1.TimeEnd IS NULL--SELECT * FROM @TimeFromSELECT T.Date, T.TimeStart, T1.TimeEndFROM @TimeFrom TINNER JOIN @TimeEnd T1 ON T.ID = T1.ID |
 |
|
|
|