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 |
|
erwine
Yak Posting Veteran
69 Posts |
Posted - 2006-06-01 : 01:10:03
|
| CREATE TABLE TEST( EmpNo varchar(8), WorkDate datetime)DECLARE @LASTDATE = '20060131'INSERT INTO TESTSELECT '1','20060101' UNIONSELECT '1','20060105' UNIONSELECT '1','20060120' UNIONSELECT '2','20060115' UNIONSELECT '2','20060125' UNIONSELECT '3','20060126' Above is my testing table. I wanna have result like :empNo beginDate endDate1 '20060101' '20060105'1 '20060105' '20060120' 1 '20060120' @LASTDATE2 '20060115' '20060125'2 '20060125' @LASTDATE3 '20060126' @LASTDATEthe query will basically takes the next begindate of similar empNoif not null, if it's null then enddate= @LASTDATEanyone?thx for ur kind attentionregards,erwine... sql is fun... |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-06-01 : 02:01:24
|
[code]DECLARE @LASTDATE = '20060131'DECLARE @LASTDATE datetimeselect @LASTDATE = '20060131'select t1.EmpNo, t1.WorkDate as BeginDate, coalesce(t2.WorkDate, @LASTDATE) as EndDatefrom TEST t1 left join TEST t2 on t1.EmpNo = t2.EmpNo and t1.WorkDate < t2.WorkDate and t2.WorkDate = (select min(WorkDate) from TEST x where x.EmpNo = t1.EmpNo and x.WorkDate > t1.WorkDate)[/code] KH |
 |
|
|
erwine
Yak Posting Veteran
69 Posts |
Posted - 2006-06-01 : 02:58:34
|
| thx khtan..i have tested it.it works perfectly:P... sql is fun... |
 |
|
|
|
|
|
|
|