2017-04-06 56 views
0
select 
    c_elementvalue.value AS "VALUE", 
    c_elementvalue.name AS "NAME", 
    rv_fact_acct.postingtype AS "POSTINGTYPE", 
    sum(rv_fact_acct.amtacct) AS "AMNT", 
    'YTDB' AS "TYPE", 
    c_period.enddate AS "ENDDATE", 
    max(ad_client.description) AS "COMPANY" 
from 
    adempiere.c_period, 
    adempiere.rv_fact_acct, 
    adempiere.c_elementvalue, 
    adempiere.ad_client 
where 
    (rv_fact_acct.ad_client_id = ad_client.ad_client_id) and 
    (rv_fact_acct.c_period_id = c_period.c_period_id) and 
    (rv_fact_acct.account_id = c_elementvalue.c_elementvalue_id) and 
    (rv_fact_acct.dateacct BETWEEN to_date(to_char('2017-03-01' ,'YYYY') ||'-04-01', 'yyyy-mm-dd') AND '2017-03-31' ) AND 
    (rv_fact_acct.ad_client_id = 1000000) and 
    (rv_fact_acct.c_acctschema_id = 1000000)and 
    (rv_fact_acct.postingtype = 'B')and 
    (rv_fact_acct.accounttype in ('R','E')) 
group by c_elementvalue.value , c_elementvalue.name , rv_fact_acct.postingtype , c_period.enddate 
order by 5 asc, 1 asc 

當執行上面的sql語句(postgres)時,我收到了一條錯誤消息。無法選擇最佳候選功能。您可能需要添加明確的類型轉換

錯誤消息:

[Err] ERROR: function to_char(unknown, unknown) is not unique 
LINE 68: (rv_fact_acct.dateacct BETWEEN to_date(to_char('2017-03-... 
               ^
HINT: Could not choose a best candidate function. You might need to add explicit type casts. 
+0

是什麼 -

例如任務「獲得下個月的第一次約會」可以表達進行您想用來過濾帳戶日期的邏輯?你現在的代碼沒有意義。 –

回答

0

目前尚不清楚你打算通過該帳戶使用日期過濾什麼邏輯,但你目前使用的to_char()to_date()似乎是錯誤的原因。如果你只是想從2017年三月搶的記錄,那麼使用以下命令:

rv_fact_acct.dateacct BETWEEN '2017-03-01' AND '2017-03-31' 

如果你給我們介紹一下你正在嘗試做的更多信息,可以進行相應的更新。

1

查詢的這部分是有問題的:

to_date(to_char('2017-03-01' ,'YYYY') ||'-04-01', 'yyyy-mm-dd') 

目前沒有任何功能to_char,有第一個參數字符串。

 
postgres=# \df to_char 
            List of functions 
┌────────────┬─────────┬──────────────────┬───────────────────────────────────┬────────┐ 
│ Schema │ Name │ Result data type │  Argument data types  │ Type │ 
╞════════════╪═════════╪══════════════════╪═══════════════════════════════════╪════════╡ 
│ pg_catalog │ to_char │ text    │ bigint, text      │ normal │ 
│ pg_catalog │ to_char │ text    │ double precision, text   │ normal │ 
│ pg_catalog │ to_char │ text    │ integer, text      │ normal │ 
│ pg_catalog │ to_char │ text    │ interval, text     │ normal │ 
│ pg_catalog │ to_char │ text    │ numeric, text      │ normal │ 
│ pg_catalog │ to_char │ text    │ real, text      │ normal │ 
│ pg_catalog │ to_char │ text    │ timestamp without time zone, text │ normal │ 
│ pg_catalog │ to_char │ text    │ timestamp with time zone, text │ normal │ 
└────────────┴─────────┴──────────────────┴───────────────────────────────────┴────────┘ 
(8 rows) 

你可以施放串2017-03-01date類型。 PostgreSQL的不能做自己,因爲有更多的變種:numerictimestamp,...

postgres=# select to_date(to_char('2017-03-01'::date ,'YYYY') ||'-04-01', 'yyyy-mm-dd'); 
┌────────────┐ 
│ to_date │ 
╞════════════╡ 
│ 2017-04-01 │ 
└────────────┘ 
(1 row) 

通常,日期時間操作使用的字符串操作是錯誤的。對於日期算術,PostgreSQL(和所有SQL數據庫)具有很好的functions

postgres=# select date_trunc('month', current_date + interval '1month')::date; 
┌────────────┐ 
│ date_trunc │ 
╞════════════╡ 
│ 2017-05-01 │ 
└────────────┘ 
(1 row) 

您可以在SQL語言編寫自定義函數(宏):

postgres=# create or replace function next_month(date) 
      returns date as $$ 
      select date_trunc('month', $1 + interval '1month')::date $$ 
      language sql; 
CREATE FUNCTION 
postgres=# select next_month(current_date); 
┌────────────┐ 
│ next_month │ 
╞════════════╡ 
│ 2017-05-01 │ 
└────────────┘ 
(1 row)