gpt4 book ai didi

sql - 选择不存在的行

转载 作者:行者123 更新时间:2023-12-02 06:59:38 26 4
gpt4 key购买 nike

假设我有一张 table :

ColumnA       ColumnB
---------------------------------
1 10.75
4 1234.30
6 2000.99

如何编写一个 SELECT 查询,结果如下:

ColumnA    ColumnB
---------------------------------
1 10.75
2 0.00
3 0.00
4 1234.30
5 0.00
6 2000.99

最佳答案

您可以使用 CTE 创建一个从 1 到表中最大值的数字列表:

; with  numbers as
(
select max(ColumnA) as nr
from YourTable
union all
select nr - 1
from numbers
where nr > 1
)
select nr.nr as ColumnA
, yt.ColumnB
from numbers nr
left join
YourTable yt
on nr.nr = yt.ColumnA
order by
nr.nr
option (maxrecursion 0)

See it working at SQL Fiddle.

关于sql - 选择不存在的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23517959/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com