简体   繁体   English

如何使用 case 语句中的函数返回值以选择每个函数的 MAX?

[英]How do I return a value using a function within a case statement to choose the MAX of each function?

I have a query where I am calculating the difference between multiple date/times using a function.我有一个查询,我正在使用函数计算多个日期/时间之间的差异。 It returns the time between the 2 dates (imported_date and create_date for the first line and picked_date and packed_on_date on the second line) in minutes.它以分钟为单位返回 2 个日期之间的时间(第一行是 imported_date 和 create_date,第二行是picked_date 和packed_on_date)。

Examples below: dbo.WorkTime(o.imported_date,pkd.create_date) dbo.WorkTime(pkd.picked_date, hums.packed_on_date)示例如下: dbo.WorkTime(o.imported_date,pkd.create_date) dbo.WorkTime(pkd.picked_date, hums.packed_on_date)

My desired result would be to return a status for each line based on the process that took the longest time.我想要的结果是根据花费时间最长的过程为每一行返回一个状态。 If I were doing this in excel I would create an IF statement.如果我在 excel 中这样做,我会创建一个 IF 语句。

Is there a way for me to write a case statement or something else that will return a status based on the MAX of each functions process times?有没有办法让我写一个 case 语句或其他什么东西,它会根据每个函数处理时间的 MAX 返回一个状态?

Try this As query试试这个 As 查询

select   ( case when dbo.WorkTime(o.imported_date,pkd.create_date) >=
dbo.WorkTime(pkd.picked_date, hums.packed_on_date) then 
dbo.WorkTime(o.imported_date,pkd.create_date) else 
dbo.WorkTime(pkd.picked_date, hums.packed_on_date) end )  MaxTime

or declare a variable或者声明一个变量

 declare @MaxTime float;
 Set  @MaxTime = ( case when dbo.WorkTime(o.imported_date,pkd.create_date) >= 
 dbo.WorkTime(pkd.picked_date, hums.packed_on_date) then
 dbo.WorkTime(o.imported_date,pkd.create_date) else  
 dbo.WorkTime(pkd.picked_date, hums.packed_on_date)
 end )  MaxTime
 return @MaxTime;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM