2011-09-13 57 views
1

请帮我解决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

什么是预期结果,您获得了什么? – Eineki

+0

@Eineki我会更新预期的结果... – seoppc

+0

@Eineki请找到预期的结果。 – seoppc

回答

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 
+0

请帮忙编写完整的查询 – seoppc

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使自己改进和简化组通过,从而避免了查询抑制,但生产higly不可预料的结果,尤其是如果你的查询很复杂。

如果你不需要为每个s.open_bal和s.discount设置不同的结果行,那么你可能不需要select中的这些(重复)数据。

然后我没有花时间分析完整的查询。但是你的需求似乎相当复杂。我会说分而治之,KISS(Keep It Stupid Simple),让你完全理解一些查询而不是一个巨大的查询。特别是如果某些结果的要求不同(一些涉及细节,一些涉及聚合,一些涉及不同聚合等),因为您可能需要一些窗口功能(“按关键字分区”),而您没有这些功能在MySQL上。

+0

*“GROUP BY应该包含select中不是聚合表达式的每个元素。”*这对于初学者和RDBMS来说都是不错的建议,它不支持“select的每个元素都应该取决于聚合表达式或者在功能上依赖于GROUP BY列表中的元素。“ –

+0

@regilero你能帮我写完整的查询吗,我会非常感谢你。 – seoppc

+0

@seoppc:今天时间不多。首先尝试对每个结果进行查询。然后,当这将是确定和测试,你可能有时间合并所有这些查询在一个。 – regilero