日期:2014-05-18  浏览次数:20483 次

求最大值问题
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)