[HY000] [1111]无效使用组功能

时间:2017-03-04 03:56:24

标签: mysql sql

我搜索了很多,但是错误1111的其他问题都解决了我的问题。 我的需求是计算一些身份证的不同电话号码 以下代码有效:

SELECT
  a.id_borrow_application,
  count(DISTINCT c.phone_no) CVG_CALL_OUT_COUNTS_6M
FROM t_snow_borrow_application_id a
  JOIN t_snow_call_mobile b
  JOIN t_snow_call_record_201612 c ON
                                     (
                                       a.id_borrow_application = b.id_borrow_application
                                       AND b.id = c.id_call_mobile
                                       )
WHERE c.call_type = 0
GROUP BY a.id_borrow_application;

但是当我想要一起编写4个类似的查询时,标题中的错误 发生的情况。

  

[HY000] [1111]无效使用群组功能

SELECT
  a.id_borrow_application,
  sum(CASE WHEN call_type = 0
    THEN count(DISTINCT c.phone_no)
      ELSE 0 END) CVG_CALL_OUT_COUNTS_6M,
  sum(CASE WHEN call_type = 0 AND c.days <= 30
    THEN count(DISTINCT c.phone_no)
      ELSE 0 END) CVG_CALL_OUT_COUNTS_1M,
  sum(CASE WHEN call_type = 1
    THEN count(DISTINCT c.phone_no)
      ELSE 0 END) CVG_CALL_IN_COUNTS_6M,
  sum(CASE WHEN call_type = 1 AND c.days <= 30
    THEN count(DISTINCT c.phone_no)
      ELSE 0 END) CVG_CALL_IN_COUNTS_1M
FROM t_snow_borrow_application_id a
  JOIN t_snow_call_mobile b
  JOIN t_snow_call_record_201612 c ON
                                     (
                                  a.id_borrow_application = b.id_borrow_application
                                   AND b.id = c.id_call_mobile
                                       )
GROUP BY a.id_borrow_application;

我必须写4个查询吗?

1 个答案:

答案 0 :(得分:1)

您正在嵌套聚合函数,这在MySQL中是不允许的。

对于不同的条件,您实际上并不需要sum函数来计算不同的phone_nos。从案例外部NODE *merge(NODE *pSrc1, NODE *pSrc2){ NODE *pDst = NULL; // ptr to destination (merged) list NODE *ppDst = &pDst; // ptr to pDst or some next pointer // ... *ppDst = ... // add a node to the destination list // ... ppDst = &((*ppDst)->next) // advance ppDst // ... return pDst; // return ptr to merged list } 开始,移除count (distinct的{​​{1}}函数和sum子句。

试试这个:

else