引用外部查询 MySQL

References Outer Query MySQL

我尝试在外部位置使用“总计”,但出现错误“未知”AL.Total,如何解决? 这是我的查询:

SELECT 
AL.CODE,
AL.NAME,
SUM(AL.BEGINING+AL.MUTATION) AS TOTAL
from
(SELECT
    ACC.CODE,
    ACC.NAME,
    ACC.BEGINING,
    SUM(JOURNAL.DEBIT-JOURNAL.KREDIT) AS MUTATION
FROM 
    G_JOURNAL INNER JOIN P ON Category.Category= G_JOURNAL.Category
    INNER JOIN JOURNAL ON G_JOURNAL.NOINDEX = JOURNAL.NOINDEX_JOURNAL
    INNER JOIN ACCOUNT ON JOURNAL.CODE = ACC.CODE  
    INNER JOIN SUBCLASS ON SUBCLASS.NOSUBCLASS= ACC.NOSUBCLASS
    INNER JOIN CLASS ON SUBCLASS.NOSUBCLASS = CLASS.NOCLASS
WHERE  
        (MONTH(G_JOURNAL.DATE) <= '12' AND YEAR (G_JOURNAL.DATE<= '2020') 
    AND 
        SUBCLASS.NOSUBCLASS=120
 GROUP BY ACC.NAME) AL
 WHERE AL.TOTAL >0
 GROUP BY AL.NAME
 ORDER BY AL.CODE ASC

尝试 HAVING TOTAL >0 而不是 WHERE AL.TOTAL >0,因为 TOTAL 不在 AL 临时 table

SELECT 
AL.CODE,
AL.NAME,
SUM(AL.BEGINING+AL.MUTATION) AS TOTAL
from
(SELECT
    ACC.CODE,
    ACC.NAME,
    ACC.BEGINING,
    SUM(JOURNAL.DEBIT-JOURNAL.KREDIT) AS MUTATION
FROM 
    G_JOURNAL INNER JOIN P ON Category.Category= G_JOURNAL.Category
    INNER JOIN JOURNAL ON G_JOURNAL.NOINDEX = JOURNAL.NOINDEX_JOURNAL
    INNER JOIN ACCOUNT ON JOURNAL.CODE = ACC.CODE  
    INNER JOIN SUBCLASS ON SUBCLASS.NOSUBCLASS= ACC.NOSUBCLASS
    INNER JOIN CLASS ON SUBCLASS.NOSUBCLASS = CLASS.NOCLASS
WHERE  
        (MONTH(G_JOURNAL.DATE) <= '12' AND YEAR (G_JOURNAL.DATE<= '2020') 
    AND 
        SUBCLASS.NOSUBCLASS=120
 GROUP BY ACC.NAME) AL
 GROUP BY AL.NAME
 HAVING TOTAL >0
 ORDER BY AL.CODE ASC