Mysql计算错误

时间:2016-01-20 13:34:00

标签: mysql database

我修复了最后一期,但现在还有其他的一点:我用这种格式编辑了我的查询。但是,我仍然有一个大问题 - PU_1day& PU_7day计算错误:每行附加“1”(当true为2时为3,当为0时为1)。我怎么解决这个问题? :(

SELECT u.date, 
       u.des_channel, 
       u.des_type, 
       u.country,
       count(distinct(u.id)) as Reg_n, 
       sum(if((u.date=bp.date)*(u.id=bp.user_id),bp.o_outcome,0)) as P_$,
       sum(if((u.date=bp.date)*(u.id=bp.user_id),1,0)) as P_n,
       count(distinct(if((u.date=bp.date)*(u.id=bp.user_id),bp.user_id,0))) as PU_1day,
       count(distinct(if(truncate(timestampdiff(hour,u.datetime,bp.datetime)/24,0)<7,bp.user_id,0))) as PU_7day
FROM mayadata.users u 
left join mayadata.billing_pays bp
       on u.id=bp.user_id
WHERE  u.country in ('TH','ZA','ID','IN','NG','MY') and
       truncate(timestampdiff(hour,u.datetime,bp.datetime)/24,0)<7 and 
       u.date>='2011-01-01' 
GROUP BY u.date, u.des_channel, u.des_type

1 个答案:

答案 0 :(得分:1)

您可以尝试在PU_1day&amp;中替换 0 中的第二个参数 null PU_7day计算表达式如下:

SELECT u.date, 
       u.des_channel, 
       u.des_type, 
       u.country,
       count(distinct(u.id)) as Reg_n, 
       sum(if((u.date=bp.date)*(u.id=bp.user_id),bp.o_outcome,0)) as P_$,
       sum(if((u.date=bp.date)*(u.id=bp.user_id),1,0)) as P_n,
       count(distinct(if((u.date=bp.date)*(u.id=bp.user_id),bp.user_id,null))) as PU_1day,
       count(distinct(if(truncate(timestampdiff(hour,u.datetime,bp.datetime)/24,0)<7,bp.user_id,null))) as PU_7day
FROM mayadata.users u 
left join mayadata.billing_pays bp
       on u.id=bp.user_id
WHERE  u.country in ('TH','ZA','ID','IN','NG','MY') and
       truncate(timestampdiff(hour,u.datetime,bp.datetime)/24,0)<7 and 
       u.date>='2011-01-01' 
GROUP BY u.date, u.des_channel, u.des_type