Sql 仅在最大值字段上选择行
名称、学校和日期都来自不同的表,即名称、学校和日期通过内部联接连接在一起 我只想选择具有最新日期的行。结果应该是:Sql 仅在最大值字段上选择行,sql,sql-server,Sql,Sql Server,名称、学校和日期都来自不同的表,即名称、学校和日期通过内部联接连接在一起 我只想选择具有最新日期的行。结果应该是: 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 试试这个: 从表t1中选择* 不
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
试试这个:
从表t1中选择*
不存在的地方
从表t2中选择1
其中t1.id=t2.id
和t1.name=t2.name
和t1.dateid name school date
1 John RKS 1/4/2008
2 Sera WCS 1/4/2011
3 Jack KIL 1/3/2017
你可以用排号。
如果希望所有行都具有最新日期,请使用稠密的秩或秩
演示链接:您尝试了什么?有很多方法可以做到这一点。给我吧……我没有试过,因为我不知道从哪里开始。第一个表不是一个数据基表,而是一个连接查询的结果。有3个表与上面的第一个列表相结合。我仍然想要date的值。您可以选择任何您想要的列,我使用select*,其中包含日期列alreadysome id仍在重复…我只需要最近可用的行。怎么可能?请看我的示例第一个表中的值是我查询的结果…三个表通过内部连接连接在一起…因此名称、学校和日期是三个不同的表..请从这里开始没有tb1.school,它的tb2.school和tb3.date您能给我所有的表结构吗?
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
)
;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