2017-06-20 78 views
0
id name school date 
1 John QVS 1/3/2000 
1 John RKS 1/4/2008 
2 Sera ACS 1/2/2009 
2 Sera WCS 1/4/2011 
3 Jack YUN 1/4/2014 
3 Jack KIL 1/3/2017 

姓名,学校和日期都来自不同的表格,即名称,学校和日期连接在一起的内部连接。只在最大字段上选择行

我想选择只有最新日期的行。结果应该是:

id name school date 
1 John RKS 1/4/2008 
2 Sera WCS 1/4/2011 
3 Jack KIL 1/3/2017 
+0

你尝试过什么?有很多方法可以做到这一点。 –

+0

给我然后...我没有试过因为我不知道从哪里开始从 –

+0

第一张桌子不是一个dtabase表,但结果从一个加入的查询 –

回答

0

试试这个:

select * from table t1 where not exists ( select 1 from table t2 where t1.id = t2.id and t1.name = t2.name and t1.date < t2.date )

+0

theres 3个表合并到上面的第一个列表 –

0

您可以使用此。我希望它能为你工作。

SELECT 
tb1.id 
,tb1.name 
,tb1.school 
,tb1.[date] 
FROM 
yourTable tb1 
where [date] IN 
(
SELECT MAX([date]) FROM yourTable tb2 WHERE tb1.id = tb2.id GROUP BY tb2.id 
) 
+0

没有tb1。学校,它的tb2.school和tb3.date –

+0

你可以给我所有的表格结构吗? –

+0

请正确地从顶部读取 –

0

您可以使用Row_number。 如果你想与最新的日期的所有行,然后使用dense_rankrank

;with temp as 
(
    select id, 
      name, 
      school, 
      [date], 
      Row_number() over(partition by id order by [date] desc) as [Rn] 
      -- rank()\dense_rank() to get all latest date. 
    from yourTable 
) 
select  id, 
      name, 
      school, 
      [date] 
from temp 
where Rn = 1 

演示链接:http://rextester.com/YTH8889

+0

我仍然希望日期的值 –

+0

您可以选择任何您想要的列,我使用包含'日期'列的'select *'已经 – TriV

+0

某些ID仍在重复...我只需要该行最近可用的 –