首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

求最大值有关问题

2012-04-10 
求最大值问题declare@maxdatedatetimeselect@maxdatemax(ApplyDate)fromdbo.ApplywhereDepartmentName生

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

热点排行