Derived/calculated column on existing table

后端 未结 3 2013
盖世英雄少女心
盖世英雄少女心 2021-01-25 16:38

I have been going nuts over this issue for some time and I am seeking help.

I have SQL Server table with values, as follows:

Account - Date - Amount - Su         


        
3条回答
  •  再見小時候
    2021-01-25 17:30

    SELECT 
        t.Account,
        t.Date,
        t.Amount,
        t.Summary,
        s.Summary as SummaryPreviousYear
    FROM TestTable t    
    JOIN (
        SELECT
            Account,
            DATEPART(YEAR, Date) as Year,
            SUM(Amount) as Summary
        FROM TestTable
        GROUP BY Account, DATEPART(YEAR, Date)
    ) s
        ON s.Account = t.Account
        AND s.Year = DATEPART(YEAR, Date) - 1
    

提交回复
热议问题