2017-02-02 13 views
0
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 

これは私のデータセットの一部です。実際の列は異なる表のトランザクションの合計を使用して作成されます。ここで示す必要があるのは、タイトルが空(NULLでない)の場合、Actualは同じGroupBreakConを持つすべてのものの合計を表示する必要があるということです。結果表の値の計算と挿入

また、最後の行にTOTAL INCOMEが表示されます。 Actualは、上記で計算された両方のサブグループの合計を表示する必要があります。

我々は上記の例を取る場合、結果は誰かがこれはacheivedできるか助言することができますしてください。この

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 

ようにする必要があります。

+0

'ROLLUP'クエリWITHへの書き込みを試してみてください合計を集計します - [サンプルスクリーンショット](https://i.stack.imgur.com/bDmaC.png)を参照してください。 [Technet](https://technet.microsoft.com/en-us/library/bb522495(v = sql.105).aspx)と[Simple Talk](https://www.simple-talk。 com/sql/t-sql-programming/questions-about-cube-rollup-and-grouping-sets-you-were-too-shy-to-ask /)を使用していました。 – Serge

答えて

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 

デフォルト免責事項:このコードはテストされていない、ご自身の責任で使用し