SQL 服务器按 GroupID 排序随机但每个 GroupID 中的 asc ID
SQL Server Order By GroupID Random but asc ID in each GroupID
我正在使用 SQL Server 2012。
每次执行 SQL 时,我想将序号设置为 Num 字段,规则如下:
- 要随机化的 Grp 顺序。
- 待提升的ID字段。
谁能给我一些启发?
TABLE
+-----+-----+----+
| Num | Grp | ID |
+-----+-----+----+
| 0 | 1 | 1 |
| 0 | 1 | 2 |
| 0 | 1 | 3 |
| 0 | 2 | 4 |
| 0 | 2 | 5 |
| 0 | 2 | 6 |
| 0 | 2 | 7 |
| 0 | 3 | 8 |
| 0 | 3 | 9 |
| 0 | 4 | 10 |
| 0 | 4 | 11 |
| 0 | 4 | 12 |
| 0 | 4 | 13 |
| 0 | 4 | 14 |
+-----+-----+----+
预期输出
+-----+-----+----+
| Num | Grp | ID |
+-----+-----+----+
| 1 | 3 | 8 |
| 2 | 3 | 9 |
| 3 | 1 | 1 |
| 4 | 1 | 2 |
| 5 | 1 | 3 |
| 6 | 4 | 10 |
| 7 | 4 | 11 |
| 8 | 4 | 12 |
| 9 | 4 | 13 |
| 10 | 4 | 14 |
| 11 | 2 | 4 |
| 12 | 2 | 5 |
| 13 | 2 | 6 |
| 14 | 2 | 7 |
+-----+-----+----+
样本TABLE
CREATE TABLE #TEMP(Num INT, Grp INT, ID INT)
INSERT INTO #TEMP
SELECT 0 , 1 , 1
UNION ALL
SELECT 0 , 1 , 2
UNION ALL
SELECT 0 , 1 , 3
UNION ALL
SELECT 0 , 2 , 4
UNION ALL
SELECT 0 , 2 , 5
UNION ALL
SELECT 0 , 2 , 6
UNION ALL
SELECT 0 , 2 , 7
UNION ALL
SELECT 0 , 3 , 8
UNION ALL
SELECT 0 , 3 , 9
UNION ALL
SELECT 0 , 4 , 10
UNION ALL
SELECT 0 , 4 , 11
UNION ALL
SELECT 0 , 4 , 12
UNION ALL
SELECT 0 , 4 , 13
UNION ALL
SELECT 0 , 4 , 14
查询
;WITH CTE2 AS
(
-- Now GRP will be ordered in random order using NEWID()
SELECT ROW_NUMBER() OVER(ORDER BY NEWID()) RNO,GRP
FROM
(
-- Select unique GRP's
SELECT DISTINCT GRP
FROM #TEMP
)TAB
)
SELECT ROW_NUMBER() OVER(ORDER BY rno,ID ASC) NUM,
C2.GRP,C1.ID
FROM CTE2 C2
JOIN #TEMP C1 ON C2.GRP=C1.GRP
order by rno,ID ASC
- Click here 查看结果
更新
这是用新行号更新 table 中的 NUM
字段的查询。
;WITH CTE2 AS
(
SELECT ROW_NUMBER() OVER(ORDER BY NEWID()) RNO,GRP
FROM
(
SELECT DISTINCT GRP
FROM #TEMP
)TAB
)
UPDATE #TEMP SET NUM = TAB.NUM
FROM
(
SELECT ROW_NUMBER() OVER(ORDER BY rno,ID ASC) NUM,
C2.GRP,C1.ID
FROM CTE2 C2
JOIN #TEMP C1 ON C2.GRP=C1.GRP
)TAB
WHERE #TEMP.GRP=TAB.GRP AND #TEMP.ID=TAB.ID
SELECT * FROM #TEMP
order by NUM,ID ASC
- Click here查看结果
我正在使用 SQL Server 2012。
每次执行 SQL 时,我想将序号设置为 Num 字段,规则如下:
- 要随机化的 Grp 顺序。
- 待提升的ID字段。
谁能给我一些启发?
TABLE
+-----+-----+----+
| Num | Grp | ID |
+-----+-----+----+
| 0 | 1 | 1 |
| 0 | 1 | 2 |
| 0 | 1 | 3 |
| 0 | 2 | 4 |
| 0 | 2 | 5 |
| 0 | 2 | 6 |
| 0 | 2 | 7 |
| 0 | 3 | 8 |
| 0 | 3 | 9 |
| 0 | 4 | 10 |
| 0 | 4 | 11 |
| 0 | 4 | 12 |
| 0 | 4 | 13 |
| 0 | 4 | 14 |
+-----+-----+----+
预期输出
+-----+-----+----+
| Num | Grp | ID |
+-----+-----+----+
| 1 | 3 | 8 |
| 2 | 3 | 9 |
| 3 | 1 | 1 |
| 4 | 1 | 2 |
| 5 | 1 | 3 |
| 6 | 4 | 10 |
| 7 | 4 | 11 |
| 8 | 4 | 12 |
| 9 | 4 | 13 |
| 10 | 4 | 14 |
| 11 | 2 | 4 |
| 12 | 2 | 5 |
| 13 | 2 | 6 |
| 14 | 2 | 7 |
+-----+-----+----+
样本TABLE
CREATE TABLE #TEMP(Num INT, Grp INT, ID INT)
INSERT INTO #TEMP
SELECT 0 , 1 , 1
UNION ALL
SELECT 0 , 1 , 2
UNION ALL
SELECT 0 , 1 , 3
UNION ALL
SELECT 0 , 2 , 4
UNION ALL
SELECT 0 , 2 , 5
UNION ALL
SELECT 0 , 2 , 6
UNION ALL
SELECT 0 , 2 , 7
UNION ALL
SELECT 0 , 3 , 8
UNION ALL
SELECT 0 , 3 , 9
UNION ALL
SELECT 0 , 4 , 10
UNION ALL
SELECT 0 , 4 , 11
UNION ALL
SELECT 0 , 4 , 12
UNION ALL
SELECT 0 , 4 , 13
UNION ALL
SELECT 0 , 4 , 14
查询
;WITH CTE2 AS
(
-- Now GRP will be ordered in random order using NEWID()
SELECT ROW_NUMBER() OVER(ORDER BY NEWID()) RNO,GRP
FROM
(
-- Select unique GRP's
SELECT DISTINCT GRP
FROM #TEMP
)TAB
)
SELECT ROW_NUMBER() OVER(ORDER BY rno,ID ASC) NUM,
C2.GRP,C1.ID
FROM CTE2 C2
JOIN #TEMP C1 ON C2.GRP=C1.GRP
order by rno,ID ASC
- Click here 查看结果
更新
这是用新行号更新 table 中的 NUM
字段的查询。
;WITH CTE2 AS
(
SELECT ROW_NUMBER() OVER(ORDER BY NEWID()) RNO,GRP
FROM
(
SELECT DISTINCT GRP
FROM #TEMP
)TAB
)
UPDATE #TEMP SET NUM = TAB.NUM
FROM
(
SELECT ROW_NUMBER() OVER(ORDER BY rno,ID ASC) NUM,
C2.GRP,C1.ID
FROM CTE2 C2
JOIN #TEMP C1 ON C2.GRP=C1.GRP
)TAB
WHERE #TEMP.GRP=TAB.GRP AND #TEMP.ID=TAB.ID
SELECT * FROM #TEMP
order by NUM,ID ASC
- Click here查看结果