2015-04-27 108 views
0

我想高效地總結兩個函數調用的結果並在第三列中顯示值。這裏是語法我有...總結兩個函數調用的結果SQL Server

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

最有效的方法是避免這些功能。 –

回答

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 
+0

感謝您的優雅解決方案。幸運的是,查詢需要每年只運行幾次,但是卻有數百萬學生。 – user2055729

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 
+0

感謝您的解決方案!我很欣賞時間:) – user2055729

+0

僅供參考 - 使用計算器的第一個例子工作,但第二個例子沒有添加數字,而是將它們附加在一起。 – user2055729

+0

@ user2055729然後我可以假設你的函數'fn_FunctionX'返回字符串而不是數字 –