MS SQL用两个字段中较大的值为条件进行查询
生活随笔
收集整理的這篇文章主要介紹了
MS SQL用两个字段中较大的值为条件进行查询
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
要實(shí)現(xiàn)功能:以做K3時(shí)間與安裝完成時(shí)間較晚的時(shí)間作為工程師獎(jiǎng)金核算月份。
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查詢兩個(gè)值中較大值方法如下:
方法一、使用union all 將兩個(gè)字段合并為一個(gè)字段的結(jié)果集
方法二、使用case when 條件查詢
select case when x<y then y else x end方法三、自定義函數(shù)
create function MaxValue(@x int,@y int) returns int as begin declare @result intif @x>@y set @result=@xelseset @result=@yreturn @result end go select dbo.MaxValue(100,200) go總結(jié)
以上是生活随笔為你收集整理的MS SQL用两个字段中较大的值为条件进行查询的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2022年3月语音合成(TTS)和语音识
- 下一篇: 题解 - CF613D Kingdom