我上的Postgres 9.3.2訂貨工作,我有這個表:PostgreSQL的ROW_NUMBER不按日期
id startdate enddate no_of_days_between
1 2010-12-22 2010-12-23 1
1 2010-12-23 2010-12-24 1
1 2010-12-24 2010-12-25 1
1 2010-12-25 2010-12-26 1
1 2010-12-26 2010-12-27 1
1 2010-12-27 2010-12-28 1
1 2010-12-28 2010-12-29 1
1 2010-12-29 2011-03-06 67
1 2011-03-06 2011-03-07 1
1 2011-03-07 2011-03-08 1
1 2011-03-08 2011-03-09 1
,我想要做的就是找到連續數日連勝。對於這一點,我現在用的是ROW_NUMBER窗函數在此查詢:
select t.*, row_number() over (partition by no_of_days_between order by enddate) as no_of_consecutive_days from t
什麼,我想回去是這樣的:
id startdate enddate no_of_days no_of_consecutive_days
_between
1 2010-12-22 2010-12-23 1 1
1 2010-12-23 2010-12-24 1 2
1 2010-12-24 2010-12-25 1 3
1 2010-12-25 2010-12-26 1 4
1 2010-12-26 2010-12-27 1 5
1 2010-12-27 2010-12-28 1 6
1 2010-12-28 2010-12-29 1 7
1 2010-12-29 2011-03-06 67 1
1 2011-03-06 2011-03-07 1 1
1 2011-03-07 2011-03-08 1 2
1 2011-03-08 2011-03-09 1 3
但查詢返回的是什麼更喜歡它首先由有序no_of_days_between然後通過enddate所以我回來了:
id startdate enddate no_of_days no_of_consecutive_days
_between
1 2010-12-22 2010-12-23 1 1
1 2010-12-23 2010-12-24 1 2
1 2010-12-24 2010-12-25 1 3
1 2010-12-25 2010-12-26 1 4
1 2010-12-26 2010-12-27 1 5
1 2010-12-27 2010-12-28 1 6
1 2010-12-28 2010-12-29 1 7
1 2011-03-06 2011-03-07 1 8
1 2011-03-07 2011-03-08 1 9
1 2011-03-08 2011-03-09 1 10
1 2010-12-29 2011-03-06 67 1
有沒有人遇到過這個問題呢?我怎樣才能強制它先訂購然後再分區?
感謝
我明白,但是,順序仍然沒有看到數據的正確方法。它已按照我要求將其分區的列進行排序。 – mskem