2016-01-08 102 views
0

我想顯示的功能如下:Postgres的功能與選擇

select LoanId, BookId ,(OutDate-InDate)*0.3 as money, OutDate-InDate days 
from loans 
where (OutDate-InDate)> 30 and OutDate::varchar(4)='2000'; 

作爲輸入只有一年,作爲輸出應該返回選擇。 我試過make功能,但它不起作用。 任何想法?

+0

請分享您的代碼,並解釋到底是什麼沒有在那裏工作。 – Mureinik

+1

請閱讀以下內容並在問題中添加更多信息,以便我們提供幫助! https://stackoverflow.com/help/mcve – Ross

+0

我試圖讀你的思想,所以我可以回答你的問題....但它沒有奏效。任何想法? –

回答

0

我有一個表像下面

create table loan(loanid int,bookid int,outdate date,indate date); 

像這樣

insert into loan values(1,2,'01-12-2015','01-10-2015'),(1,2,'01-01-2016','08-01-2016'); 

一些數據,我用下面的select查詢

select loanid,bookid,(outdate-indate)*0.3 as "money",outdate-indate as "days" 
from loan 
where (OutDate-InDate)> 30 and extract(year from outdate)='2015' 

loanid bookid money days 
------ ------ ----- ---- 
1  2  18.3 61 

現在我有一個函數做的工作是

create or replace function FN_LOAN_DET(in_year text) 
returns table (loanid int,bookid int,money numeric,days int) 
as 
$$ 
select loanid,bookid,(outdate-indate)*0.3 as "money",outdate-indate as "days" 
from loan 
where (OutDate-InDate)> 30 and extract(year from outdate)=$1 
$$ 
language sql 

當我打電話select * from FN_LOAN_DET('2015')我會得到

loanid bookid money days 
------ ------ ----- ---- 
1  2  18.3 61