TSQL在CTE中使用ROW_NUMBER,选择顺序不工作

时间:2015-08-28 09:21:20

标签: sql-server tsql

我从一排nubmers中取出字符串。当我使用row_number函数时,order by子句不起作用

DECLARE @text VARCHAR(MAX)

IF OBJECT_ID('tempdb..#numbers') IS NOT NULL DROP TABLE #numbers
SELECT CAST(ROW_NUMBER() OVER (ORDER BY name) AS INT) AS number INTO #numbers FROM master..spt_values 

SET     @text = ''

;WITH 
numbers (number)
AS
(
    SELECT CAST(ROW_NUMBER() OVER (ORDER BY name) AS INT) AS number FROM master..spt_values 
),
a
AS
(
    SELECT number FROM numbers WHERE number < 10
),
b
AS
(
    SELECT number FROM numbers WHERE number < 10
)
SELECT      @text = @text + LTRIM(STR(a.number*b.number))
FROM        a
CROSS JOIN  b
ORDER BY    a.number, b.number DESC

SELECT @text

result "9"

SET     @text = ''

;WITH 
numbers (number)
AS
(
    SELECT number FROM #numbers
),
a
AS
(
    SELECT number FROM numbers WHERE number < 10
),
b
AS
(
    SELECT number FROM numbers WHERE number < 10
)
SELECT      @text = @text + LTRIM(STR(a.number*b.number))
FROM        a
CROSS JOIN  b
ORDER BY    a.number, b.number DESC

SELECT @text

结果“9876543211816141210864227242118151296336322824201612844540353025201510554484236302418126635649423528211477264564840322416881726354453627189”

差异在哪里?

2 个答案:

答案 0 :(得分:1)

我希望这与this issue有关,总结一下,当你使用变量连接时,例如。

SELECT @Variable = @Variable + someField
FROM    Table
ORDER BY AnotherField;

结果取决于物理实现和内部访问路径。我目前正在努力在互联网上找到基准测试,但我认为SQL Server中最快,最可靠的方法是使用XML扩展来将行连接到列:

WITH Numbers AS (SELECT * FROM (VALUES (1),(2),(3),(4),(5),(6),(7),(8),(9)) t (Number))
SELECT [Text] = (SELECT LTRIM(STR(a.number*b.number))
                FROM    Numbers AS A 
                        CROSS JOIN Numbers AS B
                ORDER BY A.Number, b.Number DESC
                FOR XML PATH(''), TYPE).value('.', 'VARCHAR(MAX)');

N.B。我还删除了对master..spt_values的引用并替换为表值构造函数 - 这只会添加不必要的读取以生成从1到9的序列。

如果您的序列需要更多数字,我仍然不会使用系统表,使用Iztik Ben-Gan的堆叠CTE方法,如this article中所述:

DECLARE @Numbers INT = 100000;

WITH N1 AS (SELECT N FROM (VALUES (1),(1),(1),(1),(1),(1),(1),(1),(1),(1)) t (N)),
N2 (N) AS (SELECT 1 FROM N1 AS N1 CROSS JOIN N1 AS N2),
N3 (N) AS (SELECT 1 FROM N2 AS N1 CROSS JOIN N2 AS N2),
N4 (N) AS (SELECT 1 FROM N3 AS N1 CROSS JOIN N3 AS N2),
Numbers (Number) AS (SELECT TOP (@Numbers) ROW_NUMBER() OVER(ORDER BY N) FROM N4)

SELECT  Number
FROM    Numbers;

答案 1 :(得分:0)

不要在ROW_NUMBER()上使用强制转换。这将返回与第二个查询相同的内容:

DECLARE @text VARCHAR(MAX) = ''

;WITH 
numbers (number)
AS
(
    SELECT ROW_NUMBER() OVER (ORDER BY name)  AS number FROM master..spt_values 
),
a
AS
(
    SELECT number FROM numbers WHERE number < 10
),
b
AS
(
    SELECT number FROM numbers WHERE number < 10
)
SELECT      @text = @text + LTRIM(STR(a.number*b.number))
FROM        a
CROSS JOIN  b
ORDER BY    a.number, b.number DESC

也不要在CTE中使用别名定义两次相同的内容:

DECLARE @text VARCHAR(MAX) = ''

;WITH 
numbers (number)
AS
(
    SELECT ROW_NUMBER() OVER (ORDER BY name) AS number FROM master..spt_values 
),
a
AS
(
    SELECT number FROM numbers WHERE number < 10
)
SELECT      @text = @text + LTRIM(STR(a.number*b.number))
FROM        a AS a
CROSS JOIN  a AS b
ORDER BY    a.number, b.number DESC

SELECT @text