如何获得具有最新日期和最低价值的唯一名称。
名称日期值
布拉德1/2/10 1.1
布拉德1/2/10 2.3
鲍勃1/6/10 1.0
布拉德2/4/09 13.2
此查询似乎不起作用
SELECT distinct
A.[ViralLoadMemberID]
,B.LastName
,B.FirstName
,A.[Date]
,A.[vaule]
FROM [t].[dbo].[tblViralLoad] A
left join [dbo].[tblEnrollees] B on A.ViralLoadMemberID = B.MemberID
where
A.Date =
(
select MAX(Date)
from dbo.tblViralLoad
where ViralLoadMemberID = A.ViralLoadMemberID
and
( Date >= '07/01/2014'
and Date <= '12/3/2014' ) )
这个想法是只使用order by
和获取一行。如果您想要最新日期的最低值,则标准SQL将是:
select t.*
from table t
order by desc desc, value asc
fetch first 1 row only;
对于旧版本的SQL Server,您将省略最后一行,并执行select top 1 * . . .
。对于MySQL,最后一行是limit 1
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句