0
我有這個函數的語法,但是我完全沒用,至於如何將條件放入代碼(SQL),所以它會返回一個答案。檢查3個表的SQL函數
我有三個表,
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)
和三個變量,
@LNumber varchar(max),
@OType varchar(max),
@CDate datetime
我需要的功能,採取行號進入表1檢查數量在那裏,如果它是那麼需要獲取OType名稱,如果是LTD或PLC,則需要檢查CPTS標誌是否設置爲1,如果是,那麼它需要檢查契約日期是否對T1.Date,如果在T1.Date之前有contrcatdate,則返回「否「如果contractdate晚於T1.Date,則返回」Yes「
這裏是我的學嘗試沒有很好的,用我的功能的,如果別人staement但不知道如何實現,要麼笑
任何幫助,將不勝感激
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, ,> [email protected] = 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
爲什麼你不簡化你的病例陳述有兩個WHEN(對於兩個YES)和一個ELSE對於所有的NO? – openshac
我可以,好主意 - 但可能會出現這樣的情況,其中沒有一個條件是真的,所以'@ answer'應該返回NULL。 OP - 如果'Yes'條件都不匹配,應該'返回'而不是返回NULL值。 –
不,你的答案只是返回NULL爲所有行ow我會轉換爲if其他也許會更好? – LoveToys