2

我需要加入三个表以显示以下内容。
我可以加入前两个并使用此查询显示所有唯一的nominal_acc_no

SELECT nominal_acc_no,coa_name FROM
(
    SELECT nominal_acc_no ,coa_name FROM acc_chart_of_accounts
    UNION
    SELECT nominal_acc_no,coa_name FROM acc_chart_of_sub_accounts
) A;

但我正在努力如何加入第三个并输出如下所示的结果。我需要对nominal_acc_no 进行分组,并将借方和贷方与acc_posting_details 表中company_id 上的WHERE 相加。任何和所有的帮助表示赞赏。

 Acc_chart_of_accounts           
Coa_id   nominal_acc_no 
    1           10   
    2           20        

acc_chart_of_sub_accounts
coa_sub_id    nominal_acc_no  company_id
      1             10                 1
      2             20                 1
      3             110                1



Acc_posting_details
Id         nominal_acc_no   debit    credit  company_id
1              10           25.00                1
2              10           15.00                1
3              20                    30.00       1
4              110          10.00                1
5              110           8.00                1

Result
Nominal_acc_no        debit       credit    company_id
10                    40.00         -           1
20                      -         30.00         1
110                   18.00         -           1
4

3 回答 3

2
SELECT
  acsa.nominal_acc_no,
  IFNULL(sum(apd.debit),0) AS debit,
  IFNULL(sum(apd.credit),0) AS credit,
  acsa.company_id
FROM acc_chart_of_sub_accounts AS acsa
  LEFT JOIN acc_posting_details AS apd
    ON acsa.nominal_acc_no = apd.Nominal_acc_no
GROUP BY acsa.nominal_acc_no, acsa.company_id

输出

Nominal_acc_no        debit     credit    company_id
-----------------------------------------------------
10                    40.00     0           1
20                    0         30.00       1
110                   18.00     0           1   

编辑:我在查询中修复了一个别名问题。

SQL Fiddle 演示在这里

于 2013-04-18T13:01:16.747 回答
0
SELECT Nominal_acc_no,SUM(debit) AS total_debit,SUM(credit) AS total_credit from   Acc_posting_details group by nominal_acc_no;

您可以在分组依据之前插入其他 where 条件。

于 2013-04-18T13:11:23.533 回答
0

Can you show the complete structure of your tables, that is, all the columns?

Your first SELECT statement refers to a column coa_name within tables acc_chart_of_accounts and acc_chart_of_sub_accounts, but then you don't mention that column in description of those tables. That makes me think there might be other relevant information that is missing.

Do tables acc_chart_of_accounts and acc_chart_of_sub_accounts contain any debits or credits? If not, why do you need info from them? Based on what I understand from your question, it seems that Acc_posting_details has all the info you need:

"I need to GROUP ON nominal_acc_no and SUM the debit & credit with a WHERE on the company_id in the acc_posting_details table"

Does this query solve your problem?

 SELECT Nominal_acc_no,
        SUM(debit) AS total_debit,
        SUM(credit) AS total_credit,
        company_id
 FROM Acc_posting_details
 WHERE company_id = 1
 GROUP BY nominal_acc_no;

ADDED:

If I understand you last comment, you need to get exctly one row for every Nominal_acc_no that exists in acc_chart_of_accounts.

In that case, Acc_posting_details does not have all the info you need, because some nominal accounts might have no debits or credits, and so they don't exist in Acc_posting_details.

 SELECT aca.Nominal_acc_no,
        IFNULL(SUM(apd.debit),0) AS total_debit,
        IFNULL(SUM(apd.credit),0) AS total_credit,
        aca.company_id
 FROM acc_chart_of_accounts aca
 LEFT OUTER JOIN Acc_posting_details apd
              ON aca.Nominal_acc_no = apd.Nominal_acc_no
 WHERE aca.company_id = 1
 GROUP BY aca.nominal_acc_no;

This query will give you one row for every nominal account in acc_chart_of_accounts. If an account has any rows in Acc_posting_details, you will get their credits and debits added together. For accounts that have no records in Acc_posting_details, you will get 0 in the total_debit and total_credit columns.

You said that you want company_id in WHERE clasue. Alternatively, if you want a complete report over all the companies, you would remove the WHERE clause and replace the GROUP BY clause with

 GROUP BY aca.nominal_acc_no, aca.company_id;

Is this what you were looking for? Let me know if it works.

于 2013-04-18T14:10:02.050 回答