如何在mySQL中汇总手动创建的列

时间:2019-01-28 14:30:33

标签: mysql sql database

我正在为工作中的C#应用​​程序在mySql中创建会计科目表。我从表中手动获取了所有数据并命名了列。我的问题是,如何将我制作的借方栏然后贷方栏加到新行?然后将总计总计。

我尝试了sum函数,但似乎无法使它起作用。我在“字段列表”中收到错误未知列“借方”

SELECT * FROM (
    SELECT 1 as seq, '350' as Account, sum(invoice_amount) as Debit, 0 as Credit, '  ' as Total FROM invoice WHERE Voided = 0
    Union all
    SELECT 2 as seq, '103' as Account, 0 as Debit, sum(check_amount) as Credit, '  ' as Total FROM auctiondbh.Checks
    Union all
    SELECT 3 as seq, '356' as Account, 0 as Debit, sum(Amount) as Credit, '  ' as Total FROM ScratchBal WHERE rec_Owner = @PCName AND RowType = 'BFC'
    Union all
    SELECT 4 as seq, '554' as Account, 0 as Debit, sum(Amount) as Credit, '  ' as Total FROM ScratchBal WHERE rec_Owner = @PCName AND RowType = 'FEED' AND Description = 'Total Feed'
    Union all
    SELECT 5 as seq, '505' as Account, 0 as Debit, sum(Amount) as Credit, '  ' as Total FROM ScratchBal WHERE rec_Owner = @PCName AND RowType = 'TOT_COMM'
    Union all
    SELECT 6 as seq, '525' as Account, 0 as Debit, sum(Amount) as Credit, '  ' as Total FROM ScratchBal WHERE rec_Owner = @PCName AND RowType = 'CUSTOM_CHARGES' AND  description = 'Commingle%'
    Union all
    SELECT 7 as seq, '598' as Account, 0 as Debit, 0 as Credit, '  ' as Total
    Union all
    SELECT 8 as seq, '362' as Account, sum(check_amount) as Debit, 0 as Credit, '  ' as Total FROM auctiondbh.Checks WHERE 'Comment' LIKE 'GEN. Trans%'
    Union all
    SELECT 9 as seq, '796' as Account, 0 as Debit, 0 as Credit, '  ' as Total
    Union all
    SELECT 10 as seq, '366' as Account, sum(Amount) as Debit, 0 as Credit, '  ' as Total FROM ScratchBal WHERE rec_Owner = @PCName AND RowType = 'CUSTOM_CHARGES' and '%BUYER%'
    Union all
    SELECT 11 as seq, '368' as Account, 0 as Debit, 0 as Credit, '  ' as Total
    Union all
    SELECT 12 as seq, '364' as Account, sum(check_amount) as Debit, 0 as Credit, '  ' as Total FROM auctiondbh.Checks where Comment like 'C/C%' 
    Union all
    SELECT 13 as seq, '999' as Account, sum(check_amount) as Debit, 0 as Credit, '  ' as Total FROM auctiondbh.Checks where Comment like 'DUP%'
        Union all
    SELECT 14 as seq, 'TOTALS' as Account, sum(Debit) as Debit, sum(Credit) as Credit, (sum(Debit) - Credit)  as total
) x
order by seq;

这里是应该如何工作的:

seq --- Acct --- Debit   --  Credit --  Total  

1------ 350 -----    0  ------  0     
2------ 103 -----    10 ---- 10   
3------ 356 -----    3  ------  4     
4------ 554 -----    50 ---- 50   
5------ 505 -----    10 -----10   
...        
14--- TOTALS--- 73 --- 74 -----   (1) 

第14行将对“借方和贷方”列求和,然后对第14行进行总计

1 个答案:

答案 0 :(得分:0)

您可以使用With clause从一个查询中生成两个查询

with cte0 as
(
    SELECT * FROM (
        SELECT 1 as seq, '350' as Account, sum(invoice_amount) as Debit, 0 as Credit, '  ' as Total FROM invoice WHERE Voided = 0
        Union all
        SELECT 2 as seq, '103' as Account, 0 as Debit, sum(check_amount) as Credit, '  ' as Total FROM auctiondbh.Checks
        Union all
        SELECT 3 as seq, '356' as Account, 0 as Debit, sum(Amount) as Credit, '  ' as Total FROM ScratchBal WHERE rec_Owner = @PCName AND RowType = 'BFC'
        Union all
        SELECT 4 as seq, '554' as Account, 0 as Debit, sum(Amount) as Credit, '  ' as Total FROM ScratchBal WHERE rec_Owner = @PCName AND RowType = 'FEED' AND Description = 'Total Feed'
        Union all
        SELECT 5 as seq, '505' as Account, 0 as Debit, sum(Amount) as Credit, '  ' as Total FROM ScratchBal WHERE rec_Owner = @PCName AND RowType = 'TOT_COMM'
        Union all
        SELECT 6 as seq, '525' as Account, 0 as Debit, sum(Amount) as Credit, '  ' as Total FROM ScratchBal WHERE rec_Owner = @PCName AND RowType = 'CUSTOM_CHARGES' AND  description = 'Commingle%'
        Union all
        SELECT 7 as seq, '598' as Account, 0 as Debit, 0 as Credit, '  ' as Total
        Union all
        SELECT 8 as seq, '362' as Account, sum(check_amount) as Debit, 0 as Credit, '  ' as Total FROM auctiondbh.Checks WHERE 'Comment' LIKE 'GEN. Trans%'
        Union all
        SELECT 9 as seq, '796' as Account, 0 as Debit, 0 as Credit, '  ' as Total
        Union all
        SELECT 10 as seq, '366' as Account, sum(Amount) as Debit, 0 as Credit, '  ' as Total FROM ScratchBal WHERE rec_Owner = @PCName AND RowType = 'CUSTOM_CHARGES' and '%BUYER%'
        Union all
        SELECT 11 as seq, '368' as Account, 0 as Debit, 0 as Credit, '  ' as Total
        Union all
        SELECT 12 as seq, '364' as Account, sum(check_amount) as Debit, 0 as Credit, '  ' as Total FROM auctiondbh.Checks where Comment like 'C/C%' 
        Union all
        SELECT 13 as seq, '999' as Account, sum(check_amount) as Debit, 0 as Credit, '  ' as Total FROM auctiondbh.Checks where Comment like 'DUP%'
    ) x
)
select * from cte0  union all
select max(seq) + 1, 'Totals', sum(debit), sum(credit), sum(debit) - sum(credit) from cte0