mysql按周一到周日分段统计

图片说明
如图,表名lottery_activity_record,现在需要统计一个起止时间范围内,某个活动每周一至周日的人均数量,即每一周内的该活动(activity_Id)的总量除以人数(DISTINCT open_Id),结果四舍五入取整,存在跨年的情况.很急很急,求大神们帮助

2个回答

差不多下面这样
 SELECT s/n,t1.w from (
select sum(activity_id) S,w from (
select activity_id,WEEK(createtime,1) as w from lottery_activity_record
) b group by w
) t1
,
(
select count(open_id) n,w from(
select distinct open_Id,WEEK(createtime,1) as w from lottery_activity_record
) a group by w
) t2 where t1.w=t2.w

SELECT s/n,t1.w from (
select sum(activity_id) S,w from (
select activity_id,WEEK(createtime,1) as w from lottery_activity_record
) b group by w
) t1
,
(
select count(open_id) n,w from(
select distinct open_Id,WEEK(createtime,1) as w from lottery_activity_record
) a group by w
) t2 where t1.w=t2.w

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问