列“ic.inscount”必须出现在GROUP BY子句中或用于聚合函数

时间:2016-09-22 03:02:18

标签: sql postgresql group-by

当我运行此查询时

 WITH install_count_join_date AS (
  SELECT 
    date_trunc('month', join_date) AS date, 
    COUNT(*) AS "inscountjoin"
  FROM 
    apps202_prod.search 
  WHERE 
    join_date >= '2016-06-01'
  AND
    app_id = 3
  GROUP BY 
    date_trunc('month', join_date)
), install_count AS (
  SELECT
    DATE(original_timestamp) AS date,
    COUNT(*) AS "inscount"
  FROM 
    apps202_prod.search 
  WHERE 
    original_timestamp >= '2016-06-01'
  AND
    app_id = 3
  GROUP BY 
    DATE(original_timestamp)
) 

SELECT 
  date_trunc('month', mr.date) AS "money_revenue_date",
  SUM(mr.amount) AS "amt",
  ic.inscount AS "install_count"
FROM 
  mysql_apps202_prod.apps202_prod_money_revenue mr
  join install_count ic on date_trunc('month', ic.date) = date_trunc('month', mr.date)
WHERE
  date_trunc('month', mr.date)  >= '2016-06-01'
AND
  mr.app_id = 3

GROUP BY
  date_trunc('month', mr.date)

我收到此错误:

  

列ic.inscount必须出现在GROUP BY子句中或用于聚合函数

1 个答案:

答案 0 :(得分:0)

你必须更改你的select语句如下所示,因为你得到了很多关于你正在处理的问题的反馈,如果你使用group,那么你选择所有具有聚合函数的列除了group by。在你的情况下,ic.inscount既不在group by也不使用任何聚合函数,所以请根据你的要求使用下面的一个。

SELECT 
   date_trunc('month', mr.date) AS "money_revenue_date",
   SUM(mr.amount) AS "amt",
   ic.inscount AS "install_count"
FROM mysql_apps202_prod.apps202_prod_money_revenue mr
join install_count_join_date ic on date_trunc('month', ic.date) = date_trunc('month', mr.date)
WHERE date_trunc('month', mr.date)  >= '2016-06-01'
    AND mr.app_id = 3
GROUP BY date_trunc('month', mr.date), ic.inscount

OR

SELECT 
    date_trunc('month', mr.date) AS "money_revenue_date",
    SUM(mr.amount) AS "amt",
    MAX(ic.inscount) AS "install_count" --Any Addregate Function
FROM mysql_apps202_prod.apps202_prod_money_revenue mr
join install_count_join_date ic on date_trunc('month', ic.date) = date_trunc('month', mr.date)
WHERE date_trunc('month', mr.date)  >= '2016-06-01'
    AND mr.app_id = 3
GROUP BY date_trunc('month', mr.date)