PSQL:聚合函数(总和)不起作用

时间:2015-12-07 19:53:31

标签: postgresql aggregate-functions psql

我有此查询(def autocomplete book = Book.search(params[:term], limit: 10).map(&:title) end 属于artist_money类型,例如MONEY。):

$30,456.11

当我试图得到结果时,这是错误:

SELECT SUM(
CASE
WHEN end_date - date '2015-12-3' <= 28 AND end_date - date '2015-12-3' > 0 THEN artist_money
END,
CASE
WHEN date '2015-12-3' - start_date > 28 THEN artist_money
END
) AS "gonorar"
FROM peacecard 
WHERE artist_id = 12345 AND contract IS NOT NULL

发生了什么事?根据{{​​3}},ERROR: function sum(money, money) does not exist LINE 1: SELECT sum( ^ HINT: No function matches the given name and argument types. You might need to add explicit type casts. ********** Error ********** 应该采用参数SUM类型。

非常感谢!

2 个答案:

答案 0 :(得分:2)

问题是逗号,而不是钱类型。也许你打算:

SELECT SUM(CASE WHEN end_date - date '2015-12-3' <= 28 AND end_date - date '2015-12-03' > 0
                THEN artist_money
           END),
       SUM(CASE WHEN date '2015-12-3' - start_date > 28
                THEN artist_money
           END
          ) AS "gonorar"
FROM peacecard 
WHERE artist_id = 12345 AND contract IS NOT NULL;

或者,如果您想要一个列,那么仅使用一个case更有意义:

SELECT SUM(CASE WHEN end_date - date '2015-12-3' <= 28 AND end_date - date '2015-12-03' > 0
                THEN artist_money
                WHEN date '2015-12-3' - start_date > 28
                THEN artist_money
           END
          ) AS "gonorar"
FROM peacecard 
WHERE artist_id = 12345 AND contract IS NOT NULL;

答案 1 :(得分:1)

您正尝试将两个参数传递给聚合1参数函数。