2

我有一个名为 general_ledger 的表,我需要从中显示 dr_amount、cr_amount 以及它们之间的余额作为 running_balance。这就是为什么我写了一个下面给出的查询。但是我得到了每个查询的结果,比如当前行的余额。但我需要用剩余的余额产生结果。假设第一行 dr_balance 为 20000,cr_balance 为 5000,剩余余额为 15000。在第二行中,只有 cr_balance 为 5000。现在扣除的结果应该是 10000,但我的结果是 -5000。我不知道如何解决这个问题。谁能帮我解决这个问题?我非常需要你的帮助。这是我在下面给出的查询:

    SELECT                    '' AS cost_center_id
                                        , '' AS cost_center_name
                                        , '' AS office_code
                                        , CONVERT('2013-02-01',DATETIME) AS transaction_date
                                        , '' AS accounts_head_id
                                        , '' AS account_name
                                        , '' AS opposite_accounts_head_id
                                        , '' AS opposite_account_name
                                        , 'Opening Balance' AS particulars
                                        , tempOpeningBalance.dr_amount
                                        , tempOpeningBalance.cr_amount
                                        , '' AS voucher_no
                                        , '' AS vin
                                        FROM (SELECT IFNULL(mcoa.account_code,'1101010101100321') AS account_code
                                                , IFNULL(mcoa.account_name,'Cash') AS account_name
                                                , IFNULL(mcoa.account_type,'ASSET') AS accountType
                                                , CAST(IFNULL(SUM(IFNULL(maingl.dr_balance,0)),0) AS DECIMAL(27,5)) AS dr_amount
                                                , CAST(IFNULL(SUM(IFNULL(maingl.cr_balance,0)),0) AS DECIMAL(27,5)) AS cr_amount
                                                FROM master_chart_of_accounts AS mcoa
                                                INNER JOIN chart_of_accounts AS coa ON (mcoa.id = coa.master_chart_of_accounts_id AND mcoa.id = 80)
                                                LEFT JOIN general_ledger AS maingl ON (coa.id = maingl.accounts_head_id AND coa.account_code='1101010101100321')
                                                INNER JOIN
                                                (   SELECT  gl.accounts_head_id, MAX(gl.gl_id) AS max_gl_id, gl.office_code, gl.office_type, gl.country_id,gl.cost_center_id
                                                       FROM     general_ledger AS gl
                            -- INNER JOIN voucher_info AS vi ON (gl.voucher_info_id = vi.id)
                                                        -- WHERE  vi.posting_date < '2013-02-01' AND
                                                        WHERE gl.transaction_date < '2013-02-01' AND
                                                          gl.cost_center_id IN ('BI0000000000000000000001') AND
                                                          gl.country_id IN (1) AND
                                                          gl.office_code IN ('UG500013') AND
                                                          1=1
                                                GROUP BY gl.accounts_head_id, gl.office_code, gl.office_type, gl.country_id,gl.cost_center_id
                                                ORDER BY gl.accounts_head_id
                                                ) AS tmpgl
                                                ON (    maingl.office_code = tmpgl.office_code
                                                AND maingl.office_type = tmpgl.office_type
                                                AND maingl.accounts_head_id = tmpgl.accounts_head_id
                                                AND maingl.country_id = tmpgl.country_id
                                                AND maingl.cost_center_id = tmpgl.cost_center_id
                                                AND maingl.gl_id = tmpgl.max_gl_id
                                                   )
                                                 WHERE  mcoa.account_status_id = 1 AND
                                                    coa.account_status_id = 1
                                             ) AS tempOpeningBalance

UNION

SELECT      vi.cost_center_id
      , cc.center_name AS cost_center_name
      , gl.office_code
      , vi.posting_date AS transaction_date
      , vd.accounts_head_id
      , (SELECT chart_of_accounts.account_name FROM chart_of_accounts WHERE chart_of_accounts.id = vd.accounts_head_id) AS account_name
      , vd.opposite_accounts_head_id
      , (SELECT chart_of_accounts.account_name FROM chart_of_accounts WHERE chart_of_accounts.id = vd.opposite_accounts_head_id) AS opposite_account_name
      , vd.particulars
      , gl.dr_amount AS dr_amount -- here to check
      , gl.cr_amount AS cr_amount
      , vi.voucher_no
      , vi.vin
FROM general_ledger AS gl
INNER JOIN voucher_info AS vi
 ON (gl.voucher_info_id = vi.id)
INNER JOIN cost_center AS cc
 ON (vi.cost_center_id = cc.id)
INNER JOIN voucher_details AS vd
 ON (vi.id = vd.voucher_info_id)
INNER JOIN chart_of_accounts AS coa
 ON (vd.accounts_head_id = coa.id)

WHERE   vi.posting_date BETWEEN '2013-02-01' AND'2013-02-28'
    AND vi.voucher_status_id = 3
    AND vd.status_id = 1
    AND vi.office_code = 'UG500063'
    AND coa.account_code='1101010101100321'
    AND coa.cost_center_id = 'BI0000000000000000000001'
ORDER BY   cost_center_name
     , office_code
     , transaction_date;
4

1 回答 1

2

使用这样的变量

SET @running_balance=0;
SELECT dr_amount AS dr_amount 
      , cr_amount AS cr_amount
      , @running_balance := (@running_balance + dr_amount - cr_amount)
FROM general_ledger
于 2013-03-27T05:44:43.810 回答