简体   繁体   中英

altering a table using sql

I have this table

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

and I want to get this

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

Note how the duration of a project (this is a project) is 3 and the "amount per month" is 2000, so I added two more rows to show that the next months (6 and 7) will have an "amount per month" as well. How do I do that with sql/tsql?

try this for SQL SERVER, i included my test temp table:

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

Please see the script below that may work for your requirement. I have also compensated for calender year and month increment. Please test and let me know.

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]

For those that are wondering how to increment the year if needed, here is an example building on Suing response (really easy, just include two case statements):

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM