0

是否有可能有一个 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 
4

2 回答 2

3

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

1 查询(使用您的示例数据)SQLFIDDLE 示例

    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 示例2

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 |
于 2012-10-12T06:45:45.833 回答
0

另一个例子说明为什么 mysql 应该强制所有 GROUP BY 子句的 SELECT 只包含 GROUP BY 子句中指定的列,或者由聚合函数包围。Mysql 将此错误称为“功能”。

我的意思是,看看你的两个 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 
于 2012-10-12T02:44:59.460 回答