2017-02-23 87 views
2

我想使用GROUP BY CASE语句量化我的查询输出。 我在SELECT部分​​中的GROUP BY部分尝试了CASE,但仍得到“错误代码:1111.无效的组功能使用。”MySQL GROUP BY CASE错误

这里是我的代码(与CASE两节)

SELECT 
case when BBsum.sum <= 9999 then '<10K' 
    when BBsum.sum <= 10999 then '<11K' 
    when BBsum.sum <= 11999 then '<12K' 
    when BBsum.sum <= 12999 then '<13K' 
    when BBsum.sum <= 13999 then '<14K' 
    when BBsum.sum <= 14999 then '<15K' 
    when BBsum.sum <= 15999 then '<16K' 
    when BBsum.sum <= 16999 then '<17K' 
    when BBsum.sum <= 17999 then '<18K' 
    when BBsum.sum <= 18999 then '<19K' 
    when BBsum.sum <= 19999 then '<20K' 
    else '20K+' end as sum, 
avg(sum(CASE when C.finance_type = 'INSTALLMENT_LOAN' then C.paid_amount end)/sum(CASE when C.finance_type = 'INSTALLMENT_LOAN' then C.initial_amount end)) as IL_RR, 
avg(sum(CASE when C.finance_type = 'PAYDAY_LOAN' then C.paid_amount end)/sum(CASE when C.finance_type = 'PAYDAY_LOAN' then C.initial_amount end)) as PDL_RR, 
avg(sum(C.paid_amount)/sum(C.initial_amount)) as RR 
FROM 
credit C 
left join (select BB.borrower_id id, sum(BB.points) sum from borrower_bonus BB 
where BB.type = 'CHARGE' 
group by bb.borrower_id) BBsum on C.borrower_id = BBsum.id 
where C.status IN ('EXPIRED', 'COMPLETED','SOLD') 
group by case when BBsum.sum <= 9999 then '<10K' 
    when BBsum.sum <= 10999 then '<11K' 
    when BBsum.sum <= 11999 then '<12K' 
    when BBsum.sum <= 12999 then '<13K' 
    when BBsum.sum <= 13999 then '<14K' 
    when BBsum.sum <= 14999 then '<15K' 
    when BBsum.sum <= 15999 then '<16K' 
    when BBsum.sum <= 16999 then '<17K' 
    when BBsum.sum <= 17999 then '<18K' 
    when BBsum.sum <= 18999 then '<19K' 
    when BBsum.sum <= 19999 then '<20K' 
    else '20K+' end 
order by sum DESC 

我怀疑,我必须做一些更简单的实现我想要的(分裂RR平均收到奖金的同伙)

+2

你正在嵌套聚合函数'avg(sum('。MySQL does not allow that – GurV

+0

)不要使用'sum'作为列的别名,它可能会被函数sum的代码弄糊涂,试试类似sumOfBB。即使mysql引擎碰巧让它通过,它的错误练习 – gbtimmon

回答

2

您正在嵌套聚合函数,MySQL不允许。

这可能是你想要什么:

SELECT 
case when BBsum.sum <= 9999 then '<10K' 
    when BBsum.sum <= 10999 then '<11K' 
    when BBsum.sum <= 11999 then '<12K' 
    when BBsum.sum <= 12999 then '<13K' 
    when BBsum.sum <= 13999 then '<14K' 
    when BBsum.sum <= 14999 then '<15K' 
    when BBsum.sum <= 15999 then '<16K' 
    when BBsum.sum <= 16999 then '<17K' 
    when BBsum.sum <= 17999 then '<18K' 
    when BBsum.sum <= 18999 then '<19K' 
    when BBsum.sum <= 19999 then '<20K' 
    else '20K+' end as sum, 
sum(CASE when C.finance_type = 'INSTALLMENT_LOAN' then C.paid_amount end) 
    /sum(CASE when C.finance_type = 'INSTALLMENT_LOAN' then C.initial_amount end) as IL_RR, 
sum(CASE when C.finance_type = 'PAYDAY_LOAN' then C.paid_amount end) 
    /sum(CASE when C.finance_type = 'PAYDAY_LOAN' then C.initial_amount end) as PDL_RR, 
sum(C.paid_amount)/sum(C.initial_amount) as RR 
FROM 
credit C 
left join (
    select BB.borrower_id id, sum(BB.points) sum 
    from borrower_bonus BB 
    where BB.type = 'CHARGE' 
    group by bb.borrower_id 
) BBsum on C.borrower_id = BBsum.id 
where C.status IN ('EXPIRED', 'COMPLETED','SOLD') 
group by case when BBsum.sum <= 9999 then '<10K' 
    when BBsum.sum <= 10999 then '<11K' 
    when BBsum.sum <= 11999 then '<12K' 
    when BBsum.sum <= 12999 then '<13K' 
    when BBsum.sum <= 13999 then '<14K' 
    when BBsum.sum <= 14999 then '<15K' 
    when BBsum.sum <= 15999 then '<16K' 
    when BBsum.sum <= 16999 then '<17K' 
    when BBsum.sum <= 17999 then '<18K' 
    when BBsum.sum <= 18999 then '<19K' 
    when BBsum.sum <= 19999 then '<20K' 
    else '20K+' end 
order by sum DESC 
+0

它的工作原理没有平均值,谢谢! –

0

我想你需要看您的加入你想只计算BB成员负责账目的值,但由于左的加入,行在borrower_bonus表中没有'CHARGE'行的贷项表将返回空值填充,然后分组到平均值中。因此,您正在计算所有奖金帐户的平均值,而不仅是计费帐户。内部加入将确保只有收费帐户得到平均。

SELECT -- SELECT THE AVERAGES 
    CASE 
     WHEN BBSum.pntSum = 21 THEN '20K+' 
     ELSE CONCAT('<', BBSum.pntSum, '+') 
    END AS AccountType, 
    AVG(BBSum.RR), 
    AVG(BBSum.IL_RR), 
    AVG(BBSum.PDL_RR) 
FROM 
(
    SELECT --FIND THE RR, PDL_RR and IL_RR for each account. 
     Credit.paid_amount/Credit.initial_amount AS RR, 
     CASE WHEN Credit.finance_type = 'INSTALLMENT_LOAN' THEN RR ELSE 0 END AS IL_RR, 
     CASE WHEN Credit.finance_type = 'PAYDAY_LOAN'  THEN RR ELSE 0 END AS PDL_RR 
    FROM 
     Credit 
    WHERE 
     Credit.status IN ('EXPIRED', 'COMPLETED','SOLD') 
     AND Credit.finance_type IN ('INSTALLMENT_LOAN', 'PAYDAY_LOAN') 
) AS C 
INNER JOIN -- get all credit lines with corresponding borrowers rows. 
(
    SELECT -- Find the CHARGE account and their grouping 
     BB.borrower_id AS id 
     , CASE 
      WHEN SUM(BB.points) <= 9999 THEN 10 
      WHEN SUM(BB.points) <= 19999 THEN FLOOR(SUM(BB.points))/1000) + 1 
      ELSE 21 
     END AS pntSum 
    FROM 
     borrower_bonus AS BB 
    WHERE 
     BB.type = 'CHARGE' 
    GROUP BY 
     BB.borrower_id 
) as BBSum 
ON 
    C.Borrower_id = BBSum.id 
GROUP BY 
    BBSum.pntSum 
ORDER BY 
    BBSum.pntSum DESC 
0

这只是关于如何编写查询的建议。我认为GurV正确识别的问题:

SELECT bbgroup, 
     (sum(CASE when C.finance_type = 'INSTALLMENT_LOAN' then C.paid_amount end)/
     sum(CASE when C.finance_type = 'INSTALLMENT_LOAN' then C.initial_amount end) 
     ) as IL_RR, 
     (sum(CASE when C.finance_type = 'PAYDAY_LOAN' then C.paid_amount end)/
     sum(CASE when C.finance_type = 'PAYDAY_LOAN' then C.initial_amount end) 
     ) as PDL_RR, 
     (sum(C.paid_amount)/sum(C.initial_amount)) as RR 
FROM credit C left join 
    (select BB.borrower_id, sum(BB.points) as points_sum, 
      (case when sum(BB.points) <= 9999 then '<10K' 
        when sum(BB.points) <= 10999 then '<11K' 
        when sum(BB.points) <= 11999 then '<12K' 
        when sum(BB.points) <= 12999 then '<13K' 
        when sum(BB.points) <= 13999 then '<14K' 
        when sum(BB.points) <= 14999 then '<15K' 
        when sum(BB.points) <= 15999 then '<16K' 
        when sum(BB.points) <= 16999 then '<17K' 
        when sum(BB.points) <= 17999 then '<18K' 
        when sum(BB.points) <= 18999 then '<19K' 
        when sum(BB.points) <= 19999 then '<20K' 
        else '20K+' 
       end) as bbgroup 
     from borrower_bonus BB 
     where BB.type = 'CHARGE' 
     group by bb.borrower_id 
    ) BBsum 
    on C.borrower_id = BBsum.borrower_id 
where C.status IN ('EXPIRED', 'COMPLETED','SOLD') 
group by bbgroup 
order by bbgroup DESC; 

把一个复杂的case声明在一个地方确实有助于可维护性和可读性。我也会改变条件以符合描述:

   (case when sum(BB.points) < 10000 then '<10K' 
        when sum(BB.points) < 11000 then '<11K' 
        . . . 

但是,您可能有使用999版本的特定业务规则。