0

以下にスカラー値関数がありますが、CASE WHEN 式を追加したいのですが、今のところ正しい構文を取得できていません。

USE [MYDatabaseName]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER FUNCTION [dbo].[leaveBalanceByEmployeeID] (@EmployeeID INT)
RETURNS decimal(10,1)
AS
BEGIN
  DECLARE @returnValue decimal(10,1)
 SELECT @returnValue = MIN(dbo.LeaveMaster.LeaveDaysCarriedFoward + dbo.MonthsInService(dbo.LeaveMaster.DaysCarriedFowardCutoffDate, GETDATE()) 
            * dbo.EmployeeTypes.MonthlyLeaveAssignment)  - (SELECT     ISNULL(SUM(ActualLeaveDaysTaken), 0) AS LV
           FROM  dbo.LeaveApplications
           WHERE (EmployeeRecordID = dbo.EmployeeMaster.id AND HRMApproval = 'True' AND MarkAsDeleted = 'False'))  - CASE WHEN 0 <
           (SELECT ISNULL(SUM(DaysAuthorised),0) AS ECL
           FROM  dbo.CompassionateLeaveApplications AS CompassionateLeaveApplications_2
           WHERE (MasterEmployeeID = dbo.EmployeeMaster.id AND YEAR(CompassionateLeaveApplications_2.AuthorisedFromDate) =YEAR(GETDate())))  - ISNULL(MAX(dbo.Districts.CompassionateDays),0) THEN
           (SELECT  ISNULL(SUM(DaysAuthorised),0) AS ECL
           FROM   dbo.CompassionateLeaveApplications AS CompassionateLeaveApplications_2
           WHERE  (MasterEmployeeID = dbo.EmployeeMaster.id) AND YEAR(CompassionateLeaveApplications_2.AuthorisedFromDate) =YEAR(GETDate())) - ISNULL(MAX(dbo.Districts.CompassionateDays),0) ELSE 0 END                                                     
FROM       dbo.EmployeeMaster Full outer join dbo.LeaveMaster ON dbo.EmployeeMaster.id = dbo.LeaveMaster.EmpRecordID Full outer join
                      dbo.EmployeeTypes ON dbo.EmployeeMaster.EmployeeType = dbo.EmployeeTypes.ID Full outer join
                      Human_Resources.Departments ON dbo.EmployeeMaster.DepartmentId = Human_Resources.Departments.DepartmentID Full outer join
                      dbo.StaffHomeAddresses ON dbo.EmployeeMaster.id = dbo.StaffHomeAddresses.StaffID Full outer join
                      dbo.CompassionateLeaveApplications AS CompassionateLeaveApplications_1 ON 
                      dbo.EmployeeMaster.id = CompassionateLeaveApplications_1.MasterEmployeeID Full outer join
                      dbo.Districts ON dbo.StaffHomeAddresses.District = dbo.Districts.DistrictID
                      WHERE    dbo.EmployeeMaster.id = @EmployeeID
GROUP BY dbo.EmployeeMaster.id
return @returnValue
END

現在、テーブル dbo.EmployeeMaster には、IsActive という名前のビット列があります。私が達成したいのは、IsActive 列 = True の場合、@returnValue は上記のクエリが返すものを返す必要があります。それ以外の場合、IsActive = False の場合、0.0 を返したいだけです。

4

2 に答える 2

0

アクティブでない場合は、変数を目的の値で初期化するだけです。

DECLARE @returnValue decimal(10,1) = 0.0

Where calusule を変更して、アクティブかどうかを確認します

 WHERE    dbo.EmployeeMaster.id = @EmployeeID and dbo.EmployeeMaster.IsActive = 1

その条件を追加することにより、変数はアクティブな場合にのみ更新されます。別の場合には、初期化で使用した値が含まれます。

USE [MYDatabaseName]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER FUNCTION [dbo].[leaveBalanceByEmployeeID] (@EmployeeID INT)
RETURNS decimal(10,1)
AS
BEGIN
  DECLARE @returnValue decimal(10,1) = 0.0
 SELECT @returnValue = MIN(dbo.LeaveMaster.LeaveDaysCarriedFoward + dbo.MonthsInService(dbo.LeaveMaster.DaysCarriedFowardCutoffDate, GETDATE()) 
            * dbo.EmployeeTypes.MonthlyLeaveAssignment)  - (SELECT     ISNULL(SUM(ActualLeaveDaysTaken), 0) AS LV
           FROM  dbo.LeaveApplications
           WHERE (EmployeeRecordID = dbo.EmployeeMaster.id AND HRMApproval = 'True' AND MarkAsDeleted = 'False'))  - CASE WHEN 0 <
           (SELECT ISNULL(SUM(DaysAuthorised),0) AS ECL
           FROM  dbo.CompassionateLeaveApplications AS CompassionateLeaveApplications_2
           WHERE (MasterEmployeeID = dbo.EmployeeMaster.id AND YEAR(CompassionateLeaveApplications_2.AuthorisedFromDate) =YEAR(GETDate())))  - ISNULL(MAX(dbo.Districts.CompassionateDays),0) THEN
           (SELECT  ISNULL(SUM(DaysAuthorised),0) AS ECL
           FROM   dbo.CompassionateLeaveApplications AS CompassionateLeaveApplications_2
           WHERE  (MasterEmployeeID = dbo.EmployeeMaster.id) AND YEAR(CompassionateLeaveApplications_2.AuthorisedFromDate) =YEAR(GETDate())) - ISNULL(MAX(dbo.Districts.CompassionateDays),0) ELSE 0 END                                                     
FROM       dbo.EmployeeMaster Full outer join dbo.LeaveMaster ON dbo.EmployeeMaster.id = dbo.LeaveMaster.EmpRecordID Full outer join
                      dbo.EmployeeTypes ON dbo.EmployeeMaster.EmployeeType = dbo.EmployeeTypes.ID Full outer join
                      Human_Resources.Departments ON dbo.EmployeeMaster.DepartmentId = Human_Resources.Departments.DepartmentID Full outer join
                      dbo.StaffHomeAddresses ON dbo.EmployeeMaster.id = dbo.StaffHomeAddresses.StaffID Full outer join
                      dbo.CompassionateLeaveApplications AS CompassionateLeaveApplications_1 ON 
                      dbo.EmployeeMaster.id = CompassionateLeaveApplications_1.MasterEmployeeID Full outer join
                      dbo.Districts ON dbo.StaffHomeAddresses.District = dbo.Districts.DistrictID
                      WHERE    dbo.EmployeeMaster.id = @EmployeeID and dbo.EmployeeMaster.IsActive = 1 
GROUP BY dbo.EmployeeMaster.id
return @returnValue
END
于 2013-09-02T10:55:56.997 に答える