0
我有一個用戶定義的標量函數,名爲fn_CountWeekDays,它接受2個參數startdate和enddate並返回一個整數值。該功能基本上計算開始和結束日期之間的平日的no。在case語句中使用標量函數
功能代碼:
CREATE FUNCTION [dbo].[fn_CountWeekDays]
(
@fromdate Datetime,
@todate Datetime
)
RETURNS INT AS
Begin
Declare @NoOfWeekDays int
Set @NoOfWeekDays = (DATEDIFF(dd, @fromdate, @todate) + 1)
-(DATEDIFF(wk, @fromdate, @todate) * 2)
-(CASE WHEN DATENAME(dw, @fromdate) = 'Sunday' THEN 1 ELSE 0 END)
-(CASE WHEN DATENAME(dw, @todate) = 'Saturday' THEN 1 ELSE 0 END)
Return @NoOfWeekDays
End
如何調用該函數在一個case語句,並將其與其他整數值在同一時間比較?
下面是我的查詢:
select
Case
when Priority in ('1 - Critical','2 - High') and fn_CountWeekDays(Opened,Closed) <= 3 and ProblemState = 'Closed/Resolved' then 1 else 0
End as RCA_KPI_P1_P2_Met,
Case
when Priority = '3 - Moderate' and fn_CountWeekDays(Opened,Closed) <= 5 and ProblemState = 'Closed/Resolved' then 1 else 0
End as RCA_KPI_P3_Met
from v_Problem
這不起作用?它看起來應該是 – nimdil
我希望這也可以工作。從性能角度來看,將函數重寫爲表值函數會更好 – HoneyBadger
如何使用表值函數?我已更新該帖子以包含功能代碼。 –