2016-12-08 73 views
-2

我有一個表test需要更新「排序依據」列

+----+--+------+--+--+----------+--+--------------+ 
| ID | | Name | | | orderby | | processgroup | 
+----+--+------+--+--+----------+--+--------------+ 
| 1 | | ABC | | |  10 | |   1 | 
| 10 | | DEF | | |  12 | |   1 | 
| 15 | | LMN | | |  1 | |   1 | 
| 44 | | JKL | | |  4 | |   1 | 
| 42 | | XYZ | | |  3 | |   2 | 
+----+--+------+--+--+----------+--+--------------+ 

我想更新序列中的orderby列,我期待像

+----+--+------+--+--+----------+--+--------------+ 
| ID | | Name | | | orderby | | processgroup | 
+----+--+------+--+--+----------+--+--------------+ 
| 1 | | ABC | | |  1 | |   1 | 
| 10 | | DEF | | |  2 | |   1 | 
| 15 | | LMN | | |  3 | |   1 | 
| 44 | | JKL | | |  4 | |   1 | 
| 42 | | XYZ | | |  5 | |   1 | 
+----+--+------+--+--+----------+--+--------------+ 

邏輯輸出這背後是當我們有procesgroup作爲1,orderby列應更新爲1,2,3,4,當procesgroup爲2時,則更新orderby爲5.

+0

不能明白你的意思。你試過什麼了? – ydoow

+0

@ydoow它確定我得到了答案 –

回答

-1

這可能會幫助你

;WITH CTE AS (
SELECT ROW_NUMBER() OVER (ORDER BY processgroup, ID) AS SNO, ID FROM TABLE1 
) 
UPDATE TABLE1 SET TABLE1.orderby= CTE.SNO FROM CTE WHERE TABLE1.ID = CTE.ID 
+0

爲什麼投票呢? ?解釋 –

+0

Thanx Shakeer這正是我在我心中 –

+0

謝謝。所以我才知道我理解正確。 @紅魔鬼 –