检查3个表的SQL函数

时间:2013-07-11 09:34:25

标签: sql function

我有函数的语法,但是如果我将条件放入代码(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然后返回“否”否则如果签约晚于T1.Date然后返回“是”

这是我的尝试不是很好,朋友建议在我的函数中使用if else staement但不知道如何实现lol

任何帮助将不胜感激

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

1 个答案:

答案 0 :(得分:0)

您的主查询可以略微简化,但仍然给出相同的结果(我认为 - 没有测试数据,这很难说):

    SELECT @Answer = CASE
            WHEN (@OrganisationType  = 'PLC' OR @OrganisationType  = 'LTD') AND @ContractDate < [U].[Date] AND [U].[CTPS] = 0 THEN 'No'
            WHEN (@OrganisationType  = 'PLC' OR @OrganisationType  = 'LTD') AND @ContractDate < [U].[Date] AND [U].[CTPS] = 1 THEN 'No' 
            WHEN (@OrganisationType != 'PLC' OR @OrganisationType != 'LTD') AND @ContractDate < [U].[Date] AND [U].[CTPS] = 0 THEN 'No'
            WHEN (@OrganisationType != 'PLC' OR @OrganisationType != 'LTD') AND @ContractDate < [U].[Date] AND [U].[CTPS] = 1 THEN 'No'
            WHEN (@OrganisationType  = 'PLC' OR @OrganisationType  = 'LTD') AND @ContractDate > [U].[Date] AND [U].[CTPS] = 0 THEN 'No'
            WHEN (@OrganisationType != 'PLC' OR @OrganisationType != 'LTD') AND @ContractDate > [U].[Date] AND [U].[CTPS] = 1 THEN 'Yes'
            WHEN (@OrganisationType != 'PLC' OR @OrganisationType != 'LTD') 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 [OrganisationType] o ON [p].[OrganisationTypeFK] = [o].[OrganisationTypeID] 
        INNER JOIN [Line] l ON [l].[ContractFK] = [c].[ContractID]
            INNER JOIN [Lookup] U ON U.CLI  = l.LineNumber
        LEFT JOIN [Deal] d ON [c].[ContractID] = [d].[ContractFK]
            INNER JOIN  [Branch] b ON [d].[BranchFK] = [b].[BranchID] AND BranchTypeFK IN (1,2,4)
    WHERE [U].[CLI] = @LineNumber
      AND [U].[Date] IS NOT NULL

您还会在功能底部返回@Answer两次,并且在某些情况下@Answer可能仍然是NULL;这可能是故意的。除此之外,如果它满足您的业务需求并以可接受的速度执行,则没有真正的驱动程序可以进一步重写此功能。