我之前没有在这里发帖,所以请原谅我,如果这不正确或需要改变。
我目前正试图在某一行计数后将一个表拆分为两个单独的表。
所以例如我有2列数据,有2000行,我想把它分成两个表,其中第一个表有相同的2列,前1000个结果,第二个表有相同的两个colums但最后1000个结果。希望这是有道理的。
我有什么方法可以做到这一点吗?
由于
答案 0 :(得分:0)
SELECT top 1000 column1, column2 order by column1 into new_table from tablex
SELECT * FROM (SELECT column1, column2 row_number() over (order by column1) as rownum
from tablex) as a
into new_table from tablex WHERE a.rownum between 1000 and 2001
这应该将第一个记录限制为1000个记录,然后将第二个记录限制为1001 - 2001
答案 1 :(得分:0)
使用ROWNUMER() 与FIDDLE http://sqlfiddle.com/#!3/6230e/3
-- First 1000
select * from
(SELECT ROW_NUMBER() OVER(ORDER BY Col1 asc) AS Row,
Col1, Col2
FROM ur_table)d
where d.Row between 1 and 1000
-- Last 1000
select * from
(SELECT ROW_NUMBER() OVER(ORDER BY Col1 asc) AS Row,
Col1, Col2
FROM ur_table)d
where d.Row between 1000 and 2000
答案 2 :(得分:0)
我可能会使用这种方法。如果您需要解释任何操作,请发表评论。
CREATE TABLE #TableA (Col1 INT)
CREATE TABLE #TableB (Col1 INT)
INSERT INTO #TableA (Col1)
SELECT 1 UNION ALL
SELECT 2 UNION ALL
SELECT 3 UNION ALL
SELECT 4 UNION ALL
SELECT 5
DELETE #TableA
OUTPUT DELETED.Col1 INTO #TableB
WHERE Col1 < 3
答案 3 :(得分:0)
答案较短。
SELECT Col1, Col2
FROM TABLE1
ORDER BY Col1
OFFSET 0 ROW FETCH NEXT 1000 ROWS ONLY
SELECT Col1, Col2
FROM TABLE1
ORDER BY Col1
OFFSET 1000 ROW FETCH NEXT 1000 ROWS ONLY