2016-10-25 4 views
0

わかりましたので、私はうまく機能し、このクエリを使用して起動します。Postgresの別のテーブルから集計機能を追加すること

SELECT org.organization_id, org.comp_code, org.name, cust.name as customer, 
o.order_number as number, o.order_id as id, cast(o.total_charge as real) as receivable 
    FROM 
    organization as org, orders as o, organization as cust, 
    reconcile_order as ro 
    WHERE org.organization_id = o.shipper_org_id 
    and o.broker_org_id = cust.organization_id 
    and o.order_id = ro.order_id 
    and o.status = 'D' 
    and org.comp_code = 'ABC' 
    and (ro.receive_payment_in_full = 0 or ro.receive_payment_in_full is NULL) 

しかし、今、私は、各ORDER_IDのために別のテーブルからの合計を追加します。この他の表はreconcile_receivablesと呼ばれ、amountという列があります。しかし、このトリックは、複数の金額が一意のorder_idに関連する可能性があるということです。だから、私はすべてのユニークな順序のためにこれらの金額を合計する必要があります。これは私が試したものです:助けないgroup by句を追加する

column "org.organization_id" must appear in the GROUP BY clause 
or be used in an aggregate function 

SELECT org.organization_id, org.comp_code, org.name, cust.name as customer, 
o.order_number as number, o.order_id as id, cast(o.total_charge as real) as receivable, 
sum(rr.amount) as partial_pay 
    FROM 
    organization as org, orders as o, organization as cust, 
    reconcile_order as ro, reconcile_receivables as rr 
    WHERE org.organization_id = o.shipper_org_id 
    and o.broker_org_id = cust.organization_id 
    and o.order_id = ro.order_id 
    and o.order_id = rr.order_id 
    and o.status = 'D' 
    and org.comp_code = 'ABC' 
    and (ro.receive_payment_in_full = 0 or ro.receive_payment_in_full is NULL) 

私の問題は、私はこのエラーを取得しています。だから、どのo.order_idに関連するすべてのrr.amountの合計を表す列を表示するためにこれを解決することができますか?たとえば、合計が1000で、order_id = 3に関連する2つのrr.amount値があります。したがって、その行には合計額の列が1000の値で表示されます。

+2

'Group by'で非集計カラムを追加します –

答えて

1

PostgreSQLの推奨するものを実行してくださいGROUP BY節。

あなたはsum()またはcount()などの集約関数でないをしている出力リストからすべて列を追加する必要があります。

reconcile_receivablesはありませんが、ordersがある可能性はありますか?その場合はLEFT OUTER JOINを使用してください。

+0

あなたの脇は正しいので、私は' o.order_id = rr.order_idに左外部結合rrをしました'それ以降のすべての条件を追加しました。しかし、今、私は奇妙な '関係rrは存在しない'エラーを取得します – Roberto

+0

'rr'は存在しません。 'left outer join reconcile_receivables rr on ...' –

+0

私もそれを試しましたが、今は 'table name" rrが "複数回指定されています"ので、以前のrrの指定を削除すると、 'FROM-clauseテーブル "o"のエントリ '私はエラーのループで立ち往生しています。 – Roberto

0

あなたはこれで問題が列を置換する、ウィンドウ関数を使用することができます。

:私はそれはあなたがそのようなクエリを使用することができ、私はクエリ全体のテキストを参照してくださいと私が見つけた

UPDを助けることができると思い

sum(rr.amount) over(partition by order.id) as partial_pay 

SELECT org.organization_id,..., sum(rr.amount) as partial_pay FROM organization as org, orders as o, organization as cust, reconcile_order as ro, (select sum(amount) as amount, order_id FROM reconcile_receivables GROUP BY order_id) as rr WHERE org.organization_id = o.shipper_org_id and o.broker_org_id = cust.organization_id and o.order_id = ro.order_id and o.order_id = rr.order_id and o.status = 'D' and org.comp_code = 'ABC' and (ro.receive_payment_in_full = 0 or ro.receive_payment_in_full is NULL) 

これは、ウィンドウ関数を使用する最初のクエリよりも、より最適化され、はるかに高速なクエリが可能です。それはあなたのデータベース構造(使用するインデックスなど)と行数に依存します

関連する問題