2

私はこのようなものを1つ選択しています

SELECT 
TOP (5)
 ROW_NUMBER() OVER(ORDER By S.Id) AS RowNumber 
 ,S.SubscribeNumber AS SN
FROM billing.Subscribe AS S
ORDER BY S.Id

このような出力があります

RowNumber            SN
-------------------- --------
1                    01100017
2                    01100025
3                    01100033
4                    01100041
5                    01100050
(5 row(s) affected)

そして、私はこのように別の選択をしています

 SELECT 
 ROW_NUMBER() OVER(ORDER By S.Id) AS RowNumber 
 ,S.SubscribeNumber AS SN
FROM billing.Subscribe AS S
 ORDER BY S.Id
 OFFSET (5) ROWS
FETCH NEXT (5) ROWS ONLY;

このような出力があります

RowNumber            SN
-------------------- --------
6                    01100068
7                    01100076
8                    01100084
9                    01100092
10                   01100106
(5 row(s) affected)

どのようにユニオンオールツーアップを選択して、このような結果を得ることができますか

RowNumber            Sn
-------------------- --------
1                    01100017
6                    01100068
2                    01100025
7                    01100076
3                    01100033
8                    01100084
4                    01100041
9                    01100092
5                    01100050
10                   01100106
(10 row(s) affected)
4

2 に答える 2

2

してみてください:

SELECT * FROM(
    SELECT 
    TOP (5)
     ROW_NUMBER() OVER(ORDER By S.Id) AS RowNumber 
     ,S.SubscribeNumber AS SN
    FROM billing.Subscribe AS S
    ORDER BY S.Id
    UNION ALL
    SELECT 
     ROW_NUMBER() OVER(ORDER By S.Id) AS RowNumber 
     ,S.SubscribeNumber AS SN
    FROM billing.Subscribe AS S
     ORDER BY S.Id
     OFFSET (5) ROWS
    FETCH NEXT (5) ROWS ONLY
)x
ORDER BY CASE WHEN RowNumber-5<=0 THEN RowNumber ELSE RowNumber-5 END, RowNumber

むしろ、これは私が推測するあなたの問題を解決します

SELECT * FROM(
    SELECT 
    TOP (10)
     ROW_NUMBER() OVER(ORDER By S.Id) AS RowNumber 
     ,S.SubscribeNumber AS SN
    FROM billing.Subscribe AS S
    ORDER BY S.Id
)x
ORDER BY CASE WHEN RowNumber-5<=0 THEN RowNumber ELSE RowNumber-5 END, RowNumber
于 2013-05-20T11:47:08.013 に答える