mysql错误结果大查询

时间:2011-09-13 07:32:25

标签: mysql

请帮我修复mysql查询并获得正确的结果......

请参阅以下表格的数据集......

| id | name  | batch | discount | open_bal | inactive |
+----+-------+-------+----------+----------+----------+
|  1 | Ash   | 19    | 0        | -5000    | 0        |
+----+-------+-------+----------+----------+----------+
|  2 | Tuh   | 15    | 0        | 0        | 0        |
+----+-------+-------+----------+----------+----------+

发票

| id   | invoice_num | student_id | reg_fee | tut_fee | other_fee | discount |
+------+-------------+------------+---------+---------+-----------+----------+
|  1   | 2011/1      | 1          | 5000    | 0       | 0         |  0       |
+------+-------------+------------+---------+---------+-----------+----------+
|  137 | 2011/137    | 1          | 15000   | 0       | 0         |  0       |
+------+-------------+------------+---------+---------+-----------+----------+
|  169 | 2011/169    | 2          | 15000   | 0       | 0         |  0       |
+------+-------------+------------+---------+---------+-----------+----------+

recipts

| id   | recipt_num  | student_id | reg_fee | tut_fee | other_fee | status     |
+------+-------------+------------+---------+---------+-----------+------------+
|  264 | 2011/264    | 1          | 0       | 15000   | 0         |  confirmed |
+------+-------------+------------+---------+---------+-----------+------------+
|  18  | 2011/18     | 2          | 0       | 5250    | 0         |  confirmed |
+------+-------------+------------+---------+---------+-----------+------------+
|  251 | 2011/251    | 2          | 4650    | 0       | 0         |  pending   |
+------+-------------+------------+---------+---------+-----------+------------+

批次

| id  | name     |
+-----+----------+
|  19 | S.T-11   |
+-----+----------+
|  15 | Mc/11-13 |
+-----+----------+

我想按批次实现报告......

Batch id - batch id from batches table
Batch Name - batch name from batches table
Total Students - count(s.id) from students table group by batch
Opening Bal - sum(s.openbal) from students table
Gross Fee - sum(reg_fee+tut_fee+other_fee) from invoices table
Discount - sum(i.discount) from invoices table
Net Payable - (openbal + grossfee) - discount
Net Received - sum(reg_fee+tut_fee+other_fee) from recipts table where r.status = 'confirmed'
Due Balance - Net Payable - Net Received

预期报告

| batch_id | batch_name | total_students | opening_bal | gross_fee | discount | net_payable | net_recieved | due_balance |
+----------+------------+----------------+-------------+-----------+----------+-------------+--------------+-------------+
|  15      | 2011/264   | 1              | 0           | 15000     | 0        |  15000      |    5250      | 9750        |
+----------+------------+----------------+-------------+-----------+----------+-------------+--------------+-------------+
|  19      | S.T-11     | 1              | -5000       | 20000     | 0        |  15000      | 15000        | 0           |
+----------+------------+----------------+-------------+-----------+----------+-------------+--------------+-------------+

我尝试使用以下查询,但结果错误。

SELECT b.name AS batch_name, 
       b.id AS batch_id, 
       COUNT( s.id ) AS total_students, 
       COALESCE( s.open_bal, 0 ) AS open_balance, 
       COALESCE( sum( i.reg_fee + i.tut_fee + i.other_fee ) , 0 ) AS gross_fee,
       COALESCE( s.discount, 0 ) , 
       COALESCE( sum( i.reg_fee + i.tut_fee + i.other_fee ) , 0 ) - 
                 COALESCE( s.discount, 0 ) AS net_payable, 
       COALESCE( sum( r.reg_fee + r.tut_fee + r.other_fee ) , 0 ) AS net_recieved,
       COALESCE( s.discount, 0 ) , 
       COALESCE( sum( i.reg_fee + i.tut_fee + i.other_fee ) , 0 ) - 
                 COALESCE( s.discount, 0 ) - 
                 COALESCE( sum( r.reg_fee + r.tut_fee + r.other_fee ) , 0 ) 
                 AS due_balance
FROM batches b
LEFT JOIN students s ON s.batch = b.id
LEFT JOIN invoices i ON i.student_id = s.id
LEFT JOIN recipts r ON r.student_id = s.id
WHERE s.inactive =0 and r.status = 'confirmed'
GROUP BY b.name;

请帮我改写这个查询...

2 个答案:

答案 0 :(得分:1)

谈论SQL这一行当然是错误的:

GROUP BY b.name;

GROUP BY应包含select的每个元素,而不是聚合表达式。

使用以下方法尝试查询:

GROUP BY b.name,b.id,COALESCE(s.open_bal,0), COALESCE(s.discount,0);

当你没有制作正确的GROUP BY表达式时,MySQL会自己创建改进和简化的组,这可以避免查询拒绝,但会产生非常明显的结果,特别是如果你的查询很复杂的话。 p>

如果你不需要每个s.open_bal和s.discount都有一个明确的结果行,那么你可能不需要在select中使用这些(重复)数据。

然后我没有花时间分析完整的查询。但是你的需求似乎很复杂。我会说Divide and conquer,KISS(Keep It Stupid Simple),让你完全理解几个查询而不是一个大问题。特别是如果一些结果的要求不同(一些处理细节,一些处理聚合,一些处理不同的聚合等),因为你可能需要一些你没有的窗口函数(“partition by”关键字)在MySQL上。

答案 1 :(得分:0)

也许你应该像这个例子一样尝试修复你的总和:

COALESCE( sum( i.reg_fee + i.tut_fee + i.other_fee ) , 0 ) //bad
 sum( COALESCE(i.reg_fee,0) + COALESCE(i.tut_fee,0) + COALESCE(i.other_fee,0) )  //good