mysql 总计,MySQL的汇总总计行

I need to create a MariaDB SQL that will allow me to sum two columns (Debit & Credit Columns) to get the difference, as well as return the subtotal of every different account type. The table is as follows:

Account | Debit | Credit

acc1 | 1 | 2

acc1 | 1 | 4

acc2 | 3 | 2

acc2 | 2 | 1

acc2 | 2 | 1

acc3 | 5 | 2

acc3 | 5 | 1

acc3 | 5 | 2

I would like to return the following:

Account | Balance(debit-credit)

acc1 | -1

acc1 | -3

-------------------------------

Total acc1 | -4

-------------------------------

acc2 | 1

acc2 | 1

acc2 | 1

-------------------------------

Total acc2 | 3

-------------------------------

acc3 | 3

acc3 | 4

acc3 | 3

-------------------------------

Total acc3 | 10

-------------------------------

GrandTotal | 9

-------------------------------

Grandtotal is Totals of acc1 + acc2 + acc3

This is what I have tried so far, how ever all I am only getting the grandtotal and no subtotals

SELECT * FROM (

SELECT COALESCE(account,'TOTAL') AS Account, CASE

WHEN account LIKE 'INC%'

THEN sum((gl.credit - gl.debit))

ELSE sum((gl.debit - gl.credit))

END AS Balance

FROM `tabGL Entry` gl

WHERE (NOT (account LIKE 'CASS%')

AND NOT (account LIKE 'CLIA%')

AND NOT (account LIKE 'FASS%'))

GROUP BY account WITH ROLLUP

) AS gl

ORDER BY CASE

WHEN account LIKE 'INC%' THEN 1

WHEN account LIKE 'DCOI%' THEN 2

WHEN account LIKE 'DMC%' THEN 3

WHEN account LIKE 'INFC%' THEN 4

WHEN account LIKE 'IDEX%' THEN 5

ELSE 6

END

解决方案

You could try it with UNION, like:

SELECT *

FROM (SELECT COALESCE('TOTAL ', account) AS Account

, SUM(CASE WHEN account LIKE 'INC%'

THEN (gl.credit - gl.debit)

ELSE (gl.debit - gl.credit)

END) AS Balance

FROM `tabGL Entry` gl

WHERE (NOT (account LIKE 'CASS%')

AND NOT (account LIKE 'CLIA%')

AND NOT (account LIKE 'FASS%'))

GROUP BY account WITH ROLLUP

-- UNION SELECT account

-- , CASE WHEN account LIKE 'INC%'

-- THEN (gl.credit - gl.debit)

-- ELSE (gl.debit - gl.credit)

-- END AS Balance

-- FROM `tabGL Entry` gl

-- WHERE (NOT (account LIKE 'CASS%')

-- AND NOT (account LIKE 'CLIA%')

-- AND NOT (account LIKE 'FASS%'))

) AS gl

ORDER BY CASE

WHEN account LIKE 'INC%' THEN 1

WHEN account LIKE 'DCOI%' THEN 2

WHEN account LIKE 'DMC%' THEN 3

WHEN account LIKE 'INFC%' THEN 4

WHEN account LIKE 'IDEX%' THEN 5

ELSE 6

END

I think this should do what you want

The commented-out part is for the single rows, the upper part for subtotals

Now after the last comment here the query that gives you the subtotals and the grandtotal

grouping by COALESCE seemed to be the Problem so now it's done by LEFT(account,3) and the ORDER Statement had to be modified to 3 Chars

SELECT *

FROM (SELECT LEFT(account,3) AS Account

, SUM(CASE WHEN account LIKE 'INC%'

THEN (credit - debit)

ELSE (debit - credit)

END) AS Balance

FROM acc

GROUP BY LEFT(account,3) WITH ROLLUP

) AS ac

ORDER BY CASE

WHEN account LIKE 'INC%' THEN 1

WHEN account LIKE 'DCO%' THEN 2

WHEN account LIKE 'DMC%' THEN 3

WHEN account LIKE 'INF%' THEN 4

WHEN account LIKE 'IDE%' THEN 5

ELSE 6

END

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值