我有一个关于在2个查询中添加行号的问题。
我的查询是:
SELECT
'telxm001001' AS Node,
'1' AS [Speed Dialing],
'0' AS [Spd DI Numbers by Range],
NULL as 'Speed Dialing No.',
REPLACE(TelefonGeschaeft, '+', '00') AS [Call Number],
Nachname AS [Directory Name],
Vorname AS [Directory First Name]
FROM
dbo.MaData
WHERE
(TelefonGeschaeft LIKE '+%')
UNION ALL
SELECT
'telxm001001' AS Node,
'1' AS [Speed Dialing],
'0' AS [Spd DI Numbers by Range],
ROW_NUMBER() OVER (ORDER BY Nachname) AS 'Speed Dialing No.',
REPLACE(MobiltelefonGeschaeft, '+', '00') AS [Call Number],
Nachname AS [Directory Name],
Vorname AS [Directory First Name]
FROM
dbo.MaData
WHERE
(MobiltelefonGeschaeft LIKE '+%')
我有一个包含1400个条目的SQL Server表。 2个查询为我带来了正确的结果,但行号不正确,因为第二个查询开始时,数字从1开始。所以在我的查询结果中,我有一个从1到680的数字,然后在第二个查询开始时,数字从1开始。有没有办法在两个查询完成后添加行编号,因此编号从1到1400?
祝你好运 switzly
答案 0 :(得分:0)
对派生查询应用ROW_NUMBER排序;通过各个查询提供鉴别器(例如queryOrder)以保持相关结果的分组。
SELECT ROW_NUMBER() OVER (ORDER BY queryOrder, [Directory Name]) AS number
FROM (
SELECT 1 AS queryOrder, [Directory Name] ..
UNION ALL
SELECT 2 AS queryOrder, [Directory Name] ..
) j
答案 1 :(得分:0)
在深入了解此查询之前,您是否希望获得两个查询的行号?
如果是,为什么在第一个查询中放置NULL?
如果您希望两个查询都有行号,请在两个查询中使用它。
否则,请尝试将它们放入另一个查询中,如下所示:
select
ROW_NUMBER over ..., Node, [Speed Dialing], ....
from
(<actual query here>) as temp1;