要实现功能:以做K3时间与安装完成时间较晚的时间作为工程师奖金核算月份。
select a.IAId,a.achproid,b.InstallId,b.InstallDate,ap.K3Date
from CRM_ApplyProduct a
left join CRM_InstallApply b on b.Id=a.ApplyId
left join CRM_AchievementProduct ap on ap.Id=a.achproid
where datediff(month,(select max(temp) from(select temp=b.InstallDate union all select temp=ap.K3Date)temp),cast(@YearMonth as datetime))=0
查询两个值中较大值方法如下:
方法一、使用union all 将两个字段合并为一个字段的结果集
select max(temp) from(select temp=100 union all select temp=200)temp
方法二、使用case when 条件查询
select case when x<y then y else x end
方法三、自定义函数
create function MaxValue(@x int,@y int)
returns int
as
begin
declare @result int
if @x>@y
set @result=@x
else
set @result=@y
return @result
end
go
select dbo.MaxValue(100,200)
go