2017-05-26 128 views
0

如何枚舉執行查詢,我通過「seasson」,它顯示了我,作爲一個結果,與司機,點,構造和位置的排名相關的信息。PostgreSQL的存儲過程:我想查詢

我想要的是有這樣的事情:

POS IN RANKING | DRIVER NAME | CONSTRUCTOR NAME | POINTS 
--------------------------------------------------------- 
    1   "Hamilton"  MC Laren   360 
    2   "Alonso"  Ferrari   290 
    ...     ...     .... 

我得到的problema的是,我無法枚舉行。我認爲「POS排名」應該是row_number()函數的結果,但對於某些reasson我不能使它工作。

這是我的存儲功能:

CREATE TYPE ranking_t AS (
pos integer, 
driver character varying(30), 
constructor character varying(30), 
points integer 
); 

CREATE OR REPLACE FUNCTION pra2.GetRankingOfPilots(sea pra2.season.name%type) 
RETURNS ranking_t AS $$ 
DECLARE 
    ranking_pilots ranking_t; 
BEGIN 
    SELECT 
     row_number() OVER (ORDER BY totalpuntos), 
     driver.name driver, 
     constructor.name constructor, 
     season.name season, 
     CAST(sum(runs.points) AS int) TotalPuntos 
    INTO ranking_pilots 
    FROM 
     pra2.hired hired 
    INNER JOIN pra2.constructor on hired.name_constructor = pra2.constructor.name 
    INNER JOIN pra2.driver on hired.num_driver = pra2.driver.num 
    INNER JOIN pra2.runs on pra2.driver.num=pra2.runs.num_driver 
    INNER JOIN pra2.race on pra2.runs.name_race=pra2.race.name AND pra2.runs.season_fk=pra2.race.season_fk AND pra2.runs.season_fk=pra2.race.season_fk 
    INNER JOIN pra2.season on hired.name_season=pra2.season.name AND pra2.race.season_fk=pra2.season.name 
    WHERE 
     pra2.season.name=sea 
    GROUP BY 
     season,driver,constructor 
    ORDER BY 
     TotalPuntos Desc; 

END; 
$$ 
LANGUAGE plpgsql; 

我將不勝感激任何意見。

預先感謝您!

+0

阿隆索不在法拉利或漢密爾頓與邁凱輪:P,只是說。 –

+1

'我無法做到這一點。'問題是什麼?任何錯誤? –

+0

是的,錯誤是,我嘗試枚舉查詢,並通過我使用的函數row_number()的方式,它會產生一個錯誤,因爲它說,字段totalpuntos不存在 – galeonweb

回答

0

我想這個問題是您正在使用的別名,而不是源。

你不能做到這一點

SELECT 
    row_number() OVER (ORDER BY totalpuntos), 
    CAST(sum(runs.points) AS int) TotalPuntos 

因此,您創建一個子查詢

SELECT row_number() OVER (ORDER BY totalpuntos) 
FROM (SELECT CAST(sum(runs.points) AS int) TotalPuntos 
     From YourQuery 
    ) as Subquery 

也許你可以使用該功能,但OVER裏面不知道,如果可能的。

SELECT 
    row_number() OVER (ORDER BY CAST(sum(runs.points) AS int)), 

編輯:爲了回報你喜歡這款

CREATE OR REPLACE FUNCTION foo(a int) 
RETURNS TABLE(b int, c int) AS $$ 
BEGIN 
    RETURN QUERY SELECT i, i+1 FROM generate_series(1, a) g(i); 
END; 
$$ LANGUAGE plpgsql; 
1

包裝查詢獲取行號的表。

另外:改變返回類型爲SETOF ranking_t,取出變量,並使用RETURN QUERY

CREATE OR REPLACE FUNCTION pra2.GetRankingOfPilots(sea pra2.season.name%type) 
RETURNS SETOF ranking_t AS $$ 
BEGIN 
    RETURN QUERY 
    SELECT row_number() OVER (ORDER BY totalpuntos)::int, * 
    FROM (
     SELECT 
      driver.name driver, 
      constructor.name constructor, 
      season.name season, 
      CAST(sum(runs.points) AS int) TotalPuntos 
     FROM 
      pra2.hired hired 
     INNER JOIN pra2.constructor on hired.name_constructor = pra2.constructor.name 
     INNER JOIN pra2.driver on hired.num_driver = pra2.driver.num 
     INNER JOIN pra2.runs on pra2.driver.num=pra2.runs.num_driver 
     INNER JOIN pra2.race on pra2.runs.name_race=pra2.race.name AND pra2.runs.season_fk=pra2.race.season_fk AND pra2.runs.season_fk=pra2.race.season_fk 
     INNER JOIN pra2.season on hired.name_season=pra2.season.name AND pra2.race.season_fk=pra2.season.name 
     WHERE 
      pra2.season.name=sea 
     GROUP BY 
      season,driver,constructor 
     ) s 
    ORDER BY 
     TotalPuntos Desc; 
END; 
$$ 
LANGUAGE plpgsql; 
+0

無法正常工作。它說「執行到達函數的末尾而沒有找到返回」 – galeonweb

+0

您的原始查詢具有'SELECT INTO',但您的存儲過程具有'RETURNS ranking_t',因此您沒有'RETURN',但這是一個不同的錯誤 –

+0

好吧,現在工作,但它只給了我一行,它應該顯示更多:/ – galeonweb