简体   繁体   English

帐户总数SQL

[英]Grand total of accounts SQL

I am a student this is homework... I have one table with four columns: Accounts(numbers), balances(money), and two for descriptions. 我是一个学生,这是家庭作业...我有一个表格,该表格有四列:帐户(数字),余额(金钱)和两行用于描述。 I have to get a grand total of the balances... This VIEW shows the sums of the balance column but I need to total the sums, also. 我必须获得总计的余额...此视图显示了余额列的总和,但我也需要总计。 Any help pointing me in the right direction will be appreciated. 向我指出正确方向的任何帮助将不胜感激。

CREATE VIEW [account_balance_sums]

AS
    SELECT SUM(balance) AS total,
           SUBSTRING(Account,0,2) AS account_group,


      FROM COA

  GROUP BY account_group

GO

SELECT * FROM [account_balance_sums]

You just want the total of all balances? 您只想要所有余额的总数吗?

SELECT Sum(Balances)
FROM COA

Additionally your VIEW will not work as you cannot have an alias in a GROUP BY clause.. 另外,由于您不能在GROUP BY子句中使用别名,因此VIEW将无法使用。

Edit after comment... 评论后编辑...

I'm not sure whether the question implies that the grand total should be a part of the view, also is your account number column numeric? 我不确定这个问题是否意味着总计应该是视图的一部分,您的帐号列也是数字的吗? As SUBSTRING will not work. 由于SUBSTRING无法使用。

CREATE VIEW viewAccount_Balance_Sums
AS 
SELECT SUM(Balance) as Total, LEFT(Account,2) AS Account_group
FROM  COA
GROUP BY LEFT(Account,2)
UNION ALL
SELECT SUM(Balance), 'Grand Total'
FROM COA

Try totaling them the same way the view creates a total for each account, by using SUM? 尝试使用SUM,以与视图为每个帐户创建总计相同的方式对它们进行总计?

SELECT SUM(balance) FROM COA

(Just don't GROUP BY , so that you get a full total instead of just a per-accountgroup total.) (只是不要使用GROUP BY ,这样您可以获得全部总计,而不是每个帐户组总计。)

Alternatively, you could sum the account totals returned from the view: 或者,您可以对从视图返回的帐户总数求和:

SELECT SUM(total) FROM [account_balance_sums]

尝试将SUM与查询中的视图结合使用。

SELECT SUM(balance) AS total, SUBSTRING(Account,0,2) AS account_group 
FROM COA    
GROUP BY account_group wirh rollup

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM