计算和插入结果表中的值

时间:2017-02-02 14:52:14

标签: sql-server if-statement

ReportingCategory   Title   SubtotalGroup   GroupBreakCon   Actual
 Income Statement new BI    INCOME  1   0   NULL
 Income Statement new BI    Levy income NULL    0   -483541.57
 Income Statement new BI    Levy income NULL    0   -657131.06
 Income Statement new BI    Levy income NULL    0   -184526.04
 Income Statement new BI    Levy income NULL    0   -338064.92
 Income Statement new BI    Levy income NULL    0   -110019.3
 Income Statement new BI    Levy income NULL    0   -529367.5
 Income Statement new BI    Levy income NULL    0   -22501.73
 Income Statement new BI    Levy income NULL    0   -123021.98
 Income Statement new BI    Levy income NULL    0   -9927.83
 Income Statement new BI    Levy income NULL    0   -95300.19
 Income Statement new BI    Levy income NULL    0   -135235.24
 Income Statement new BI    Levy income NULL    0   -439673.25
 Income Statement new BI    Levy income NULL    0   -41530.53
 Income Statement new BI    Levy income NULL    0   -394361.39
 Income Statement new BI    Levy income NULL    0   -902.5
 Income Statement new BI    Levy income NULL    0   0
 Income Statement new BI    Levy income NULL    0   0
 Income Statement new BI    Levy income NULL    0   -7.72
 Income Statement new BI    Levy income NULL    0   -11961.34
 Income Statement new BI    Levy income NULL    0   -60589.64
 Income Statement new BI    Levy income NULL    0   0
 Income Statement new BI    Levy income NULL    0   -3828.74
 Income Statement new BI    Levy income NULL    0   -143189.58
 Income Statement new BI    Levy income NULL    0   -800950.83
 Income Statement new BI    Levy income NULL    0   0
 Income Statement new BI    Levy income NULL    0   580.27
 Income Statement new BI    Levy income NULL    0   -78141.85
 Income Statement new BI    Levy income NULL    0   -358986.95
 Income Statement new BI    Consultancy Fees    NULL    0   -15000
 Income Statement new BI        1   0   NULL
 Income Statement new BI    OTHER INCOME    1   100000000   NULL
 Income Statement new BI        1   100000000   NULL
 Income Statement new BI    TOTAL INCOME    3   200000000   NULL

这是我数据集的一部分。使用来自不同表的事务总和创建列实际。我需要在这里展示的是,如果Title是空的(不是NULL),那么Actual应该显示具有相同GroupBreakCon的所有内容的总和。

您还可以在最后一行看到总收入。 Actual应显示上面计算的两个小组总和的总和。

如果我们采用上面的例子,那么结果应该是这样的

ReportingCategory   Title   SubtotalGroup   GroupBreakCon   Actual
 Income Statement new BI    INCOME  1   0   NULL
 Income Statement new BI    Levy income NULL    0   -483541.57
 Income Statement new BI    Levy income NULL    0   -657131.06
 Income Statement new BI    Levy income NULL    0   -184526.04
 Income Statement new BI    Levy income NULL    0   -338064.92
 Income Statement new BI    Levy income NULL    0   -110019.3
 Income Statement new BI    Levy income NULL    0   -529367.5
 Income Statement new BI    Levy income NULL    0   -22501.73
 Income Statement new BI    Levy income NULL    0   -123021.98
 Income Statement new BI    Levy income NULL    0   -9927.83
 Income Statement new BI    Levy income NULL    0   -95300.19
 Income Statement new BI    Levy income NULL    0   -135235.24
 Income Statement new BI    Levy income NULL    0   -439673.25
 Income Statement new BI    Levy income NULL    0   -41530.53
 Income Statement new BI    Levy income NULL    0   -394361.39
 Income Statement new BI    Levy income NULL    0   -902.5
 Income Statement new BI    Levy income NULL    0   0
 Income Statement new BI    Levy income NULL    0   0
 Income Statement new BI    Levy income NULL    0   -7.72
 Income Statement new BI    Levy income NULL    0   -11961.34
 Income Statement new BI    Levy income NULL    0   -60589.64
 Income Statement new BI    Levy income NULL    0   0
 Income Statement new BI    Levy income NULL    0   -3828.74
 Income Statement new BI    Levy income NULL    0   -143189.58
 Income Statement new BI    Levy income NULL    0   -800950.83
 Income Statement new BI    Levy income NULL    0   0
 Income Statement new BI    Levy income NULL    0   580.27
 Income Statement new BI    Levy income NULL    0   -78141.85
 Income Statement new BI    Levy income NULL    0   -358986.95
 Income Statement new BI    Consultancy Fees    NULL    0   -15000
 Income Statement new BI        1   0   -5037181
 Income Statement new BI    OTHER INCOME    1   100000000   NULL
 Income Statement new BI        1   100000000   0
 Income Statement new BI    TOTAL INCOME    3   200000000   -5037181

请有人建议如何实现这一目标。

1 个答案:

答案 0 :(得分:0)

您可以先尝试计算每个GroupbreakCon的总数,然后使用:

;with totals as
(
  select GroupBreakCon, sum(actual) as Subtotal
  from Income_Statement
  group by GroupBreakCon
)
select ReportingCategory, Title, case when len(Title) = 0 then Actual else (select subtotal from totals where GroupBreakCon = i.GroupBreakCon) end as calculated_actual
from Income_Statement

默认免责声明:此代码未经测试,使用风险自负