更新时间:2022-10-17 22:45:21
用例:
当@PaidThisMonth
作为内联表值 UDF
创建函数最小值(@Param1 整数,@Param2 整数)返回表为返回(当@Param1
用法:
选择 MinValue 作为 PaidforPast来自 dbo.Minimum(@PaidThisMonth, @OwedPast)
附录:这可能最适合仅处理两个可能的值,如果有两个以上,请考虑使用 Values 子句的 Craig 的回答.>
I have two variables, one is called PaidThisMonth
, and the other is called OwedPast
. They are both results of some subqueries in SQL. How can I select the smaller of the two and return it as a value titled PaidForPast
?
The MIN
function works on columns, not variables.
Use Case:
Select Case When @PaidThisMonth < @OwedPast
Then @PaidThisMonth Else @OwedPast End PaidForPast
As Inline table valued UDF
CREATE FUNCTION Minimum
(@Param1 Integer, @Param2 Integer)
Returns Table As
Return(Select Case When @Param1 < @Param2
Then @Param1 Else @Param2 End MinValue)
Usage:
Select MinValue as PaidforPast
From dbo.Minimum(@PaidThisMonth, @OwedPast)
ADDENDUM: This is probably best for when addressing only two possible values, if there are more than two, consider Craig's answer using Values clause.