私はデータテーブルの履歴を持っています。私は、私は必要のあるグラフ、毎日の最新のエントリーを月、年、週ごとに選択する方法は?
- 最終エントリを生成したい 各週の
- 最終エントリー各月の
- 最終エントリー毎年
ため
次のSQLコードでは、last entry per day
を取得できます。
他の3つの結果はどのように取得できますか?
私はデータテーブルの履歴を持っています。私は、私は必要のあるグラフ、毎日の最新のエントリーを月、年、週ごとに選択する方法は?
ため
次のSQLコードでは、last entry per day
を取得できます。
他の3つの結果はどのように取得できますか?
あなたはwindow functionでかなりエレガントにこの問題を解決することができます:週、月の期間については
SELECT created_on, earned_value, planned_value, budgeted_cost
FROM (
SELECT created_on, earned_value, planned_value, budgeted_cost,
rank() OVER (PARTITION BY extract(year from created_on),
extract(doy from created_on)
ORDER BY created_on DESC) r
FROM history) sub
WHERE r = 1
ORDER BY created_on;
を、あなたは、単に(1月1日から数えて、今年のDOY =日)extract(week ...
にextract(doy from created_on)
パラメータを変更することができます。年次データの場合は、最初のextract()
のみが必要です。したがって、2番目のPARTITION
という用語を削除する必要があります。
EXTRACT(WEEK FROM tt.created_on), EXTRACT(month FROM tt.created_on),EXTRACT(year FROM tt.created_on)
トリックしました。
SO、毎日の
の1-最終エントリー
SELECT t.created_on,
t.earned_value,
t.planned_value,
t.budgeted_cost
FROM history t
JOIN (SELECT MAX(tt.created_on) 'maxtimestamp'
FROM history tt
GROUP BY EXTRACT(DAY FROM tt.created_on)) m ON m.maxtimestamp = t.created_on
2 - 各週の最終エントリー
SELECT t.created_on,
t.earned_value,
t.planned_value,
t.budgeted_cost
FROM history t
JOIN (SELECT MAX(tt.created_on) 'maxtimestamp'
FROM history tt
GROUP BY EXTRACT(WEEK FROM tt.created_on)) m ON m.maxtimestamp = t.created_on
、3-毎月
SELECT t.created_on,
t.earned_value,
t.planned_value,
t.budgeted_cost
FROM history t
JOIN (SELECT MAX(tt.created_on) 'maxtimestamp'
FROM history tt
GROUP BY EXTRACT(MONTH FROM tt.created_on)) m ON m.maxtimestamp = t.created_on
のための最終エントリー
4-最後のeの入力グループへのACH年
SELECT t.created_on,
t.earned_value,
t.planned_value,
t.budgeted_cost
FROM history t
JOIN (SELECT MAX(tt.created_on) 'maxtimestamp'
FROM history tt
GROUP BY EXTRACT(YEAR FROM tt.created_on)) m ON m.maxtimestamp = t.created_on
使用 'EXTRACT(tt.created_on FROM WEEK)、EXTRACT(tt.created_onから月)、EXTRACT(tt.created_on FROM年)'クエリの結果。 – Elad