求和SQL Server的两个函数调用的结果

时间:2015-04-27 14:56:00

标签: sql sql-server function

我想有效地对两个函数调用的结果进行求和,并在第三列中显示该值。这是我的语法......

SELECT TOP (1000) Region, 
dbo.fn_Function1(Code, 2016) AS [Highest Students], 
dbo.fn_Function2(Code, 2016) AS [Highest Schools],
dbo.fn_Function3(Code, 2016) AS [Average Students],
dbo.fn_Function4(Code, 2016) AS [Average Schools], 
dbo.fn_Calculator(0,0) as [Total Students], 
dbo.fn_Calculator(0,0) as [Total Schools]
From myTable

fn_calculator应该将(最高学生+普通学生)的值作为参数并将它们相加。同样适用于(最高学校+普通学校)

我宁愿不在计算器函数的参数内再次重新运行这些函数。我现在只有(0,0)作为占位符。我试着像这样加载计算器:但是它不能识别列名:

dbo.fn_Calculator([Highest Students], [Average Students])

2 个答案:

答案 0 :(得分:1)

你可以这样做

select top (1000)
    Region, 
    calc.[Highest Students], 
    calc.[Highest Schools],
    calc.[Average Students],
    calc.[Average Schools], 
    dbo.fn_Calculator(calc.[Highest Students], calc.[Average Students]) as [Total Students], 
    dbo.fn_Calculator(calc.[Highest Schools], calc.[Average Schools]) as [Total Schools]
From myTable as t
    outer apply (select
        dbo.fn_Function1(Code, 2016) AS [Highest Students], 
        dbo.fn_Function2(Code, 2016) AS [Highest Schools],
        dbo.fn_Function3(Code, 2016) AS [Average Students],
        dbo.fn_Function4(Code, 2016) AS [Average Schools]
    ) as calc

但总的来说,@ TimSchmelter对于避免使用函数是非常正确的 - 如果可能的话 - 标量函数通常对性能非常不利。在您的情况下,您至少可以删除fn_calculator函数:

select top (1000)
    Region, 
    calc.[Highest Students], 
    calc.[Highest Schools],
    calc.[Average Students],
    calc.[Average Schools], 
    calc.[Highest Students] + calc.[Average Students] as [Total Students], 
    calc.[Highest Schools] + calc.[Average Schools] as [Total Schools]
From myTable as t
    outer apply (select
        dbo.fn_Function1(Code, 2016) AS [Highest Students], 
        dbo.fn_Function2(Code, 2016) AS [Highest Schools],
        dbo.fn_Function3(Code, 2016) AS [Average Students],
        dbo.fn_Function4(Code, 2016) AS [Average Schools]
    ) as calc

您也可以使用cte:

;with cte as (
    select top (1000)
        t.Region,
        dbo.fn_Function1(Code, 2016) AS [Highest Students], 
        dbo.fn_Function2(Code, 2016) AS [Highest Schools],
        dbo.fn_Function3(Code, 2016) AS [Average Students],
        dbo.fn_Function4(Code, 2016) AS [Average Schools]
    From myTable as t   
)
select
     c.Region,
     c.[Highest Students], 
     c.[Highest Schools],
     c.[Average Students],
     c.[Average Schools], 
     dbo.fn_Calculator(c.[Highest Students], c.[Average Students]) as [Total Students], 
     dbo.fn_Calculator(c.[Highest Schools], c.[Average Schools]) as [Total Schools]
from cte as c

答案 1 :(得分:1)

最有效的方法是避免这些功能。但您可以使用子查询或CTE:

WITH Calcs AS
(
   SELECT Region, 
    dbo.fn_Function1(Code, 2016) AS [Highest Students], 
    dbo.fn_Function2(Code, 2016) AS [Highest Schools],
    dbo.fn_Function3(Code, 2016) AS [Average Students],
    dbo.fn_Function4(Code, 2016) AS [Average Schools], 
    From myTable
)
SELECT TOP 1000 
     [Highest Students],
     [Highest Schools],
     [Average Students],
     [Average Schools],
    dbo.fn_Calculator([Highest Students], [Average Students]) as [Total Students], 
    dbo.fn_Calculator([Highest Schools], [Average Schools]) as [Total Schools]
FROM Calcs