2017-04-06 117 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年3月からレコードを取得したい場合は、以下を使用します。

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) 

あなたはdate型に文字列2017-03-01をキャストすることができます。 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は、それを自己を行うことはできません。 PostgreSQL(およびすべてのSQLデータベース)は、日付計算のために偉大な functionsを持っています。あなたは、SQL言語でカスタム関数を書くことができます

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

(マクロ):

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) 
関連する問題