SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 help for sql query
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

vijayabalajicse
Starting Member

5 Posts

Posted - 10/05/2013 :  10:10:05  Show Profile  Reply with Quote
<pre>
table
names
-----
abu
arun
sugan
suhe
sagu
viru
dhone
goru


<u> Sql query for above output </u>

previous_name current_name Lastname
null abu arun
abu arun sugan
arun sugan suhe
sugan suhe sagu
suhe sagu viru
sagu viru dhone
viru dhone goru
dhone goru null
</pre>

khtan
In (Som, Ni, Yak)

Singapore
17431 Posts

Posted - 10/06/2013 :  04:20:00  Show Profile  Reply with Quote
what is your question ?


KH
Time is always against us

Go to Top of Page

vijayabalajicse
Starting Member

5 Posts

Posted - 10/06/2013 :  05:24:42  Show Profile  Reply with Quote



table

names
-----
abu
arun
sugan
suhe
sagu
viru
dhone
goru

What query do I need to obtain the following output?

previous_name current_name Lastnam
null abu arun
abu arun sugan
arun sugan suhe
sugan suhe sagu
suhe sagu viru
sagu viru dhone
viru dhone goru
dhone goru null

Go to Top of Page

James K
Flowing Fount of Yak Knowledge

3323 Posts

Posted - 10/06/2013 :  17:14:32  Show Profile  Reply with Quote
quote:
Originally posted by vijayabalajicse

<pre>
table
names
-----
abu
arun
sugan
suhe
sagu
viru
dhone
goru


<u> Sql query for above output </u>

previous_name current_name Lastname
null abu arun
abu arun sugan
arun sugan suhe
sugan suhe sagu
suhe sagu viru
sagu viru dhone
viru dhone goru
dhone goru null
</pre>

If your source table has only the one column that you have indicated, there is no way you can get the output you are looking for. That is because the data in a table is an unordered collection. So SQL Server does not know that abu comes first, or that arun follows it or any other information about the ordering. Unless you have some other column that specifies how these names are to be ordered, you cannot get the output you are looking for.
Go to Top of Page

vijayabalajicse
Starting Member

5 Posts

Posted - 10/06/2013 :  21:40:34  Show Profile  Reply with Quote
SELECT
LAG(names) OVER (ORDER BY names) Previousnames,
names,
LEAD(names) OVER (ORDER BY names) Nextnames
FROM yourTable
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000