我想用2個參數創建一個函數。這是我的SELECT
聲明:用SELECT CASE創建函數
SELECT CASE
WHEN duration <= 10000000 THEN '00-01 sec'
WHEN duration <= 40000000 THEN '01-04 sec'
WHEN duration <= 100000000 THEN '04-10 sec'
WHEN duration <= 300000000 THEN '10-30 sec'
WHEN duration <= 600000000 THEN '30-60 sec'
ELSE 'more than 60 sec' END AS "Kategorien",
COUNT(*) AS Requestcounter
FROM tablename
WHERE starttime BETWEEN '2016-03-01 00:00:00' AND '2016-03-08 14:00:00'
GROUP BY
(CASE WHEN duration <= 10000000 THEN '00-01 sec'
WHEN duration <= 40000000 THEN '01-04 sec'
ELSE 'more than 60 sec' END);
結果:
Kategorien | requestcounter
------------+----------------
00-01 sec | 2073
01-04 sec | 2
(2 rows)
我想從我的函數這一結果。但我得到的錯誤:
query has no destination for result data
這是我的函數:
CREATE OR REPLACE FUNCTION requestcounter(mindate timestamptz,maxdate timestamptz)
RETURNS SETOF integer AS $$
BEGIN
SELECT CASE WHEN duration <= 10000000 THEN '00-01 sec'
WHEN duration <= 40000000 THEN '01-04 sec'
ELSE 'more than 60 sec' END AS "Kategorien",
COUNT(*) AS Requestcounter from tablename where starttime BETWEEN mindate and maxdate
GROUP BY
(CASE WHEN duration <= 10000000 THEN '00-01 sec'
WHEN duration <= 40000000 THEN '01-04 sec'
ELSE 'more than 60 sec' END);
Return;
END;
$$ LANGUAGE plpgsql;
我知道RETURNS SETOF integer
是不對的,但我不知道如何得到這個工作?
Thx爲您提供幫助! requestcounter --------------------(「00-01 sec」,2979)(「01-04 sec」,2)這是我的結果 - 有可能它看起來像這樣:Kategorien | requestcounter ------------ + ---------------- 00-01 sec | 2073 01-04 sec | 2?最好的問候 這樣我有2列? – liquid
@cartmanownz:用'SELECT * FROM ...'調用你的函數來分解行。 –
非常感謝你!我用postgres 9.3.8來試試這個 - 它有效,但是8.4有一個問題。你知道爲什麼嗎? – liquid