![]() | |
![]() |
| | Thread Tools | Display Modes |
#11
| |||
| |||
|
|
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. |
#12
| |||
| |||
|
|
Erland Sommarskog (esq... (AT) sommarskog (DOT) se) writes: Thanks for the sample data! I see that I had forgot the join condition on EmpId - without the queryis kind of meaningless. The principle is simple: number the rows, and then make a self-join on the row number with the next number. Pressed Send to quickly! Here is the query: WITH numbered AS ( * *SELECT EmpId, EmpName, DepartmentID, AllocationDate, * * * * * rowno = row_number() OVER(PARTITION BY EmpId * * * * * * * * * * * * * * * * * * ORDER BY AllocationDate) * *FROM * EmployeeDept ) 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 * * * * * * * * *AND b.EmpId = a.EmpId ORDER *BY a.EmpId, a.rowno -- 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/sqlserver/bb895970.aspx |
#13
| |||
| |||
|
|
The WITH clause works in a manner similar to a derived table, but sets up the syntax so that it can be referenced more than once. In this instance it allows the table to joined to itself rather than a second derivation of itself as would happen using the derived table syntax. |
|
The OVER construct (just what is the syntax term?) specifies that row numbering restarts for each new value of the field specified in PARTITION BY, with the rows arranged within each partition as specified in ORDER BY. |
|
Do you know if the engine optimizes by indexing the WITH table on the PARTTION field and the field receiveing row_number()? |
#14
| |||
| |||
|
|
Fred. (ghrno-goo... (AT) yahoo (DOT) com) writes: The WITH clause works in a manner similar to a derived table, but sets up the syntax so that it can be referenced more than once. *In this instance it allows the table to joined to itself rather than a second derivation of itself as would happen using the derived table syntax. Correct. This is known as a CTE, Common Table Expression. The OVER construct (just what is the syntax term?) specifies that row numbering restarts for each new value of the field specified in PARTITION BY, with the rows arranged within each partition as specified in ORDER BY. Correct. It's called the OVER clause. Do you know if the engine optimizes by indexing the WITH table on the PARTTION field and the field receiveing row_number()? The CTE is purely logical. Some products are able to materialize the CTE as optimisation, but SQL Server is not. Thus, in some cases it can be better to use a temp table to hold the numbering. Lennart mention LEAD and LAG. They are also subclauses within OVER. They are not available in SQL Server currently, but in products that have LAG and LEAD, this can be written as a straight query without a self-join. -- 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/sqlserver/bb895970.aspx |
#15
| |||
| |||
|
|
Any rumors on lead and/or lag analytical functions in sqlserver? |
#16
| |||
| |||
|
|
Lennart Jonsson (erik.lennart.jonsson (AT) gmail (DOT) com) writes: Any rumors on lead and/or lag analytical functions in sqlserver? I have not watched this video in detail, but Microsoft reportedly demoed new window functions at TechEd: http://channel9.msdn.com/Events/Tech...ca/2011/DBI310 |
#17
| |||
| |||
|
![]() |
| Thread Tools | |
| Display Modes | |
| |