English 中文(简体)
在查询中将总和乘以数
原标题:Sums are being multiplied in query
  • 时间:2012-05-23 14:41:20
  •  标签:
  • mysql

我在研究两个问题做家庭作业, 过了好几个小时,我刚解决了这两个问题, 我最后一个问题是,我的两个问题 都以数字值翻了一番,而不是单一个数字值。

这就是我有的:

SELECT SUM(P.AMT_PAID) AS TOTAL_PAID, C.CITATION_ID, C.DATE_ISSUED, SUM(V.FINE_CHARGED) AS TOTAL_CHARGED
FROM PAYMENT P, CITATION C, VIOLATION_CITATION V
WHERE V.CITATION_ID = C.CITATION_ID
AND C.CITATION_ID = P.CITATION_ID
GROUP BY C.CITATION_ID;

我的另一面,

SELECT C.CITATION_ID, C.DATE_ISSUED, SUM(V.FINE_CHARGED) AS TOTAL_CHARGED, SUM(P.AMT_PAID) AS TOTAL_PAID, SUM(V.FINE_CHARGED) - SUM(P.AMT_PAID) AS TOTAL_OWED
FROM (CITATION C)
LEFT JOIN VIOLATION_CITATION V
ON V.CITATION_ID = C.CITATION_ID
LEFT JOIN PAYMENT P
ON P.CITATION_ID = C.CITATION_ID
GROUP BY C.CITATION_ID
ORDER BY TOTAL_OWED DESC;

我相信有些事情我忽略了,如果有人能告诉我我在哪里,那将是一个很大的帮助。

最佳回答
Select Sum(P.Amt_Paid) As Total_Paid, C.Citation_Id
    , C.Date_Issued, Sum(V.Fine_Charged) As Total_Charged
From Payment P
    Join Citation C
        On C.Citation_Id = P.Citation_Id
    Join Violation_Citation V
        On V.Citation_Id = C.Citation_Id
Group By C.Citation_Id

首先,您应该使用 JOIN 语法, 而不是使用不受逗号限制的表格列表。 这样可以更容易阅读,更标准化,并且通过忽略过滤条款来帮助预防问题。

其次,一个数额过大的最可能的原因是加入 VIOLATION_CITATION 表格。如果将组除去和具有综合功能的列,你可能会看到,对于每个 P.AMT_PAID 的情况,都重复使用 VIOLAION_CITATION 。也许,以下将解决问题:

Select Coalesce(PaidByCitation.TotalAmtPaid,0) As Total_Paid
    , C.Citation_Id, C.Date_Issued
    , Coalesce(ViolationByCitation.TotalCharged,0) As Total_Charged
    , Coalesce(ViolationByCitation.TotalCharged,0)
        - Coalesce(PaidByCitation.TotalAmtPaid,0) As Total_Owed
From Citation As C
    Left Join   (
                Select P.Citation_Id, Sum( P.Amt_Paid ) As TotalAmtPaid
                From Payment As P
                Group By P.Citation_Id
                ) As PaidByCitation
        On PaidByCitation.Citation_Id = C.Citation_Id
    Left Join   (
                Select V.Citation_Id, Sum( V.Find_Charged ) As TotalCharged
                From Violation_Citation As V
                Group By V.Citation_Id
                ) As ViolationByCitation
        On ViolationByCitation.Citation_Id = C.Citation_Id

使用 Coalesce 是为了确保如果左键返回给定的 Citation_ID 值,则没有行,我们用零替换 Null

问题回答

暂无回答




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签