开发者

Sql Query for below problem [closed]

开发者 https://www.devze.com 2023-04-04 21:24 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form. For help clari
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

i have one table having data as mentioned be开发者_Go百科low:

  Name
  ----
  ram
  ram
  ram
  sita
  sita
  sita

now i need out put like below:

ram
sita
ram
sita
ram
sita

is any one know the answer?


You can do this with the analytic function ROW_NUMBER():

select name from 
    ( select name
             , row_number() over (partition by name order by id) as rn
      from your_table
order by rn, name
/

You will need a column to order the names: I've proposed ID but don't know what columns your table has.


The above is the Oracle syntax. T-SQL may support different syntax.


declare @T table(Name varchar(5))

insert into @T values
('ram'),
('ram'),
('ram'),
('sita'),
('sita'),
('sita')

select Name
from @T    
order by row_number() over(partition by Name order by Name), Name
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号