开发者

Sql Query for below problem [closed]

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜