Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/68.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
Mysql 使用sql修改表_Mysql_Sql_Sql Server_Tsql - Fatal编程技术网

Mysql 使用sql修改表

Mysql 使用sql修改表,mysql,sql,sql-server,tsql,Mysql,Sql,Sql Server,Tsql,我有这张桌子 **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

我有这张桌子

**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])

请参阅下面的脚本,该脚本可能适合您的要求。我还补偿了日历年和月的增量。请测试并让我知道

DECLARE @temp AS TABLE([Year] INT,[Month] INT,Duration INT,Amount INT)

INSERT INTO @temp([year], [month], Duration, Amount)
VALUES (2011, 5, 3, 2000),(2012, 11, 3, 3000),(2013, 9, 12, 1000);

;WITH cte_datefix
    AS (
    SELECT [Year],
         [Month],
         Duration,
         Amount,
         CAST(CAST([Year] AS VARCHAR(4)) + RIGHT('00' + CAST([Month] AS VARCHAR(2)), 2) + '01' AS DATE) AS [Date]
    FROM @temp
    ),
cte_Reslut
    AS (SELECT [Year],
            [Month],
            Duration,
            Amount,
            [Date],
            1 AS Months
       FROM cte_datefix
       UNION ALL
       SELECT t.[Year],
            t.[Month],
            t.Duration,
            t.Amount,
            DATEADD(M, Months, t.[Date]) AS [Date],
            cr.Months + 1 AS Months
       FROM cte_Reslut AS cr
           INNER JOIN cte_datefix AS t
           ON t.[Year] = cr.[Year]
       WHERE cr.Months < cr.Duration
    )
    SELECT YEAR([Date]) AS [Year],
         MONTH([Date]) AS [Month],
         1 AS Duration,
         Amount
    FROM cte_Reslut
    ORDER BY [Date]

对于那些想知道如果需要如何增加年数的人,这里有一个基于起诉响应的示例,非常简单,只需包括两个案例陈述:

select
 2012 as [year] 
,11 as [month]
,5 as [duration]
,2000 as [amount]
into #temp

select * from #temp

SELECT
 case 
    when [month] + n.number > 12 
        then [year] + 1 
        else [year]
    end as [year] 
,case 
    when [month] + n.number > 12 
        then [month] + n.number - 12 
        else [month] + n.number 
    end as newYear
,1 as newDuration
,[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])

drop table #temp

您使用的是哪种sql db?如果持续时间超过7,它是否也必须增加年份?是的。我也包括了这个问题的答案,以起诉答案为基础。我会将结果集插入另一个表中,然后在完成后重命名。