如何在不重复的情况下对不同值进行求和

时间:2018-06-04 08:12:42

标签: postgresql

如何在列上没有重复的不同值的情况下,对不同的值但是相同的ID进行求和?

我在SQL命令中的输入。

    SELECT
      students.id        AS student_id,
      students.name,
      COUNT(*)           AS enrolled,
      c2.price           AS course_price,
      (COUNT(*) * price) AS paid
    FROM students
      LEFT JOIN enrolls e on students.id = e.student_id
      LEFT JOIN courses c2 on e.course_id = c2.id
    WHERE student_id NOTNULL
    GROUP BY students.id, students.name, c2.price
    ORDER BY student_id ASC;

我的结果。

 student_id |        name         | enrolled | paid 
------------+---------------------+----------+------
       1001 | Gulbadan Bálint     |        1 |   90
       1002 | Hanna Adair         |        5 |  450
       1003 | Taddeo Bhattacharya |        1 |   90
       1004 | Persis Havlíček     |        1 |   75
       1004 | Persis Havlíček     |        5 |  450
       1005 | Tory Bateson        |        1 |   90
       1007 | Dávid Fèvre         |        1 |   90
       1008 | Masuyo Stoddard     |        1 |   90
       1009 | Iiris Levitt        |        1 |   75
       1009 | Iiris Levitt        |        2 |  180
       1013 | Artair Kovač        |        1 |   30
       1013 | Artair Kovač        |        1 |   90
       1015 | Matilda Guinness    |        2 |  180
       1017 | Margarita Ek        |        1 |   90
       1018 | Misti Zima          |        3 |  270
       1019 | Conall Ventura      |        1 |   90
       1020 | Vivian Monday       |        2 |  180

我的预期结果。

 student_id |        name         | enrolled | paid 
------------+---------------------+----------+------
       1001 | Gulbadan Bálint     |        1 |   90
       1002 | Hanna Adair         |        5 |  450
       1003 | Taddeo Bhattacharya |        1 |   90
       1004 | Persis Havlíček     |        6 |  525
       1005 | Tory Bateson        |        1 |   90
       1007 | Dávid Fèvre         |        1 |   90
       1008 | Masuyo Stoddard     |        1 |   90
       1009 | Iiris Levitt        |        3 |  255
       1013 | Artair Kovač        |        2 |  120
       1015 | Matilda Guinness    |        2 |  180
       1017 | Margarita Ek        |        1 |   90
       1018 | Misti Zima          |        3 |  270
       1019 | Conall Ventura      |        1 |   90
       1020 | Vivian Monday       |        2 |  180

我认为原因来自GROUP BY命令,但如果我不编写GROUP BY价格,则会抛出错误。

2 个答案:

答案 0 :(得分:0)

也许你可以使用SUM()函数。 请参阅下面的链接,也许与您的情况相同:
how to group by and return sum row in Postgres

答案 1 :(得分:0)

您已在当前和预期结果中排除了course_price列。您似乎错误地将其包含在group by

SELECT
  students.id        AS student_id,
  students.name,
  COUNT(*)           AS enrolled,
  --c2.price         AS course_price, --exclude this in o/p?
  (COUNT(*) * price) AS paid
FROM students
  LEFT JOIN enrolls e on students.id = e.student_id
  LEFT JOIN courses c2 on e.course_id = c2.id
WHERE student_id NOTNULL
GROUP BY students.id, students.name --,c2.price --and remove it from here 
ORDER BY student_id ASC;