あなたは(あなたの例で)このような何かを試すことができます。
表
create table test (
row_id int,
id int,
total decimal(15,2)
);
insert into test values
(6395, 1509, 112), (22986, 1509, 112),
(1393, 3284, 40.37), (24360, 3284, 40.37);
クエリ
with distinct_records as (
select distinct id, total from test
)
select a.id, b.actual_total, array_agg(a.row_id) as row_ids
from test a
inner join (select id, sum(total) as actual_total from distinct_records group by id) b
on a.id = b.id
group by a.id, b.actual_total
結果
| id | actual_total | row_ids |
|------|--------------|------------|
| 1509 | 112 | 6395,22986 |
| 3284 | 40.37 | 1393,24360 |
説明
我々は理由が異なるROW_IDで複数回表示される注文と合計何のためにあるのかわかりません。したがって、with ...
というフレーズを使用して共通テーブル式(CTE)を使用すると、個別のIDと合計が取得されます。
CTEでは、この別個のデータを使用して合計を行います。私たちは元のテーブルのIDを別個の値にわたる集約と結合します。次に、情報がよりきれいに見えるように、row_idsをカンマ区切りにします。
SQLFiddle例
http://sqlfiddle.com/#!15/72639/3
http://meta.stackoverflow.com/questions/285551/why-may-i-not-uploadを-images-of-code-on-so-asking-a-question/285557#285557 –