0

我不知道该怎么做。我只需要为唯一名称选择一行,其中 mod_date 是最新值

mod_date      smtn           name     smtn2

2013-01-31  LV002002310453   Rax      zse
2013-01-29  LV002002310453   Rax      zse         
2013-01-31  LV002002310463   Rendo    xxc           
2013-01-01  LV002002310463   Rendo    xxc          
2013-01-28  LV002002310465   Mag      xsa        
2013-01-30  LV002002310465   Mag      xsa     
2013-01-25  LV002002310465   Mag      xsa 

我的选择结果应如下所示:

 mod_date      smtn          name     smtn2

2013-01-31  LV002002310453   Rax       zse
2013-01-31  LV002002310463   Rendo     xxc       
2013-01-30  LV002002310465   Mag       xsa   

谢谢。

4

4 回答 4

3

您可以ROW_NUMBER在 CTE 中使用(假设 >= SQL-SERVER 2005)

WITH x AS (SELECT mod_date,smtn,name,smtn2, 
                RN = Row_number() 
                       OVER( 
                         partition BY name 
                         ORDER BY mod_date DESC) 
         FROM   dbo.tablename) 
SELECT mod_date,smtn,name,smtn2 
FROM   x 
WHERE  rn = 1 

DENSE_RANK当名称的最后日期不唯一时,如果您想要所有行,请改用。

于 2013-01-31T11:10:26.310 回答
1

请试试:

select * From
(
    select 
        Row_number() over (partition by name order by mod_date desc) RNUM, 
        mod_date, 
        name
    From 
        YourTable
)x where RNUM=1
于 2013-01-31T11:13:04.623 回答
0
Select distinct smtn from Databasename order by mod_date desc
于 2013-01-31T11:11:15.343 回答
0

使用 distinct 可能会忽略重复数据,但也可能会发生冲突,因为它也可能会忽略有用的数据。

您可以按您的结果排序(基于日期),以便您可以根据日期对其进行排序。

select * from (tablename) order by [Date] desc

于 2013-01-31T11:22:25.160 回答