次の構造のログテーブルがあります。
CREATE TABLE mytable (
oid integer(10),
department integer(10),
cid integer(10),
status integer(1) comment 'statuses: 1-open, 2-accept, 3-done',
recordtime datetime
);
このテーブルには、ステータスの割り当てに関するデータが格納されます。oid - 組織、cid - カード ID。このテーブルの組織更新カード (新しいステータスの設定) が行を挿入すると、組織は部門に属します
そのテーブルから統計データを選択しようとします。たとえば、部門別または組織別 (oid) の最大/最小受入時間、最大/最小完了時間、および平均受入/完了時間を選択します。
問題は、部門ごとにグループ化するときに選択した列で cid を取得する方法、部門ごとにグループ化するときに oid と oid と cid を取得する方法です。つまり、グループ化された行を選択したときの最大受け入れ時間など、組織 (oid) とカード ID (cid) を知りたい
複数の結合のためにこれらの列が必要です
UPD: Roman Pekar
の
おかげで、彼の答えは私を正しい道に導いてくれました。彼の 2 番目のクエリを使用して、最後のクエリを作成します。
最初に、部門ごとに平均受入/完了時間、最大/最小受入/完了時間を選択oid
しcid
、各部門の最大受入時間を選択します。
with cte as (
select
oid, cid,
max(case when status=1 then recorddatetime end) as open,
max(case when status=2 then recorddatetime end) as accept,
max(case when status=3 then recorddatetime end) as done
from
mytable
group by oid, cid
having
max(case when status=1 then recorddatetime end) is not null and max(case when status=2 then recorddatetime end) is not null
and max(case when status=3 then recorddatetime end) is not null
order by oid, cid
)
select distinct on(department)
department, oid, cid,
ceil(extract(epoch from avg(cte.accept - cte.open) over (partition by department))) as avg_accept_time,
ceil(extract(epoch from avg(done - open) over (partition by department))) as avg_done_time,
ceil(extract(epoch from max(accept - open) over (partition by department))) as max_accept_time,
ceil(extract(epoch from max(done - open) over (partition by department))) as max_done_time,
ceil(extract(epoch from min(accept - open) over (partition by department))) as min_accept_time,
ceil(extract(epoch from min(done - open) over (partition by department))) as min_done_time
from cte cte
order by department, max_accept_time desc
2 番目: 1 番目と同様ですが、組織に対してこれらすべての値を選択します ( oid
)
with cte as (
select
oid, cid,
max(case when status=1 then recorddatetime end) as open,
max(case when status=2 then recorddatetime end) as accept,
max(case when status=3 then recorddatetime end) as done
from
mytable
group by oid, cid
having
max(case when status=1 then recorddatetime end) is not null and max(case when status=2 then recorddatetime end) is not null
and max(case when status=3 then recorddatetime end) is not null
order by oid, cid
)
select distinct on(department, oid)
department, oid, cid,
ceil(extract(epoch from avg(cte.accept - cte.open) over (partition by department, oid))) as avg_accept_time,
ceil(extract(epoch from avg(done - open) over (partition by department, oid))) as avg_done_time,
ceil(extract(epoch from max(accept - open) over (partition by department, oid))) as max_accept_time,
ceil(extract(epoch from max(done - open) over (partition by department, oid))) as max_done_time,
ceil(extract(epoch from min(accept - open) over (partition by department, oid))) as min_accept_time,
ceil(extract(epoch from min(done - open) over (partition by department, oid))) as min_done_time
from cte cte
order by department, oid, max_accept_time desc