将三种选择合二为一

时间:2018-02-08 15:39:35

标签: sql sql-server optimization

我一直在尝试将这三个SQL函数合并为一个。

功能

功能1

`ALTER FUNCTION [dbo].[Fn_Get_User] (@_CompanyKey INT)
RETURNS VARCHAR(8000)
AS
BEGIN
DECLARE @UserIDs AS VARCHAR(8000);
SET @UserIDs = '';
    SELECT @UserIDs = @UserIDs + ', ' + x.User_ID
    FROM 
    (SELECT DISTINCT UPPER(p.User_ID) as User_ID 
    FROM  [dbo].[Program] AS p WITH (NOLOCK) 
    WHERE p.CompanyKey = @_CompanyKey
    UNION
    SELECT DISTINCT UPPER(ps.User_ID) as User_ID 
    FROM  [dbo].[Program] AS p WITH (NOLOCK) 
LEFT OUTER JOIN [dbo].[Program_Scenario] AS ps WITH (NOLOCK) 
    ON p.ProgramKey = ps.ProgramKey
    WHERE p.CompanyKey = @_CompanyKey
        AND ps.User_ID IS NOT NULL) x 
    RETURN Substring(@UserIDs, 3, 8000);
END`

功能2

`ALTER FUNCTION [dbo].[Fn_Get_Source] (@_CompanyKey INT)
RETURNS VARCHAR(8000)
AS
BEGIN
    DECLARE @System_IDs AS VARCHAR(8000);
    SET @System_IDs = '';
    SELECT @System_IDs = @System_IDs + ', ' + Original_Source_System_ID
    FROM (
        SELECT DISTINCT p.Original_Source_System_ID
        FROM [dbo].[Program] AS p WITH (NOLOCK) 
        WHERE p.CompanyKey = @_CompanyKey
    ) a
    RETURN Substring(@System_IDs, 3, 8000);
END`

功能3

`ALTER FUNCTION [dbo].[Fn_Get_ContractNbr] (@_CompanyKey INT)
RETURNS VARCHAR(8000)
AS
BEGIN
    DECLARE @ContractNums AS VARCHAR(8000);
    SET @ContractNums = '';
    SELECT @ContractNums = @ContractNums + ', ' + bi.Contract_Nbr + '-' + 
ISNULL(CAST(bi.Year_Nbr AS varchar(8)),'') + '-' +
ISNULL(CAST(bi.Layer_Nbr AS varchar(8)),'')
    FROM [dbo].[Program] AS p WITH (NOLOCK) 
    INNER JOIN [dbo].[Program_Scenario] AS ps WITH (NOLOCK) ON p.ProgramKey = ps.ProgramKey
    INNER JOIN [dbo].[Layer] AS l WITH (NOLOCK) ON ps.ProgramScenarioKey = l.ProgramScenarioKey
    INNER JOIN [dbo].[Bound_Information] AS bi WITH (NOLOCK) ON l.LayerKey = bi.LayerKey
        AND ISNULL(bi.Contract_Nbr,'') <> '' --IS NOT NULL
    WHERE p.CompanyKey = @_CompanyKey
    RETURN Substring(@ContractNums, 3, 8000);
END`

我的尝试解决方案

`SET STATISTICS TIME ON
SELECT DISTINCT UPPER((p.User_ID)  +', '+ ps.User_ID) as UserID,
        bi.Contract_Nbr as ContractNumber, 
        p.Original_Source_System_ID as SourceSysID,
        ISNULL(CAST(bi.Year_Nbr AS varchar(8)),'') as UW_Year,
        ISNULL(CAST(bi.Layer_Nbr AS varchar(8)),'') as LayerNumber, 
        p.CompanyKey,
        ps.ProgramKey,
        c.Company_Name  
    FROM 
        [dbo].[Program] AS p WITH (NOLOCK) 
    INNER JOIN [dbo].[Program_Scenario] AS ps WITH (NOLOCK) 
        ON p.ProgramKey = ps.ProgramKey
    INNER JOIN [dbo].[Layer] AS l WITH (NOLOCK) ON ps.ProgramScenarioKey = l.ProgramScenarioKey
    INNER JOIN [dbo].[Bound_Information] AS bi WITH (NOLOCK) ON l.LayerKey = bi.LayerKey
    INNER JOIN [dbo].[Company] AS c with (NOLOCK) ON p.CompanyKey = c.CompanyKey
    AND ISNULL(bi.Contract_Nbr,'') <>'' 
    AND ps.User_ID IS NOT NULL
        WHERE p.CompanyKey between 2 and 100000
        ORDER BY ProgramKey`

我的数据集是20k +行,所以我不能在这里分享它,但是在我努力的地方,我的功能并没有返回与上述三个函数相同的行数。可能有任何人看到可能解决这个问题? (分别为24755行与24052行)

0 个答案:

没有答案