私は関数の構文を持っていますが、条件をコード (SQL) に入れる方法についてはまったく役に立たないので、答えが返されます。
私は3つのテーブルを持っています、
T1.number varchar(max)
T1.date datetime
T1.ctps int
T2.number varchar(max)
T2.addedDate date
T2.RemovedDate date
T2.ctps int
T3.OType varchar(max)
T3.LNumber varchar(max)
および 3 つの変数、
@LNumber varchar(max),
@OType varchar(max),
@CDate datetime
行番号をテーブル1に入れる関数が必要です。番号がある場合はOType名を取得する必要があり、それがLTDまたはPLCの場合はCPTSフラグが1に設定されているかどうかを確認する必要がありますそうである場合は、T1.Date に対して contractdate をチェックする必要があります。T1.Date より前の contrcatdate の場合は「いいえ」を返します。
ここに私の試みはあまり良くありません.友人は私の関数でif else文を使用することを提案しましたが、それを実装する方法もわかりません笑
任意の助けをいただければ幸いです
IF OBJECT_ID(N'TPS_Checker', N'FN') IS NOT NULL
DROP FUNCTION TPS_Checker ;
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date, ,>
-- Description: <Description, ,> --@LineNumber = u.CLI, @OrganisationType = o.Name, @ContractDate = c.ContractDate
-- =============================================
CREATE FUNCTION TPS_Checker
(@LineNumber VARCHAR(MAX), @ContractDate DATETIME, @OrganisationType VARCHAR)
RETURNS VARCHAR(4)
AS
BEGIN
DECLARE @Answer varchar(4)
SELECT @Answer = CASE
WHEN ((((((@LineNumber = [U].[CLI]) AND @OrganisationType = 'PLC') OR @OrganisationType = 'LTD') AND [U].[Date] IS NOT NULL) AND @ContractDate < [U].[Date]) AND [U].[CTPS] = 0) THEN 'No'
WHEN ((((((@LineNumber = [U].[CLI]) AND @OrganisationType = 'PLC') OR @OrganisationType = 'LTD') AND [U].[Date] IS NOT NULL) AND @ContractDate < [U].[Date]) AND [U].[CTPS] = 1) THEN 'No'
WHEN ((((((@LineNumber = [U].[CLI]) AND @OrganisationType != 'PLC') OR @OrganisationType != 'LTD') AND [U].[Date] IS NOT NULL) AND @ContractDate < [U].[Date]) AND [U].[CTPS] = 0) THEN 'No'
WHEN ((((((@LineNumber = [U].[CLI]) AND @OrganisationType != 'PLC') OR @OrganisationType != 'LTD') AND [U].[Date] IS NOT NULL) AND @ContractDate < [U].[Date]) AND [U].[CTPS] = 1) THEN 'No'
WHEN ((((((@LineNumber = [U].[CLI]) AND @OrganisationType = 'PLC') OR @OrganisationType = 'LTD') AND [U].[Date] IS NOT NULL) AND @ContractDate > [U].[Date]) AND [U].[CTPS] = 0) THEN 'No'
WHEN ((((((@LineNumber = [U].[CLI]) AND @OrganisationType != 'PLC') OR @OrganisationType != 'LTD') AND [U].[Date] IS NOT NULL) AND @ContractDate > [U].[Date]) AND [U].[CTPS] = 1) THEN 'Yes'
WHEN ((((((@LineNumber = [U].[CLI]) AND @OrganisationType != 'PLC') OR @OrganisationType != 'LTD') AND [U].[Date] IS NOT NULL) AND @ContractDate > [U].[Date]) AND [U].[CTPS] = 0) THEN 'Yes'
END
FROM [Contract] c
INNER JOIN [Account] a
ON [c].[AccountFK] = [a].[AccountID]
INNER JOIN [Profile] p
ON [p].[ProfileID] = [a].[ProfileFK]
INNER JOIN [Line] l
ON [l].[ContractFK] = [ContractID]
LEFT JOIN [Deal] d
ON [c].[ContractID] = [d].[ContractFK]
INNER JOIN ( SELECT BranchTypeFK, BranchID, Name
FROM [Branch] b
)b
ON [d].[BranchFK] = [b].[BranchID]
AND BranchTypeFK IN (1,2,4)
INNER JOIN [OrganisationType] o
ON [p].[OrganisationTypeFK] = [o].[OrganisationTypeID]
LEFT JOIN [Lookup] U
ON U.CLI = l.LineNumber
LEFT JOIN History H
ON h.CLI = l.LineNumber
return @answer
-- Return the result of the function
RETURN @Answer
END
select top 100 [CORPORATE\HargreavesD].[TPS_Checker1](u.CLI ,c.ContractDate,o.Name) Answer
FROM [Contract] c
INNER JOIN [Account] a
ON [c].[AccountFK] = [a].[AccountID]
INNER JOIN [Profile] p
ON [p].[ProfileID] = [a].[ProfileFK]
INNER JOIN [Line] l
ON [l].[ContractFK] = [ContractID]
LEFT JOIN [Deal] d
ON [c].[ContractID] = [d].[ContractFK]
INNER JOIN ( SELECT BranchTypeFK, BranchID, Name
FROM [Branch] b
)b
ON [d].[BranchFK] = [b].[BranchID]
AND BranchTypeFK IN (1,2,4)
INNER JOIN [OrganisationType] o
ON [p].[OrganisationTypeFK] = [o].[OrganisationTypeID]
LEFT JOIN [Lookup] U
ON U.CLI = l.LineNumber
LEFT JOIN History H
ON h.CLI = l.LineNumber
これは私の最初の試みですが、関数ではありません
DECLARE @StartDate DATE = '01/04/2013'
DECLARE @EndDate DATE = '30/04/2013'
SELECT ContractID
,LineNumber
,ContractDate
,[Date]
,o.Name
,u.[File]
,DateAdded
,DateRemoved
,u.CTPS
,B.Name [Branch Name]
END
INTO [TPS]
FROM [Contract] c
INNER JOIN [Account] a
ON [c].[AccountFK] = [a].[AccountID]
INNER JOIN [Profile] p
ON [p].[ProfileID] = [a].[ProfileFK]
INNER JOIN [Line] l
ON [l].[ContractFK] = [ContractID]
LEFT JOIN [Deal] d
ON [c].[ContractID] = [d].[ContractFK]
INNER JOIN ( SELECT BranchTypeFK, BranchID, Name
FROM [Branch] b
)b
ON [d].[BranchFK] = [b].[BranchID]
AND BranchTypeFK IN (1,2,4)
INNER JOIN [OrganisationType] o
ON [p].[OrganisationTypeFK] = [o].[OrganisationTypeID]
LEFT JOIN (
SELECT [CLI], [Date] [lDate], [Date], [CTPS], [File] = 'Lookup'
FROM [Lookup] tl
)u
ON u.CLI = l.LineNumber
LEFT JOIN (
SELECT [CLI], [DateAdded], [DateRemoved], [CTPS], [File] = 'History'
FROM History h
)h
ON h.CLI = l.LineNumber
WHERE ContractDate BETWEEN @StartDate AND @EndDate
SELECT *
,[OnTPS] = CASE
WHEN ([Date] IS NOT NULL AND [ContractDate] < [Date]) THEN 'No'
WHEN ([Date] IS NOT NULL AND [ContractDate] > [Date]) THEN 'Yes'
WHEN ([Date] IS NULL AND [ContractDate] BETWEEN [DateAdded] AND [DateRemoved]) THEN 'Yes'
WHEN ([Date] IS NULL AND [ContractDate] NOT BETWEEN [DateAdded] AND [DateRemoved]) THEN 'No'
WHEN ([Date] IS NULL AND [ContractDate] BETWEEN [DateAdded] AND [DateRemoved] AND [Name] = 'PLC' OR [Name] = 'LTD' AND CTPS = 1) THEN 'Yes'
WHEN ([Date] IS NULL AND [ContractDate] BETWEEN [DateAdded] AND [DateRemoved] AND [Name] = 'PLC' OR [Name] = 'LTD' AND CTPS = 0) THEN 'No'
WHEN ([Date] IS NOT NULL AND [ContractDate] > [Date] AND [Name] = 'PLC' OR [Name] = 'LTD' AND CTPS = 1) THEN 'Yes'
WHEN ([Date] IS NOT NULL AND [ContractDate] > [Date] AND [Name] = 'PLC' OR [Name] = 'LTD' AND CTPS = 0) THEN 'No'
END
FROM [Temp].[dbo].[TPS] t