简体   繁体   中英

SQL Query Condition With Sum

I have three tables namely the class_record, class_violation, violation.

The table class_record has these columns and data:

Class Violation CR No.            |        Class ID
000-000                           |        A30-000
000-001                           |        A30-000

The table class_violation has these columns and data:

Class Violation CR No.            |        Violation ID
000-000                           |        2
000-000                           |        1
000-001                           |        2
000-001                           |        4
000-001                           |        3

The table violation has these columns and data:

Violation ID                      |        First Amount  |        Second Amount
1                                 |        1000          |        2000
2                                 |        200           |        400
3                                 |        500           |        1000
4                                 |        500           |        1000

The table class_record contains the information of the class record.

The table class_violation is the table that contains what are the violations that is committed. And lastly, the table violation contains the information about the violations.

If the violation is committed twice, the second amount will be triggered instead of the first amount. As you can see on table class_violation , on Violation ID column, number 2 violation id is committed twice. The second amount of the must be the charged amount instead of the first amount. So the total charged amount will be the first amount plus the second amount if committed twice. My question is that how do I get the get the second amount instead of the first amount and get its total amount of the violations committed? So far here is my SQL query but is terribly wrong:

SELECT SUM(`First Amount`) 
FROM violation 
WHERE `Violation ID` 
        IN (SELECT `Violation ID` FROM class_violation 
            WHERE `Class Violation No.` 
                     IN (SELECT `Class Violation CR No.` 
                     FROM class_record WHERE `Class ID` = 'A30-000'))

Please help me. Sorry for my english. The result of the query must be:

SUM
2600

Here is my sqlfiddle link: http://sqlfiddle.com/#!2/2712a

See this query:

SELECT class_record.ClassID,SUM(A.VioAmount),GROUP_CONCAT(A.VioAmount) FROM class_record INNER JOIN (SELECT class_record.ClassID, class_record.CR_No, IF(COUNT(violation.ViolationID)=1, SUM(FAmount),(FAmount+SAmount)) AS VioAmount FROM violation INNER JOIN class_violation ON violation.ViolationID = class_violation.ViolationID INNER JOIN class_record ON class_violation.CR_No = class_record.CR_No WHERE ClassID = 'A30-000' GROUP BY violation.ViolationID, class_record.ClassID) A ON A.ClassID = class_record.ClassID AND A.CR_No = class_record.CR_No GROUP BY class_record.ClassID

You can check the SQLFiddle too at http://sqlfiddle.com/#!2/0d855b/35

Now I am taking the values in a separate Select and then JOINing it with another to obtain the SUM. Hope this solves your problem. All the Best.

You need return column "Second amount" if count(Violation ID)=2 and "First amount" if count(Violation ID)=1?

If I understand your question, look for "CASE" operator.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM