2017-05-24 57 views
0

我有以下plpgsql過程/函數,其中我嘗試使用Auto_increment值獲取一組記錄。我編寫了「位置」來存儲自動增量,我猜這是錯誤的,但我不知道如何解決這個查詢,我想添加一個新的計算字段。我認爲我的意圖很清楚。有人可以幫助我嗎?,請!plpgsql過程:如何將auto_increment字段添加到查詢中

CREATE OR REPLACE FUNCTION pra2.GetRankingOfPilots(IN sea pra2.season.name%type) 
RETURNS setof RECORD AS $BODY$ 
DECLARE 
    r RECORD; 
    fila integer; 
BEGIN 
    fila:=0; 
    FOR r IN SELECT 
      position, 
      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 
     ORDER BY 
      TotalPuntos Desc 
    LOOP 
     fila:=fila+1; 
     r.posicion:=fila; 
     RETURN NEXT r; 
    END LOOP; 
RETURN; 
END; 
$BODY$ 
LANGUAGE plpgsql; 

預先感謝您!

回答

0

嘗試:

SELECT 
      row_number() over() position, 
      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 
     ORDER BY 
      TotalPuntos Desc 
+0

我得到的類型錯誤,說我不能投BIGINT改變創造功能 – galeonweb

+0

的返回類型刪除它,然後再次創建 –

+0

或不管它是什麼之前 –

相關問題