SQL 服务器 (2008) UNION with ROLL UP

SQL Server (2008) UNION with ROLL UP

我有一个费用报告应用程序,可以将费用费用和帐号合二为一 table,但是给定的费用可以拆分为一部分记入一个 GL 帐户,其余部分记入另一个帐户。为了适应成本拆分,费用 table 有两对金额和帐号列,供数据输入过程使用。像这样:

create table Expenses (expheaderid int, explineid int, amount_split1 decimal(5,2), 
account int, amount_split2 decimal(5,2), account2 int);
insert Expenses values (57, 11, 47.35, 80400, 0, 0);
insert Expenses values (57, 12, 163.31, 80440, 0, 0);
insert Expenses values (57, 13, 30, 80401, 5.90, 70410);
insert Expenses values (57, 14, 35, 80440, 0, 0);
insert Expenses values (57, 15, 45.15, 80440, 0, 0);
insert Expenses values (57, 16, 145.87, 80400, 68.14, 80400);
insert Expenses values (57, 17, 67.35, 80870, 0, 0);
insert Expenses values (57, 18, 105, 80402, 34.50, 80440);

我需要汇总(GROUP BY)来自相同帐户代码的金额,但汇总必须发​​生在两对金额和帐号列之间 - (amount_split1 & 帐户)第一对或 (amount_split2 & account 2) 第二列对。所以我的 table 看起来像这样:

expheaderid explineid amount_split1 account amount_split2 account2
----------- --------- ------------- ------- ------------- --------
         57        11         47.35   80400           0.00       0 
         57        12        163.31   80440           0.00       0
         57        13         30.00   80401           5.90   70410
         57        14         35.00   80440           0.00       0
         57        15         45.15   80440           0.00       0
         57        16        145.87   80400          68.14   80400
         57        17         67.35   80870              0       0
         57        18        105.00   80402          34.50   80440

我想将 amount_split1 成本与 amount_split2 成本联合起来按帐号对它们进行分组。

结果应该是这样的,从 account 列和 account2 列中的 GL accounts 收集

AggTotal  GLAccount
--------  ---------
   261.36     80400
    30.00     80401
   105.00     80402
   277.96     80440
    67.35     80470
and probably...
     0.00         0   -which I don't care about.

我根本不擅长复合查询。我试图在这样的一个语句中使用 UNION 和 GROUP:

select sum(AggTotal), GLAccount from 
((select amount_split1 as AggTotal, account as GLAccount from Expenses)
union all 
(select amount_split2 as AggTotal, account2 as GLAccount from Expenses))as t 
where Expenses.expheaderid=57 group by GLAccount;

但是expheaderid列不会绑定。此数据是更大数据集的一部分,因此 expheaderid 上的 WHERE 子句必须起作用。

感谢您的帮助。

你试过分块做吗? 试试这样的 CTE

WITH C AS(
    (select amount_split1 as AggTotal, account as GLAccount,expheaderid from Expenses)
    union all 
    (select amount_split2 as AggTotal, account2 as GLAccount,expheaderid from Expenses) 
)SELECT  sum(AggTotal) AS SUMAVGTOTAL, GLAccount  
FROM C
WHERE expheaderid=57
GROUP BY GLAccount

或者这个

   select sum(AggTotal), GLAccount from 
    ((select amount_split1 as AggTotal, account as GLAccount from Expenses   where expheaderid=57)
    union all 
    (select amount_split2 as AggTotal, account2 as GLAccount from Expenses where expheaderid=57))as t 
     group by GLAccount;

将你的 expheaderid=57 添加到 union 的每个部分

您也可以使用诱人的## 来做同样的事情。 如果它不起作用告诉我

您可以尝试在子查询中 UNION ALL,然后添加 expheaderid 列,因为您需要在子查询中的 where 上使用它,最终执行 SUM.

查询 1:

SELECT SUM(AggTotal) AggTotal,GLAccount
FROM (
  select amount_split1 as AggTotal, 
         account as GLAccount,
         expheaderid  --  <-- add this column
  from Expenses
  union all 
  select 
      amount_split2 as AggTotal, 
      account2 as GLAccount ,
      expheaderid
  from Expenses
) t1
where expheaderid = 57
GROUP BY GLAccount

Results:

| AggTotal | GLAccount |
|----------|-----------|
|        0 |         0 |
|      5.9 |     70410 |
|   261.36 |     80400 |
|       30 |     80401 |
|      105 |     80402 |
|   277.96 |     80440 |
|    67.35 |     80870 |