Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 如何在sql中完成完整系列_Sql Server - Fatal编程技术网

Sql server 如何在sql中完成完整系列

Sql server 如何在sql中完成完整系列,sql-server,Sql Server,我想实现从0到表中最大数字的完整数字刻度 假设我们有一个表T,它有两个名为x和y的字段 select x,y from t 让我们看看结果 X Y 3 11 5 23 7 45 9 1 10 34 我发现此查询用于生成序列号: With T_Misparim As (Select 1 N Union All Select N+1 N From T_Misparim Where N<1000) Select N From T_Misparim Option (Ma

我想实现从0到表中最大数字的完整数字刻度

假设我们有一个表T,它有两个名为x和y的字段

select x,y
from t
让我们看看结果

X Y
3 11
5 23
7 45
9 1 
10 34
我发现此查询用于生成序列号:

With T_Misparim As

(Select 1 N
Union All
Select  N+1 N
From    T_Misparim
Where   N<1000)
Select  N
From    T_Misparim
Option (MaxRecursion 0);

您只需使用序号CTE左键连接

select 3 as X, 11 as Y into #TEST
insert #TEST values (5,23),(7,45),(9,1),(10,34)

;with NUMS(n) as (
    select 0 union all
    select 1 + n from NUMS where n < 50
)
select
    NUMS.n N,
    T.X,
    isnull(T.Y, 0) Y
from NUMS
    left join #TEST T on (T.X = NUMS.n)
option (maxrecursion 50)

您只需使用序号CTE左键连接

select 3 as X, 11 as Y into #TEST
insert #TEST values (5,23),(7,45),(9,1),(10,34)

;with NUMS(n) as (
    select 0 union all
    select 1 + n from NUMS where n < 50
)
select
    NUMS.n N,
    T.X,
    isnull(T.Y, 0) Y
from NUMS
    left join #TEST T on (T.X = NUMS.n)
option (maxrecursion 50)
N   X   Y
0   NULL    0
1   NULL    0
2   NULL    0
3   3   11
4   NULL    0
5   5   23
6   NULL    0
7   7   45
8   NULL    0
9   9   1
10  10  34