选择sql server中具有重复值的视图的不同值

选择sql server中具有重复值的视图的不同值,sql,sql-server,group-by,distinct,Sql,Sql Server,Group By,Distinct,我对以下表格结果有一个看法: id date text name othertext --- |-------------|-------|--------|--------- 14 | 2013/02/01 | text1 | john | 399 14 | 2013/02/02 | text2 | john | 244 14 | 2013/02/03 | text3 | john | 555 14 | 2013/02/04 |

我对以下表格结果有一个看法:

id   date         text     name     othertext
--- |-------------|-------|--------|---------
14  | 2013/02/01  | text1 | john   | 399 
14  | 2013/02/02  | text2 | john   | 244
14  | 2013/02/03  | text3 | john   | 555
14  | 2013/02/04  | text4 | john   | 300
13  | 2013/02/05  | text5 | juliet | 200
12  | 2013/02/06  | text6 | borat  | 500
12  | 2013/02/07  | text7 | borat  | 600
10  | 2013/02/08  | text8 | Adam   | 700
10  | 2013/02/09  | text9 | Adam   | 700
它就像一个评论系统。每个用户都可以在不同的帖子中发表评论(“id”是帖子的id)。 我想得到最后一次评论的帖子的列表,按日期排序,但我不想重复评论过的用户的名字

这是我想要的结果:

id   date          text    name     othertext
--- |-------------|-------|--------|---------
10  | 2013/02/09  | text9 | Adam   | 700 
12  | 2013/02/07  | text2 | borat  | 600
13  | 2013/02/05  | text5 | juliet | 200
14  | 2013/02/04  | text4 | john   | 300
最后:我想知道上一次被评论但没有重复的帖子的id


非常感谢

有很多方法可以实现你想要的。由于它是
SQL Server
,因此您可以使用诸如
ROW\u NUMBER()

select t.*
from your_table t
inner join 
(
   select id, max(date) as mdate
   from your_table
   group by id
) x on x.id = t.id and t.date = x.mdate
order by t.date desc

有很多方法可以实现你想要的。由于它是
SQL Server
,因此您可以使用诸如
ROW\u NUMBER()


如果您将x.mdate更改为x.m_日期,它将非常有效。回答得又好又快,非常感谢!!如果您将x.mdate更改为x.m_日期,它将非常有效。回答得又好又快,非常感谢!!
SELECT  id, date, text, name, othertext
FROM    
        (
            SELECT  id,date,text,name,othertext,
                    ROW_NUMBER() OVER (PARTITION BY ID ORDER BY date DESC) rn
            FROM    tableName
        ) a
WHERE   rn = 1
ORDER   BY id