简体   繁体   English

在结果表中计算和插入值

[英]calculating and inserting values in a result table

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

This is a part of my data set. 这是我数据集的一部分。 the column actual is created using sum of transactions from a different table. 实际列是使用来自其他表的交易总和创建的。 What I need to show here is that if the Title is empty(not NULL) then the Actual should show the sum of everything which has the same GroupBreakCon. 我需要在这里显示的是,如果Title为空(而不是NULL),那么Actual应该显示具有相同GroupBreakCon的所有内容的总和。

Also where you can see the TOTAL INCOME in the last row. 您也可以在最后一行看到“总收入”。 the Actual should show the sum of both the subgroup totals calculated above. 实际值应显示上述两个子组总计的总和。

If we take the above example then the result should be like this 如果我们以上述示例为例,那么结果应该是这样的

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

please can someone advise how can this be acheived. 请有人建议如何实现。

You could try to calculate the totals per GroupbreakCon first and use that: 您可以尝试首先计算每个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

Default disclaimer: this code is untested, use at your own risk 默认免责声明:此代码未经测试,使用后果自负

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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