什麼樣的查詢支持在排列行時考慮我的行的順序以及它們中的數據範圍?數據應按身份順序處理,我希望範圍邊界定義爲[Time] > 2 or [Time] < -2
。基於訂單和數據範圍的分區行
編輯:換句話說,我想將行視爲一個列表,並將它們分成列表列表,其中ParitionId是行列表的索引。
編輯2:我忘了說清楚,每當[時間]值超出所需範圍時,分區ID應該加1。這個要求意味着我們不能僅僅使用GROUP BY
來存儲行,如果[時間]落在範圍內或外部,case語句返回true或false。
MS SQL服務器2014架構設置:
CREATE TABLE foobartable
([ID] int Identity(1,1) NOT NULL, [Time] float, [X] float, [Y] float)
;
INSERT INTO foobartable
([Time], [X], [Y])
VALUES
(0.0, 1, 1),
(1.0, 1, 1),
(2.0, 1, 1),
(3.0, 1, 1),
(2.0, 1, 1),
(1.0, 1, 1),
(-1.0, 1, 1),
(-2.0, 1, 1),
(-3.0, 1, 1),
(-2.0, 1, 1),
(-1.0, 1, 1),
(0.0, 1, 1),
(1.0, 1, 1),
(2.0, 1, 1),
(3.0, 1, 1),
(2.0, 1, 1)
;
預期的效果
| Id | PartitionId |
|----|-------------|
| 1 | 0 |
| 2 | 0 |
| 3 | 0 |
| 4 | 1 |
| 5 | 2 |
| 6 | 2 |
| 7 | 2 |
| 8 | 2 |
| 9 | 3 |
| 10 | 4 |
| 11 | 4 |
| 12 | 4 |
| 13 | 4 |
| 14 | 4 |
| 15 | 5 |
| 16 | 6 |
你混淆了真正的問題這麼多,我不明白你試圖在這裏做。 –
@SeanLange我想根據它在一系列行中的順序爲一行分配一個等級,並基於該行內的數據在一定範圍內還是不在一定範圍內。所需的結果只是行ID和所需的分區號。這就像將列表分成列表列表,其中每個列表的索引是分區標識。 – mortalapeman
當時間= 2或時間= -2時呢? 或你的意思是[時間]> = 2或[時間] <= -2 –