Mysql SQL:计算刚刚结转的计数列

Mysql SQL:计算刚刚结转的计数列,mysql,sql,count,group-by,Mysql,Sql,Count,Group By,好的,我能不能在使用COUNT&的同一个查询中对列进行数学运算?下面是错误消息 “#1064-您的SQL语法有错误;请检查以下手册: 对应于您的MySQL服务器版本,以便使用正确的语法 近“不同的付款。客户id)作为Jan_retain_cust, Jan_将客户/1533保留为3号线的CAC” 您缺少一个group by子句。此外,您试图在同一个select中的表达式中使用别名Jan\u retain\u cust,这是不允许的。只需将别名Jan\u retain\u cust替换为原始表达式

好的,我能不能在使用COUNT&的同一个查询中对列进行数学运算?下面是错误消息

“#1064-您的SQL语法有错误;请检查以下手册: 对应于您的MySQL服务器版本,以便使用正确的语法 近“不同的付款。客户id)作为Jan_retain_cust, Jan_将客户/1533保留为3号线的CAC”


您缺少一个
group by
子句。此外,您试图在同一个select中的表达式中使用别名
Jan\u retain\u cust
,这是不允许的。只需将别名
Jan\u retain\u cust
替换为原始表达式,即
COUNT(DISTINCT payments.customer\u id)


您不能在同一查询中引用别名列

select January.customer_id as Jancust_id,
    COUNT(distinct January.customer_id) as Jan_orig_cust,
    COUNT(distinct payments.customer_id) as Jan_retain_cust,
    COUNT(distinct payments.customer_id) / 1533 as CAC_referral
from January_Cohort January
left join telemon_payments_data payments on January.customer_id = payments.customer_id
where January.acquisition_source = 'referral'
    and payments.payment_date between '2016-06-01' and '2016-06-31'
group by January.customer_id;
选择一月。客户id作为Jancust\u id,
将(不同的1月份客户id)计算为1月份原始客户,
将(不同的付款。客户id)计算为1月1日保留客户,

Jan_retain_cust/1533作为CAC_推荐,谢谢,所以这就是我上面提到的
select January.customer_id as Jancust_id,
    COUNT(distinct January.customer_id) as Jan_orig_cust,
    COUNT(distinct payments.customer_id) as Jan_retain_cust,
    COUNT(distinct payments.customer_id) / 1533 as CAC_referral
from January_Cohort January
left join telemon_payments_data payments on January.customer_id = payments.customer_id
where January.acquisition_source = 'referral'
    and payments.payment_date between '2016-06-01' and '2016-06-31'
group by January.customer_id;
SELECT January.customer_id AS Jancust_id, 
COUNT( DISTINCT January.customer_id ) AS Jan_orig_cust,
COUNT (DISTINCT payments.customer_id) as Jan_retain_cust,
Jan_retain_cust/ 1533 AS CAC_referral    <--- You can not do.
FROM January_Cohort January
LEFT JOIN telemon_payments_data payments
ON January.customer_id = payments.customer_id
WHERE January.acquisition_source = 'referral'
AND 
payments.payment_date BETWEEN '2016-06-01' and '2016-06-31'