![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
Hi Experts, I need help to write a query in SQL server 2008. Below is my table structure. My table is having four columns EmpID,EMPName,DepartmentID,AllocationDate. Below is some sample data for the table. AllocationDate is the date on which employee is transfered in that department. EmpID *EMPName * * DepartmentID * * * *AllocationDate 1 * * * Ajay * * * * * *1 * * * * * * * 01/01/2005 1 * * * Ajay * * * * * *2 * * * * * * * 04/10/2005 1 * * * Ajay * * * * * *3 * * * * * * * 09/22/2005 1 * * * Ajay * * * * * *1 * * * * * * * 07/25/2008 1 * * * Ajay * * * * * *5 * * * * * * * 03/15/2010 2 * * * John * * * * * *2 * * * * * * * 01/10/2008 2 * * * John * * * * * *1 * * * * * * * 03/15/2009 2 * * * John * * * * * *4 * * * * * * * 02/23/2010 2 * * * John * * * * * *3 * * * * * * * 04/20/2011 3 * * * Kate * * * * * *3 * * * * * * * 01/11/2007 3 * * * Kate * * * * * *4 * * * * * * * 03/16/2008 3 * * * Kate * * * * * *2 * * * * * * * 02/24/2009 3 * * * Kate * * * * * *1 * * * * * * * 04/21/2010 Now I want to find out all the transfers from one department to another for all the employess with their old and new department along with their allocation and trasfer date. I want my result like this. EMPID * * EMPName * * * * * *OldDeptID * NewDeptID AllocationDate * TransferDate 1 * * * * *Ajay * * * * 1 * * * * *2 * * * * * 01/01/2005 * * * 04/10/2005 1 * * * * *Ajay * * * * 2 * * * * *3 * * * * * 04/10/2005 * * * 09/22/2005 1 * * * * *Ajay * * * * 3 * * * * *1 * * * * * 09/22/2005 * * * 07/25/2008 1 * * * * *Ajay * * * * 1 * * * * *5 * * * * * 07/25/2008 * * * 03/15/2010 2 * * * * *John * * * * 2 * * * * *1 * * * * * 01/10/2008 * * * 03/15/2009 2 * * * * *John * * * * 1 * * * * *4 * * * * * 03/15/2009 * * * 02/23/2010 2 * * * * *John * * * * 4 * * * * *3 * * * * * 02/23/2010 * * * 04/20/2011 3 * * * * *Kate * * * * 3 * * * * *4 * * * * * 01/11/2007 * * * 03/16/2008 3 * * * * *Kate * * * * 4 * * * * *2 * * * * * 03/16/2008 * * * 02/24/2009 3 * * * * *Kate * * * * 2 * * * * *1 * * * * * 02/24/2009 * * * 04/21/2010 Any help will be appriciated. Regards Dinesh |
#3
| |||
| |||
|
|
Now I want to find out all the transfers from one department to another for all the employess with their old and new department along with their allocation and trasfer date. I want my result like this. EMPID EMPName OldDeptID NewDeptID AllocationDate TransferDate 1 Ajay 1 2 01/01/2005 04/10/2005 1 Ajay 2 3 04/10/2005 09/22/2005 1 Ajay 3 1 09/22/2005 07/25/2008 1 Ajay 1 5 07/25/2008 03/15/2010 2 John 2 1 01/10/2008 03/15/2009 2 John 1 4 03/15/2009 02/23/2010 2 John 4 3 02/23/2010 04/20/2011 3 Kate 3 4 01/11/2007 03/16/2008 3 Kate 4 2 03/16/2008 02/24/2009 3 Kate 2 1 02/24/2009 04/21/2010 |
#4
| |||
| |||
|
|
WITH numbered AS ( SELECT EmpID, EmpName, DepartmentID, AllocationDate, rowno = row_number() OVER(PARTITION BY EmpID ORDER BY AllocationDate) ) SELECT a.EmpID, a.EmpName, a.DepartmentID AS OldDeptID, b.DepartmentID AS NewDeptID, a.AllocationDate, b.AllocationDate AS TransferDate FROM numbered a JOIN numbered b ON b.rowno = a.rowno +1 ORDER BY a.EmpId, a.rowno |
#5
| |||
| |||
|
|
On 24-04-2011 21:51, Erland Sommarskog wrote: WITH numbered AS ( SELECT EmpID, EmpName, DepartmentID, AllocationDate, rowno = row_number() OVER(PARTITION BY EmpID ORDER BY AllocationDate) ) SELECT a.EmpID, a.EmpName, a.DepartmentID AS OldDeptID, b.DepartmentID AS NewDeptID, a.AllocationDate, b.AllocationDate AS TransferDate FROM numbered a JOIN numbered b ON b.rowno = a.rowno +1 ORDER BY a.EmpId, a.rowno If I copy/paste and run this code, it complains about invalid columnnames. I do notice that there's no reference to a table in the AS (SELECT EmpId...) part, but when I put in the name of the table that I created, I get those invalid columnname errors as well. I'm sure I'm forgetting something very basic, but I can't get it to run. What am I overlooking? |
#6
| |||
| |||
|
|
I forgot the FROM clause in the CTE: |
|
Had Dinesh posted CREATE TABLE statement for his table and INSERT statements with sample data, I would have been able to test my query. Now I wasn't. Here's the create/insert stuff. |
| |
#7
| |||
| |||
|
|
Dinesh (dinesh... (AT) gmail (DOT) com) writes: Now I want to find out all the transfers from one department to another for all the employess with their old and new department along with their allocation and trasfer date. I want my result like this. EMPID * * EMPName * * * * * *OldDeptID * NewDeptID AllocationDate * TransferDate 1 * * * *Ajay * * * * *1 * * * *2 * * ** * *01/01/2005 * * * 04/10/2005 1 * * * *Ajay * * * * *2 * * * *3 * * ** * *04/10/2005 * * * 09/22/2005 1 * * * *Ajay * * * * *3 * * * *1 * * ** * *09/22/2005 * * * 07/25/2008 1 * * * *Ajay * * * * *1 * * * *5 * * ** * *07/25/2008 * * * 03/15/2010 2 * * * *John * * * * *2 * * * *1 * * ** * *01/10/2008 * * * 03/15/2009 2 * * * *John * * * * *1 * * * *4 * * ** * *03/15/2009 * * * 02/23/2010 2 * * * *John * * * * *4 * * * *3 * * ** * *02/23/2010 * * * 04/20/2011 3 * * * *Kate * * * * *3 * * * *4 * * ** * *01/11/2007 * * * 03/16/2008 3 * * * *Kate * * * * *4 * * * *2 * * ** * *03/16/2008 * * * 02/24/2009 3 * * * *Kate * * * * *2 * * * *1 * * ** * *02/24/2009 * * * 04/21/2010 WITH numbered AS ( * *SELECT EmpID, EmpName, DepartmentID, AllocationDate, * * * * * rowno = row_number() OVER(PARTITION BY EmpID * * * * * * * * * * * * * * * * * * ORDER BY AllocationDate) ) SELECT a.EmpID, a.EmpName, a.DepartmentID AS OldDeptID, * * * *b.DepartmentID AS NewDeptID, a.AllocationDate, * * * *b.AllocationDate AS TransferDate FROM * numbered a JOIN * numbered b ON b.rowno = a.rowno +1 ORDER *BY a.EmpId, a.rowno Fred who posted the other solution should take notice. The row_number function, added in SQL 2005, is much more efficient in numbering the rows than using a subquery. -- Erland Sommarskog, SQL Server MVP, esq... (AT) sommarskog (DOT) se Links for SQL Server Books Online: SQL 2008:http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx SQL 2005:http://msdn.microsoft.com/en-us/sqls...bb895970.aspx- Hide quoted text - - Show quoted text - |
#8
| |||
| |||
|
|
Here's the create/insert stuff. Since I'm trying to get my head around CTE and partition by/over order by, I'd appreciate a pointer if you can find the time for it. |
#9
| |||
| |||
|
|
Thanks for the sample data! I see that I had forgot the join condition on EmpId - without the query is kind of meaningless. The principle is simple: number the rows, and then make a self-join on the row number with the next number. |
#10
| |||
| |||
|
|
Erland Sommarskog (esquel (AT) sommarskog (DOT) se) writes: Thanks for the sample data! I see that I had forgot the join condition on EmpId - without the query is kind of meaningless. The principle is simple: number the rows, and then make a self-join on the row number with the next number. JOIN numbered b ON b.rowno = a.rowno +1 AND b.EmpId = a.EmpId |
![]() |
| Thread Tools | |
| Display Modes | |
| |