| Author |
Topic |
|
santana
Yak Posting Veteran
72 Posts |
Posted - 2010-03-09 : 09:23:26
|
| Hi,I have one history table with those information:Emp Dept Change_date (the date when the employee changed his department)2899 632 200901 2899 637 200903 2899 632 200908 2899 723 200908 But I need to know for each month on 2009, in which dept this employee was working.How can I do this with query? I only have the date when the employee changed.I would like to have this:Emp Dept Change_date Date 2899 632 200901 200901 2899 632 200901 200902 2899 637 200903 2009032899 637 200903 2009042899 637 200903 2009052899 637 200903 2009062899 637 200903 2009072899 632 200908 ->I Dont need this,only the max in the month. The employee changed twice in the same month.2899 723 200908 2009082899 723 200908 2009092899 723 200908 2009102899 723 200908 2009112899 723 200908 200912Could you help me, please?Thanks.Regards,Santana |
|
|
haroon2k9
Constraint Violating Yak Guru
328 Posts |
Posted - 2010-03-09 : 10:20:06
|
quote: Originally posted by santana Hi,I have one history table with those information:Emp Dept Change_date (the date when the employee changed his department)2899 632 200901 2899 637 200903 2899 632 200908 2899 723 200908 But I need to know for each month on 2009, in which dept this employee was working.How can I do this with query? I only have the date when the employee changed.I would like to have this:Emp Dept Change_date Date 2899 632 200901 200901 2899 632 200901 200902 2899 637 200903 2009032899 637 200903 2009042899 637 200903 2009052899 637 200903 2009062899 637 200903 2009072899 632 200908 ->I Dont need this,only the max in the month. The employee changed twice in the same month.2899 723 200908 2009082899 723 200908 2009092899 723 200908 2009102899 723 200908 2009112899 723 200908 200912Could you help me, please?Thanks.Regards,Santana
Select * from( select emp,dept,change_date,row_number()over(partition by emp,dept order by change_date desc)as seq from tblanme)as t where t.seq <=2 first execute the above qry.Then apply the where condition based on your expecteation please |
 |
|
|
santana
Yak Posting Veteran
72 Posts |
Posted - 2010-03-10 : 08:26:31
|
| Hi,could you help me again, please?Because I dont know what I did wrong.I run this query:Select * from( select emp,dep,changedate,row_number()over(partition by emp,dep order by changedate desc)as seq from [HISTORY] where fieldname = 'COSTCODE')as t where t.seq <=2 and emp in( '2899','3023') and I receive this result:emp dep changedate seq2899 632 2009-08 12899 637 2009-03 22899 632 2009-01 12899 723 2009-08 1How can I use this seq?Sorry I don't understand!!!Thanks a lot!Regards |
 |
|
|
haroon2k9
Constraint Violating Yak Guru
328 Posts |
Posted - 2010-03-10 : 09:11:20
|
quote: Originally posted by santana Hi,could you help me again, please?Because I dont know what I did wrong.I run this query:Select * from( select emp,dep,changedate,row_number()over(partition by emp,dep order by changedate desc)as seq from [HISTORY] where fieldname = 'COSTCODE')as t where t.seq <=2 and emp in( '2899','3023') and I receive this result:emp dep changedate seq2899 632 2009-08 12899 637 2009-03 22899 632 2009-01 12899 723 2009-08 1How can I use this seq?Sorry I don't understand!!!Thanks a lot!Regards
Please run this portion alone and see first and then apply the filter condtion in seq..select * from( select emp,dep,changedate,row_number()over(partition by emp,dep order by changedate desc)as seq from [HISTORY] where fieldname = 'COSTCODE')as t where emp in( '2899','3023') |
 |
|
|
santana
Yak Posting Veteran
72 Posts |
Posted - 2010-03-11 : 08:18:45
|
| Hi,Maybe there are no way to have what I need by query.Are there any way that I could populate this table? In the table I have only the record when I insert / change the department by employee - the date when the department has changed by employee. I would like to populate this table with the months between the alterations.eg.: I know that the employee has department 632 at 200901, 633 after 200903 and 723 after 200908.I would like to populate the department between 200901 and 200902 with 632, between 200903 and 200907 with 633, and 200908 to now with with 723. Is this possible? Can I do this? How?I appreciate any help! Please!!!!!Regards,SANTANA |
 |
|
|
haroon2k9
Constraint Violating Yak Guru
328 Posts |
Posted - 2010-03-11 : 08:24:07
|
quote: Originally posted by santana Hi,Maybe there are no way to have what I need by query.Are there any way that I could populate this table? In the table I have only the record when I insert / change the department by employee - the date when the department has changed by employee. I would like to populate this table with the months between the alterations.eg.: I know that the employee has department 632 at 200901, 633 after 200903 and 723 after 200908.I would like to populate the department between 200901 and 200902 with 632, between 200903 and 200907 with 633, and 200908 to now with with 723. Is this possible? Can I do this? How?I appreciate any help! Please!!!!!Regards,Renata
iam extremely sorry..honestly,faling to understand this. |
 |
|
|
santana
Yak Posting Veteran
72 Posts |
Posted - 2010-03-11 : 08:36:30
|
| Hi,I have one table withemployee number ----- date when changed the department ------- new department1010 200901 632 -> in 200901 the employee 1010 changed to department 6321010 200903 633 -> in 200903 the employee 1010 changed to department 6331010 200908 723 -> in 200908 the employee 1010 changed to department 723But I need another table or one query, what show me which department the employee have for all months, and not only the month when he chaged.I need to know for example, in 200902 he was in the departemt 632, and in 200904 in the departemtn 633.I need to know for all months in 2009, in which department he was.Like this:employee number ----- date when changed the department ------- new departemtn1010 200901 632 -> in 200901 the employee 1010 changed to department 6321010 200902 632 -> in 200902 the employee 1010 changed to department 6321010 200903 633 -> in 200903 the employee 1010 changed to department 6331010 200904 633 -> in 200904 the employee 1010 changed to department 6331010 200905 633 -> in 200905 the employee 1010 changed to department 6331010 200906 633 -> in 200906 the employee 1010 changed to department 6331010 200907 633 -> in 200907 the employee 1010 changed to department 6331010 200908 723 -> in 200908 the employee 1010 changed to department 7231010 200909 723 -> in 200909 the employee 1010 changed to department 7231010 200910 723 -> in 200910 the employee 1010 changed to department 7231010 200911 723 -> in 200911 the employee 1010 changed to department 7231010 200912 723 -> in 200912 the employee 1010 changed to department 723How can I do this??I need help, PLEASE!!!!!PLEASE!!!!!PLEASE!!!!!PLEASE!!!!!Thanks a lot!Regards |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-11 : 11:22:55
|
| [code]DECLARE @Year intSELECT @year=2009--your passed valueDECLARE @Calendar table(Period int)INSERT INTO @CalendarSELECT (100 * y.[Year]) +m.[Month] FROM (SELECT @year AS [Year]) yCROSS JOIN (SELECT 1 AS [Month] UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL ... SELECT 12)m SELECT r.Emp,c.Period,r.DeptFROM @CALENDAR cCROSS JOIN (SELECT t.Emp,t.Dept,t.Change_date as start,t1.Change_date AS End FROM YourTable t OUTER APPLY (SELECT TOP 1 Change_Date FROM YourTable WHERE Emp=t.Emp AND Dept <> t.Dept ORDER BY Change_Date ASC)t1 )rWHERE c.Period BETWEEN r.start AND COALESCE(r.End,r.start)[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
santana
Yak Posting Veteran
72 Posts |
Posted - 2010-03-17 : 09:37:46
|
| Hi,thanks for your help.I created the query below, but didn't return any value. What did I do wrong? Can I see the table Calendar or only the query result?newvalue = deptempref = empDECLARE @Year intSELECT @year=2009--your passed valueDECLARE @Calendar table(Period int)INSERT INTO @CalendarSELECT (100 * y.[Year]) +m.[Month] FROM (SELECT @year AS [Year]) yCROSS JOIN (SELECT 1 AS [Month] UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9 UNION ALL SELECT 10 UNION ALL SELECT 11 UNION ALL SELECT 12)m SELECT r.Empref,c.Period,r.[NEWVALUE]FROM @CALENDAR cCROSS JOIN (SELECT t.Empref,t.[NEWVALUE],t.CHANGEDATE as start,t1.CHANGEDATE AS [End] FROM [tmsdata].[tmsuser].[HISTORY] t --where t.empref = '2899' and t.fieldname = 'COSTCODE' OUTER APPLY (SELECT TOP 1 CHANGEDATE FROM [tmsdata].[tmsuser].[HISTORY] WHERE Empref=t.Empref AND [NEWVALUE] <> t.[NEWVALUE] and empref = '2899' and fieldname = 'COSTCODE' ORDER BY CHANGEDATE ASC)t1 )rWHERE c.Period BETWEEN r.start AND COALESCE(r.[End],r.start) |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-17 : 10:01:51
|
| do you have records for empref = '2899' and fieldname = 'COSTCODE'? if not comment the conditions inside APPLY subquery also------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
santana
Yak Posting Veteran
72 Posts |
Posted - 2010-03-17 : 10:49:44
|
| Hi,I run that:DECLARE @Year intSELECT @year=2009--your passed valueDECLARE @Calendar table(Period int)INSERT INTO @CalendarSELECT (100 * y.[Year]) +m.[Month] FROM (SELECT @year AS [Year]) yCROSS JOIN (SELECT 1 AS [Month] UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9 UNION ALL SELECT 10 UNION ALL SELECT 11 UNION ALL SELECT 12)m SELECT r.Empref,c.Period,r.[NEWVALUE]FROM @CALENDAR cCROSS JOIN (SELECT t.Empref,t.[NEWVALUE],t.fieldname, CONVERT(char(4),DATEPART(year,t.CHANGEDATE)) + CASE WHEN DATEPART(month, t.CHANGEDATE) < 10 THEN ('0')ELSE ('')END + CONVERT(char(2),DATEPART(month, t.CHANGEDATE)) as start, t1.CHANGEDATE AS [End] FROM [tmsdata].[tmsuser].[HISTORY] t OUTER APPLY (SELECT TOP 1 CONVERT(char(4),DATEPART(year,CHANGEDATE)) + CASE WHEN DATEPART(month, CHANGEDATE) < 10 THEN ('0')ELSE ('')END + CONVERT(char(2),DATEPART(month, CHANGEDATE)) as CHANGEDATE FROM [tmsdata].[tmsuser].[HISTORY] WHERE Empref=t.Empref AND [NEWVALUE] <> t.[NEWVALUE] and t.fieldname = fieldname and empref = '2899' and fieldname = 'COSTCODE' ORDER BY CONVERT(char(4),DATEPART(year,CHANGEDATE)) + CASE WHEN DATEPART(month, CHANGEDATE) < 10 THEN ('0')ELSE ('')END + CONVERT(char(2),DATEPART(month, CHANGEDATE)) ASC)t1 )rWHERE c.Period BETWEEN r.start AND COALESCE(r.[End],r.start)and empref = '2899' and fieldname = 'COSTCODEand I received only one line into the result: empref period Dept2899 200901 632But I know that there are more that for this employee:SELECT distinct [tmsdata].[tmsuser].[HISTORY].[EMPREF],[NEWVALUE],CONVERT(char(4),DATEPART(year,CHANGEDATE)) + CASE WHEN DATEPART(month, CHANGEDATE) < 10THEN ('0')ELSE ('')END+ CONVERT(char(2),DATEPART(month, CHANGEDATE)) as changedate FROM [tmsdata].[tmsuser].[HISTORY]where [tmsdata].[tmsuser].[HISTORY].empref = '2899' and fieldname = 'COSTCODE'empref newvalue changedate2899 637 200901 2899 632 200908 2899 723 200908 What I did wrong?Thank you for all!Regards, |
 |
|
|
santana
Yak Posting Veteran
72 Posts |
Posted - 2010-03-17 : 11:23:56
|
| I need to use the fieldname because this is a history table, and I want to populate date only for dept = costcode. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-17 : 11:37:36
|
| then remove TOP 1 inside subquery------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
santana
Yak Posting Veteran
72 Posts |
Posted - 2010-03-17 : 12:12:01
|
| If I remove the TOP 1 , I received this message:Msg 1033, Level 15, State 1, Line 46The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP or FOR XML is also specified.And If I remove the ORDER BY, I received this result:EMPREF DATE DEPT3077 200903 639 --> WHERE ARE THE MONTHS BEFORE MARCH?3077 200904 6393077 200905 6393077 200906 6393077 200907 6393077 200908 6393077 200909 6393077 200910 6393077 200911 639 --> THE CORRECT FOR 200911 IS 642I changed the example (see below). I chose the employee 3077. This guy only change once and is easy understand what happen in the query.empref newvalue changedate3077 639 200903 3077 642 200911Thanks!Regards,Santana |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-17 : 13:10:59
|
| do you mean you need months before first appearance in history table also?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
santana
Yak Posting Veteran
72 Posts |
Posted - 2010-03-17 : 13:47:34
|
| Hi,first thank you for your help.My period start in 2008 until now.When the employee start in the company, the manager include into the history table your department, and If the employee change the department, the manager include into the History table, the new department and the data_change. Into the history table I have for each employee, the department and when included that.But I need that information for all month since the employee start in the company until now. For example: for employee 3077.He started in 200903 in the department 639, but in 200911 the employee change for department 642.I need to see this:between 200903 and 200910 -> 639after 200911 until now -> 642like:200903 --- 639200904 --- 639200905 --- 639200906 --- 639200907 --- 639200908 --- 639200909 --- 639200910 --- 639200911 --- 642200912 --- 642201001 --- 642201002 --- 642201003 --- 642Can I do this? Is it possible?Regards,santana |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-17 : 13:57:35
|
then wont this be enough?DECLARE @Year intSELECT @year=2009--your passed valueDECLARE @Calendar table(Period int)INSERT INTO @CalendarSELECT (100 * y.[Year]) +m.[Month] FROM (SELECT @year AS [Year]) yCROSS JOIN (SELECT 1 AS [Month] UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL ... SELECT 12)m SELECT r.Emp,c.Period,r.DeptFROM @CALENDAR cCROSS JOIN (SELECT t.Emp,t.Dept,t.Change_date as start,t1.Change_date AS End FROM YourTable t OUTER APPLY (SELECT TOP 1 Change_Date FROM YourTable WHERE Emp=t.Emp AND Dept <> t.Dept ORDER BY Change_Date ASC)t1 )rWHERE c.Period BETWEEN r.start AND COALESCE(r.End-1,r.start) ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
santana
Yak Posting Veteran
72 Posts |
Posted - 2010-03-18 : 05:19:18
|
| Didn't work.Emp Date Dept3077 200903 6393077 200904 6393077 200905 6393077 200906 6393077 200907 6393077 200908 6393077 200909 6393077 200910 639The only difference: this query excluded the last line/row/register (3077 200911 639 --> THE CORRECT FOR 200911 IS 642)I don't have any idea How can I do this......Thank you a lot for your help...How can I do this? I need to populate this table or other with those information. Maybe something with LOOP.Regards, |
 |
|
|
santana
Yak Posting Veteran
72 Posts |
Posted - 2010-03-18 : 06:28:06
|
| I need something like this:While date for greater or equal to 200903 (change_date) then dept receive 639elsewhile date for greater or equal to 200911 (change_date) then department receive 642...This for the example empref dept changedate3077 639 2009033077 642 200911??????Thanks |
 |
|
|
haroon2k9
Constraint Violating Yak Guru
328 Posts |
Posted - 2010-03-18 : 06:52:04
|
quote: Originally posted by santana I need something like this:While date for greater or equal to 200903 (change_date) then dept receive 639elsewhile date for greater or equal to 200911 (change_date) then department receive 642...This for the example empref dept changedate3077 639 2009033077 642 200911??????Thanks
try something like this,dept=case when date >='200903' then 639 else when date >='200911' then 642 end |
 |
|
|
santana
Yak Posting Veteran
72 Posts |
Posted - 2010-03-18 : 07:07:51
|
| But in this case, I need to fixed the date and the department in the query? Because that for me is only one example. I have more dates, employees and departments into my table. |
 |
|
|
Next Page
|