SQL查询唯一的数据并按时间降序怎么写?

2024-11-26 05:18:50
推荐回答(2个)
回答1:

Select A.sendtoWhom,
sendDate = Isnull((
Select Top 1 B.sendDate From sendChat B
Where B.sendtoWhom = A.sendtoWhom
And B.sendEmail='aa@sina.com'
Order by B.sendDate Desc
),'')
From
(select distinct sendtoWhom
from sendChat
where sendEmail='aa@sina.com'
order by sendDate desc) A

回答2:

对于重复项的日期,你想要哪一个?你可以先试试这个:
select sendtoWhom,'sendDate' = Max(sendDate) from sendChat
where sendEmail='aa@sina.com'
group by sendtoWhom
order by sendDate desc