Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!eternal-september.org!feeder.eternal-september.org!.POSTED!not-for-mail From: Erland Sommarskog Newsgroups: comp.databases.ms-sqlserver Subject: Re: Help needed to write a qury Date: Mon, 25 Apr 2011 15:30:53 +0200 Organization: Erland Sommarskog Lines: 21 Message-ID: References: <95071489-1d30-4d5b-b075-aa465d7b654c@x37g2000prb.googlegroups.com> <4db4fdc3$0$41114$e4fe514c@news.xs4all.nl> <4db5449d$0$32470$e4fe514c@news.xs4all.nl> Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: 8bit Injection-Info: mx02.eternal-september.org; posting-host="DD6dU+BfJNjsjSP4/K/V7w"; logging-data="32229"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+OUIPpgKxZsFGCU54XzyS6" User-Agent: Xnews/2006.08.24 Mime-proxy/2.1.c.0 (Win32) Cancel-Lock: sha1:66K3xpNWpcOJq8FcgHEwEep+r6g= Xref: x330-a1.tempe.blueboxinc.net comp.databases.ms-sqlserver:205 Henk van den Berg (hvandenberg@xs4all.nl) writes: > 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. 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. -- Erland Sommarskog, SQL Server MVP, esquel@sommarskog.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