mySQL借记贷方余额

时间:2012-10-12 02:26:06

标签: mysql sum

是否可以使用mySQL查询来获取这些字段的旧余额(我的费用总和),借记(这里我应该在当前日期将总费用与相同的ID相加),信用(这些查询中不多) ,新余额(与借方相同)。

现在当这一天结束时,新余额将被添加到旧余额中,并且将为当前日期形成新余额。我有我的代码,但它不计算我以前的余额。

表:rf_expenses

--------------------------------------
id_rf_expenses   rf_expense_desc
--------------------------------------
1                    salary
2                    bonus
3                    transportation

表:费用

-----------------------------------------------------------
id_expnses    id_rf_expenses     expense_amt   expense_date
------------------------------------------------------------
1                1                  100           current
2                1                  100           yesterday
3                2                   50           current
4                2                   50           current
5                3                  200           yesterday

输出:

-----------------------------------------------------------
EXPENSE      OLD BALANCE    DEBIT     CREDIT   NEW BALANCE
-----------------------------------------------------------
SALARY           100         100        0          100          
BONUS             0          100        0          100
TRANSPO          200           0        0            0

这将发生在当前日期的每个查询中。当前的任何日期将在旧余额中总结

SELECT 
    r.rf_expense_desc, 
    COALESCE(SUM(expense_amt), 0) - COALESCE(q1.amt2, 0) OLD, 
    COALESCE(q1.amt2, 0) AS NEW
FROM 
    rf_expenses r
LEFT JOIN 
    expenses e ON r.id_RF_expense = e.id_rf_expense
LEFT JOIN 
    (SELECT 
         SUM(expense_amt) amt2, id_expense, 
         rf_expenses.id_rf_expense
     FROM 
         rf_expenses
     LEFT JOIN 
         expenses ON rf_expenses.id_RF_expense = expenses.id_rf_expense
     WHERE 
         expense_date = CURDATE( ) 
     GROUP BY 
         rf_expense_desc, expense_date) q1 ON r.id_rf_expense = q1.id_rf_expense
GROUP BY 
    rf_expense_desc 

2 个答案:

答案 0 :(得分:3)

您好我给出了两个查询:一个是日期类型列,另一个是字符串。

1个查询(包含您的示例数据) SQLFIDDLE example

    SELECT 
rf.rf_expense_desc as EXPENSE      
,(SELECT 
  COALESCE(SUM(e.expense_amt),0)
  FROM expenses e
  WHERE e.id_rf_expenses= rf.id_rf_expenses
  AND expense_date != 'current'
 ) AS 'OLD BALANCE'
,(SELECT 
  COALESCE(SUM(e.expense_amt),0)
  FROM expenses e
  WHERE e.id_rf_expenses= rf.id_rf_expenses
  AND expense_date = 'current'
 ) AS 'DEBIT'
,0 AS CREDIT
,(SELECT 
  COALESCE(SUM(e.expense_amt),0)
  FROM expenses e
  WHERE e.id_rf_expenses= rf.id_rf_expenses
  AND expense_date = 'current'
 ) AS 'NEW BALANCE'
FROM 
rf_expenses rf

2查询(包含数据类型日期) SQLFIddle example2

SELECT 
rf.rf_expense_desc as EXPENSE      
,(SELECT 
  COALESCE(SUM(e.expense_amt),0)
  FROM expenses e
  WHERE e.id_rf_expenses= rf.id_rf_expenses
  AND expense_date != CURDATE()
 ) AS 'OLD BALANCE'
,(SELECT 
  COALESCE(SUM(e.expense_amt),0)
  FROM expenses e
  WHERE e.id_rf_expenses= rf.id_rf_expenses
  AND expense_date = CURDATE()
 ) AS 'DEBIT'
,0 AS CREDIT
,(SELECT 
  COALESCE(SUM(e.expense_amt),0)
  FROM expenses e
  WHERE e.id_rf_expenses= rf.id_rf_expenses
  AND expense_date = CURDATE()
 ) AS 'NEW BALANCE'
FROM 
rf_expenses rf

结果:

|        EXPENSE | OLD BALANCE | DEBIT | CREDIT | NEW BALANCE |
---------------------------------------------------------------
|         salary |         100 |   100 |      0 |         100 |
|          bonus |           0 |   100 |      0 |         100 |
| transportation |         200 |     0 |      0 |           0 |

答案 1 :(得分:0)

另一个例子,为什么mysql应该强制所有GROUP BY子句的SELECT只有GROUP BY子句中指定的列,或者由聚合函数包围。 Mysql调用这个bug是一个“功能”。

我的意思是,查看两个GROUP BY的SELECT子句,包括内部查询和外部查询。您选择的列不在GROUP BY子句中,并且不包含SUM,AVG,COUNT或其他聚合函数。如果你对行进行分组,并且你不告诉mysql如何“减少”组中的行,它将随机取任何值。

例如,假设我们在名为“test”的表中包含此数据:

PARENTID     ID     VALUE
-------------------------
1            1      2.05
1            2      3.50
1            3      1.58
2            1      4.65
2            2      0.65

我们查询: SELECT parentid,value FROM test GROUP BY parentid,value

我们还没有说过如何处理VALUE列,例如对SUM,所以mysql将只取组中的任何值,因为PARENTID = 1我们可能得到2.05,3.50或1.58中的任何一个......但是不是它们的总和。

所以这是正确的:

SELECT parentid, SUM(value) as total FROM test GROUP BY parentid, value;

现在,当mysql减少每个组时,我们得到正确的总和,我们的结果是:

PARENTID          VALUE
-------------------------
1                 7.13
2                 5.30

因此,在您的示例中,您可能想要查询:

SELECT r.rf_expense_desc, expense_date,
    COALESCE( SUM( expense_amt ) , 0 ) - COALESCE( sum(q1.amt2), 0 ) OLD, COALESCE( sum(q1.amt2), 0 ) AS NEW
FROM rf_expenses r
LEFT JOIN expenses e ON r.id_RF_expense = e.id_rf_expense
LEFT JOIN (
    SELECT SUM( expense_amt ) amt2, count(id_expense) as counts
    FROM rf_expenses
    LEFT JOIN expenses ON rf_expenses.id_RF_expense = expenses.id_rf_expense
    WHERE expense_date = CURDATE( ) 
    GROUP BY rf_expense_desc, expense_date
) q1 ON r.id_rf_expense = q1.id_rf_expense
GROUP BY rf_expense_desc