| Author |
Topic |
|
jung1975
Aged Yak Warrior
503 Posts |
Posted - 2005-09-06 : 12:16:51
|
I have a table looks like this:Location EmployeeID Title Year Month Activie Termed 21000 1001 IT Supervisor 2002 9 1 021000 1001 Mgr Business Intelligence 2003 4 1 0 This table shows the change of title for EMployee ID = 1001My desired output looks like below.. I would like to display the detailed record for each month until the title change got affected.Location EmployeeID Title Year Month Activie Termed 21000 1001 IT Supervisor 2002 9 1 021000 1001 IT Supervisor 2002 10 1 021000 1001 IT Supervisor 2002 11 1 021000 1001 IT Supervisor 2002 12 1 021000 1001 IT Supervisor 2003 1 1 021000 1001 IT Supervisor 2003 2 1 021000 1001 IT Supervisor 2002 3 1 021000 1001 Mgr Business Intelligence 2003 4 1 0 |
|
|
SamC
White Water Yakist
3467 Posts |
Posted - 2005-09-06 : 13:23:51
|
| What range of results do you want if there are 3, 4 or "N" title changes for an employee? |
 |
|
|
SamC
White Water Yakist
3467 Posts |
Posted - 2005-09-06 : 13:32:03
|
| [code]SELECT Location, EmployeeID, Title, Year, Month, Activie, Termed FROM MyTable WHERE EmployeeID = @EmployeeID AND DATEADD(mm, Month, DATEADD(yy, Year-1900, '')) BETWEEN @StartDate AND @FinishDate ORDER BY Year ASC, Month ASC[/code]So, you'll need @StartDate and @FinishDate to get the above to work...[code]SELECT @StartDate = MIN(DATEADD(mm, Month, DATEADD(yy, Year-1900, ''))) -- Earliest date for this employee FROM MyTable WHERE EmployeeID = @EmployeeID GROUP BY EmployeeIDSELECT @FinishDate = MIN(DATEADD(mm, Month, DATEADD(yy, Year-1900, ''))) -- Earliest date that has a different title FROM MyTable WHERE EmployeeID = @EmployeeID AND Title <> (SELECT Title FROM MyTable WHERE EmployeeID = @EmployeeID AND DATEADD(mm, Month, DATEADD(yy, Year-1900, '')) = @StartDate) GROUP BY EmployeeID [/code]Edit: corrected the conversion of integer Year to DATETIME format. |
 |
|
|
VIG
Yak Posting Veteran
86 Posts |
Posted - 2005-09-06 : 15:02:16
|
| [code]declare @t table (Location int,EmployeeID int,Title varchar(50),[Year] int,[Month] int,Activie int,Termed int)insert @tselect 21000,1001,'IT Supervisor',2002,9,1,0 unionselect 21000,1001,'Mgr Business Intelligence',2003,4,1,0select t.Location,t.EmployeeID,t.Title, t.[year]+ ((t.month+n.n)/12) [Year] ,((t.month-1+n.n)%12) +1 [Month] ,t.Activie ,t.Termedfrom @t tleft join @t t1ont1.location=t.location and t.EmployeeID=t.EmployeeID and(t.[year]<t1.[year]) or (t.[year]=t1.[year] and t.[month]<t1.[month])join tally n on n.n<=isnull(12*(t1.[year]-t.[year]) + t1.[month]-t.[month]-1,0) [/code]tally - table with numbers from 0 to ..... |
 |
|
|
jung1975
Aged Yak Warrior
503 Posts |
Posted - 2005-09-06 : 16:06:23
|
drop table tallycreate table tally ( n int)goset nocount on declare @n intset @n = 1while @n < 12begininsert into tally values(@n)set @n = @n + 1end--Create test tabledeclare @t table (Location int,EmployeeID int,Title varchar(50),[Year] int,[Month] int,Active int,Termed int)insert @tselect 21000,1001,'IT Supervisor',2002,9,1,0 union allselect 21000,1001,'Mgr Business Intelligence',2003,4,1,0union all select 21000,1001,'CEO',2005,3,1,0select t.Location,t.EmployeeID,t.Title, t.[year]+ ((t.month+n.n)/12) [Year] ,((t.month-1+n.n)%12) +1 [Month] ,t.Active ,t.Termedfrom @t tleft join @t t1ont1.location=t.location and t.EmployeeID=t.EmployeeID and(t.[year]<t1.[year]) or (t.[year]=t1.[year] and t.[month]<t1.[month])join tally n on n.n <= isnull(12*(t1.[year]-t.[year]) + t1.[month]-t.[month]-1,0) this script gives me:21000 1001 IT Supervisor 2002 10 1 021000 1001 IT Supervisor 2002 11 1 021000 1001 IT Supervisor 2003 12 1 021000 1001 IT Supervisor 2003 1 1 021000 1001 IT Supervisor 2003 2 1 021000 1001 IT Supervisor 2003 3 1 021000 1001 IT Supervisor 2002 10 1 021000 1001 IT Supervisor 2002 11 1 021000 1001 IT Supervisor 2003 12 1 021000 1001 IT Supervisor 2003 1 1 021000 1001 IT Supervisor 2003 2 1 021000 1001 IT Supervisor 2003 3 1 021000 1001 IT Supervisor 2003 4 1 021000 1001 IT Supervisor 2003 5 1 021000 1001 IT Supervisor 2003 6 1 021000 1001 IT Supervisor 2003 7 1 021000 1001 IT Supervisor 2003 8 1 021000 1001 Mgr Business Intelligence 2003 5 1 021000 1001 Mgr Business Intelligence 2003 6 1 021000 1001 Mgr Business Intelligence 2003 7 1 021000 1001 Mgr Business Intelligence 2003 8 1 021000 1001 Mgr Business Intelligence 2003 9 1 021000 1001 Mgr Business Intelligence 2003 10 1 021000 1001 Mgr Business Intelligence 2003 11 1 021000 1001 Mgr Business Intelligence 2004 12 1 021000 1001 Mgr Business Intelligence 2004 1 1 021000 1001 Mgr Business Intelligence 2004 2 1 021000 1001 Mgr Business Intelligence 2004 3 1 0 But, this is what I really want to get as the output..21000 1001 IT Supervisor 2002 9 1 0 21000 1001 IT Supervisor 2002 10 1 021000 1001 IT Supervisor 2002 11 1 021000 1001 IT Supervisor 2003 12 1 021000 1001 IT Supervisor 2003 1 1 021000 1001 IT Supervisor 2003 2 1 021000 1001 IT Supervisor 2003 3 1 021000 1001 Mgr Business Intelligence 2003 4 1 021000 1001 Mgr Business Intelligence 2003 5 1 021000 1001 Mgr Business Intelligence 2003 6 1 021000 1001 Mgr Business Intelligence 2003 7 1 021000 1001 Mgr Business Intelligence 2003 8 1 021000 1001 Mgr Business Intelligence 2003 9 1 021000 1001 Mgr Business Intelligence 2003 10 1 021000 1001 Mgr Business Intelligence 2003 11 1 021000 1001 Mgr Business Intelligence 2003 12 1 021000 1001 Mgr Business Intelligence 2004 1 1 021000 1001 Mgr Business Intelligence 2004 2 1 021000 1001 Mgr Business Intelligence 2004 3 1 021000 1001 Mgr Business Intelligence 2004 4 1 021000 1001 Mgr Business Intelligence 2004 5 1 021000 1001 Mgr Business Intelligence 2004 6 1 021000 1001 Mgr Business Intelligence 2004 7 1 021000 1001 Mgr Business Intelligence 2004 8 1 021000 1001 Mgr Business Intelligence 2004 9 1 021000 1001 Mgr Business Intelligence 2004 10 1 021000 1001 Mgr Business Intelligence 2004 11 1 021000 1001 Mgr Business Intelligence 2004 12 1 021000 1001 Mgr Business Intelligence 2005 1 1 021000 1001 Mgr Business Intelligence 2005 2 1 021000 1001 CEO 2005 3 1 0 |
 |
|
|
VIG
Yak Posting Veteran
86 Posts |
Posted - 2005-09-06 : 17:17:37
|
Sorrydeclare @tally table ( n int)set nocount ondeclare @n intset @n = 0 /* Important!!! */while @n < 8000 /*The range of numbers should be big enough */begininsert into @tally values(@n)set @n = @n + 1end--Create test tabledeclare @t table (Location int,EmployeeID int,Title varchar(50),[Year] int,[Month] int,Active int,Termed int)insert @tselect 21000,1001,'IT Supervisor',2002,9,1,0 union allselect 21000,1001,'Mgr Business Intelligence',2003,4,1,0union all select 21000,1001,'CEO',2005,3,1,0select t.Location,t.EmployeeID,t.Title, year(dateadd(m,tl.n,t.dt)) [year], month(dateadd(m,tl.n,t.dt)) [month], t.Active,t.Termedfrom( select t.Location,t.EmployeeID,t.Title, dateadd(m,t.[month]-1,dateadd(yy,t.[year]-1900,0)) dt ,datediff(m,dateadd(m,t.[month]-1,dateadd(yy,t.[year]-1900,0)) ,isnull((select top 1 dateadd(m,t1.[month]-2,dateadd(yy,t1.[year]-1900,0)) from @t t1 where t1.location=t.location and t1.EmployeeID=t.EmployeeID and dateadd(m,t.[month],dateadd(yy,t.[year]-1900,0)) < dateadd(m,t1.[month],dateadd(yy,t1.[year]-1900,0))),dateadd(m,t.[month]-1,dateadd(yy,t.[year]-1900,0)))) n ,t.Active,t.Termedfrom @t t) tjoin @tally tl on t.n>= tl.n |
 |
|
|
jung1975
Aged Yak Warrior
503 Posts |
Posted - 2005-09-08 : 12:06:26
|
| thank! ,but I am not quite understand this part:,datediff(m,dateadd(m,t.[month]-1,dateadd(yy,t.[year]-1900,0)) ,isnull((select top 1 dateadd(m,t1.[month]-2,dateadd(yy,t1.[year]-1900,0)) from @t t1 where t1.location=t.location and t1.EmployeeID=t.EmployeeID and dateadd(m,t.[month],dateadd(yy,t.[year]-1900,0)) < dateadd(m,t1.[month],dateadd(yy,t1.[year]-1900,0))),dateadd(m,t.[month]-1,dateadd(yy,t.[year]-1900,0)))) nCan you explain to me what you are trying to do here? |
 |
|
|
|
|
|