gpt4 book ai didi

mysql - 使用sql修改表

转载 作者:行者123 更新时间:2023-11-29 07:56:21 26 4
gpt4 key购买 nike

我有这张 table

**Original Table**          
year month duration amount per month
2012 5 3 2000

我想得到这个

**Result table**            
year month duration amount per month
2012 5 1 2000
2012 6 1 2000
2012 7 1 2000

请注意,项目(这是一个项目)的持续时间为 3,“每月金额”为 2000,因此我添加了另外两行以显示接下来的几个月(6 和 7)将有“金额”每月”也是如此。如何使用 sql/tsql 做到这一点?

最佳答案

在 SQL SERVER 上尝试这个,我包括了我的测试临时表:

declare @temp as table
(
[year] int
, [month] int
, [duration] int
, [amount] int
)
insert into @temp
(
[year]
, [month]
, [duration]
, [amount]
)
VALUES(
2012
,5
,3
,2000
)

SELECT
[year]
,[month] + n.number
,1
,[amount]
, '1' + SUBSTRING(CAST([duration] AS varchar(10)), 2, 1000) AS Items
FROM @temp
JOIN master..spt_values n
ON n.type = 'P'
AND n.number < CONVERT(int, [duration])

关于mysql - 使用sql修改表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25048490/

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