| Author |
Topic |
|
Togaspoon
Starting Member
42 Posts |
Posted - 2009-06-19 : 09:48:08
|
| Is it possible to join 2 rows into one when the end and start times are the same.ExampleUserId TypeId StartDate EndDate1 1 1/1/2009 8:00:00 AM 1/1/2009 3:00:00 PM1 2 1/1/2009 3:00:00 PM 1/1/2009 5:00:00 PMI would like to get this:UserId StartDate EndDate1 1/1/2009 8:00:00 AM 1/1/2009 5:00:00 PM |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-06-19 : 10:05:34
|
[code]select UserId, min(StartDate), max(EndDate)from yourtablegroup by UserId[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2009-06-19 : 10:09:48
|
| You can do a self-joinJimDECLARE @Table TABLE (UserId int, TypeId int, StartDate datetime ,EndDate datetime)INSERT INTO @tableSELECT 1, 1, '1/1/2009 8:00:00 AM', '1/1/2009 3:00:00 PM' UNION ALLSELECT 1, 2, '1/1/2009 3:00:00 PM ','1/1/2009 5:00:00 PM'/*1 1/1/2009 8:00:00 AM 1/1/2009 5:00:00 PM*/SELECT a.UserID,a.StartDate,b.EndDateFROM @table aINNER JOIN @table bON a.userid = b.useridand a.enddate = b.startdate |
 |
|
|
Togaspoon
Starting Member
42 Posts |
Posted - 2009-06-19 : 10:26:54
|
| khtan,That doesn't work if there is a time gap between the 2 dates.Jim,That works if I have 2 dates but not 3, or if I have dates that have no matching timeDECLARE @Table TABLE (UserId int, TypeId int, StartDate datetime ,EndDate datetime)INSERT INTO @tableSELECT 1, 1, '1/1/2009 8:00:00 AM', '1/1/2009 1:00:00 PM' UNION ALLSELECT 1, 1, '1/1/2009 1:00:00 PM', '1/1/2009 3:00:00 PM' UNION ALLSELECT 1, 2, '1/1/2009 3:00:00 PM ','1/1/2009 5:00:00 PM' UNION ALLSELECT 2, 2, '1/2/2009 3:00:00 PM ','1/2/2009 4:00:00 PM' UNION ALLSELECT 2, 2, '1/2/2009 4:30:00 PM ','1/2/2009 5:00:00 PM'SELECT *FROM @TableSELECT a.UserID,a.StartDate,b.EndDateFROM @table aINNER JOIN@table bONa.userid = b.useridand a.enddate = b.startdateI would like this to give meUserId.....StartDate.....EndDate1......1/1/2009 08:00:00 AM.....1/1/2009 05:00:00 PM2......1/2/2009 03:00:00 PM.....1/1/2009 04:00:00 PM2......1/2/2009 04:30:00 PM.....1/1/2009 05:00:00 PM |
 |
|
|
Lattice
Starting Member
1 Post |
Posted - 2009-06-19 : 11:24:33
|
quote: Originally posted by Togaspoon khtan,That doesn't work if there is a time gap between the 2 dates.Jim,That works if I have 2 dates but not 3, or if I have dates that have no matching timeDECLARE @Table TABLE (UserId int, TypeId int, StartDate datetime ,EndDate datetime)INSERT INTO @tableSELECT 1, 1, '1/1/2009 8:00:00 AM', '1/1/2009 1:00:00 PM' UNION ALLSELECT 1, 1, '1/1/2009 1:00:00 PM', '1/1/2009 3:00:00 PM' UNION ALLSELECT 1, 2, '1/1/2009 3:00:00 PM ','1/1/2009 5:00:00 PM' UNION ALLSELECT 2, 2, '1/2/2009 3:00:00 PM ','1/2/2009 4:00:00 PM' UNION ALLSELECT 2, 2, '1/2/2009 4:30:00 PM ','1/2/2009 5:00:00 PM'SELECT *FROM @TableSELECT a.UserID,a.StartDate,b.EndDateFROM @table aINNER JOIN@table bONa.userid = b.useridand a.enddate = b.startdateI would like this to give meUserId.....StartDate.....EndDate1......1/1/2009 08:00:00 AM.....1/1/2009 05:00:00 PM2......1/2/2009 03:00:00 PM.....1/1/2009 04:00:00 PM2......1/2/2009 04:30:00 PM.....1/1/2009 05:00:00 PM
You can try thisDECLARE @Table TABLE (UserId int, TypeId int, StartDate datetime ,EndDate datetime)INSERT INTO @tableSELECT 1, 1, '1/1/2009 8:00:00 AM', '1/1/2009 1:00:00 PM' UNION ALLSELECT 1, 1, '1/1/2009 1:00:00 PM', '1/1/2009 3:00:00 PM' UNION ALLSELECT 1, 2, '1/1/2009 3:30:00 PM ','1/1/2009 5:35:12 PM' UNION ALLSELECT 2, 2, '1/1/2009 3:30:00 PM ','1/2/2009 4:30:00 PM' UNION ALLSELECT 2, 2, '1/2/2009 4:30:00 PM ','1/2/2009 5:00:00 PM'SELECT * FROM @Table-- for consecutives datesselect a.UserId,min(a.StartDate) as StartDate,max(a.EndDate) as EndDatefrom ( SELECT a.UserId,a.StartDate,a.EndDate FROM @table a INNER JOIN @table b ON a.userid = b.userid and a.enddate = b.startdate union SELECT a.UserId,a.StartDate,a.EndDate FROM @table a INNER JOIN @table b ON b.userid = a.userid and b.enddate = a.startdate) agroup by a.UserIdunion-- for interrupted datesselect a.UserId,a.StartDate,a.EndDatefrom ( SELECT a.UserId,a.StartDate,a.EndDate FROM @table a left JOIN @table b ON a.userid = b.userid and a.enddate = b.startdate where b.startdate is null) a inner join ( SELECT a.UserId,a.StartDate,a.EndDate FROM @table a left JOIN @table b ON b.userid = a.userid and b.enddate = a.startdate where b.startdate is null) bon a.UserId = b.UserIdand a.StartDate= b.StartDateand a.EndDate= b.EndDate |
 |
|
|
Togaspoon
Starting Member
42 Posts |
Posted - 2009-06-19 : 11:59:23
|
| Wow, nice job!Thanks for your help. |
 |
|
|
|
|
|