读书人

求最大值有关问题

发布时间: 2012-04-10 21:03:56 作者: rapoo

求最大值问题
declare @maxdate datetime
select @maxdate = max(ApplyDate) from dbo.Apply where DepartmentName = '生产部 '
select ApplyNumber from dbo.Apply where DepartmentName = '生产部 ' and ApplyDate = @maxdate

如何把上面两个查询语句合并为一条查询语句???

[解决办法]
select ApplyNumber from dbo.Apply where DepartmentName = '生产部 ' and ApplyDate = (select max(ApplyDate) from dbo.Apply where DepartmentName = '生产部 ')

[解决办法]
----方法1:
select ApplyNumber from dbo.Apply where DepartmentName = '生产部 ' and ApplyDate =
(select max(ApplyDate) from dbo.Apply where DepartmentName = '生产部 ')

----方法2:
select ApplyNumber from dbo.Apply where DepartmentName = '生产部 ' and ApplyDate =
(select top 1 ApplyDate from dbo.Apply where DepartmentName = '生产部 ' order by ApplyDate DESC)

----方法3:
select ApplyNumber from dbo.Apply as a where DepartmentName = '生产部 ' and
not exists(select 1 from dbo.Apply where DepartmentName = '生产部 ' and ApplyDate > a.ApplyDate)

读书人网 >SQL Server

热点推荐