-2

表格1

Acct Numb Account Type Asset Mkt Val Acct Asset MV 
--------- ------------ ------------- -------------
881009201 Main Account            30           120 
881009201 Main Account            40           120 
881009201 Main Account            20           120 
881009201 Main Account            30           120 
881009203 Sub Account             50            80 
881009203 Sub Account             10            80 
881009203 Sub Account             20            80 

表2

Acct Numb Account Type Asset Mkt Val Acct Asset MV
--------- ------------ ------------- -------------
881009201 Main Account            30           200 
881009201 Main Account            40           200 
881009201 Main Account            20           200 
881009201 Main Account            30           200 
881009201 Sub Account             80           200 

我有 Table1 上面给出的信息。我有两种类型的帐户:

  1. 主账户(以 01 结尾)
  2. 子账号(03结尾)

现在,我需要一个表,将所有主账户和子账户合并到一个帐号中,其中帐号将与其主帐号相同,但账户类型将为Sub Account.

现在我们还有 2 列:

  1. 资产市场价值
  2. 会计资产 MV

在表 1 中:该列Acct Asset MV显示主账户的总和 (30 + 40 + 20 + 30 = 120),并Asset Mkt Val显示子账户的总和 (50 + 10 + 20 = 80)。

现在在表 2 中:我想Acct Asset MV包含所有帐户的总和 (120 + 80 = 200),在子帐户中 [Asset Mkt Val] 应该是 80。

4

1 回答 1

0

在我看来,Table2 的数据可以推导出如下:

首先创建一个名为 [Table1Totals] 的已保存查询...

SELECT 
    Left([Acct Numb],Len([Acct Numb])-2) AS [Acct Root], 
    Right([Acct Numb],2) AS [Acct Suffix], 
    First(Table1.[Account Type]) AS [FirstOfAccount Type], 
    Sum(Table1.[Asset Mkt Val]) AS [SumOfAsset Mkt Val]
FROM Table1
GROUP BY 
    Left([Acct Numb],Len([Acct Numb])-2), 
    Right([Acct Numb],2);

……归来……

Acct Root  Acct Suffix  FirstOfAccount Type  SumOfAsset Mkt Val
---------  -----------  -------------------  ------------------
8810092    01           Main Account                        120
8810092    03           Sub Account                          80

那么表 2 的数据将是主帐户记录 ("*01") 的 UNION 和每个子帐户的单个记录 ("*03"):

SELECT
    t1.[Acct Numb],
    t1.[Account Type],
    t1.[Asset Mkt Val],
    (
        SELECT SUM(t1t1.[SumOfAsset Mkt Val])
        FROM Table1Totals t1t1
        WHERE t1t1.[Acct Root]=Left(t1.[Acct Numb],Len(t1.[Acct Numb])-2)
    ) AS [Acct Asset MV]
FROM Table1 t1
WHERE t1.[Acct Numb] LIKE "*01"
UNION ALL
SELECT
    t1t2.[Acct Root] & "01" AS [Acct Numb],
    t1t2.[FirstOfAccount Type] AS [Account Type],
    t1t2.[SumOfAsset Mkt Val] AS [Asset Mkt Val],
    (
        SELECT SUM(t1t3.[SumOfAsset Mkt Val])
        FROM Table1Totals t1t3
        WHERE t1t3.[Acct Root]=t1t2.[Acct Root]
    ) AS [Acct Asset MV]
FROM Table1Totals t1t2
WHERE t1t2.[Acct Suffix] = "03"

……归来……

Acct Numb  Account Type  Asset Mkt Val  Acct Asset MV
---------  ------------  -------------  -------------
881009201  Main Account             30            200
881009201  Main Account             40            200
881009201  Main Account             20            200
881009201  Main Account             30            200
881009201  Sub Account              80            200
于 2013-06-26T10:00:37.353 回答