小记

Data Source=10.0.8.14;User ID=sa;Password=123;Initial Catalog=dnt31;Pooling=true

set @circleId = 1;
set @startDate = '2014-08-01 00:00:00';
set @endDate = '2014-12-30 23:59:59';
insert into app_day_count_log(dau_count,circle_id,created_at,updated_at)
select dau_count,circle_id,concat(created_at,' ',curtime())created_at,concat(updated_at,' ',curtime())updated_at from(
select count(distinct a.fromUserName)dau_count,b.app_shop_circle_id circle_id,date_format(a.created_at,'%Y-%m-%d')created_at,date_format(a.created_at,'%Y-%m-%d')updated_at from app_wechat_request a,app_member_info b
where a.fromUserName = b.openid and b.app_shop_circle_id = @circleId and a.created_at>= @startDate and a.created_at<= @endDate group by created_at)c


---使用
select y.giftname,ifnull(z.count,0)count from(select b.name_cn giftname from app_coupon_log a,app_gift_info b where (b.gift_type=1 or b.gift_type=3) and b.price=0 and a.app_gift_info_id = b.id and b.is_delete=0 and a.created_at between '2014-12-01' and '2014-12-31 23:59:59' group by a.app_gift_info_id)y left join
(select m.salename giftname,ifnull(n.usecount,0)count from(select count(a.app_gift_info_id)salecount,b.name_cn salename
from app_gift_log a,app_gift_info b where (b.gift_type=1 or b.gift_type=3) and a.app_gift_info_id = b.id and b.is_delete=0 and a.status_type = 2 and a.use_time between '2014-12-01' and '2014-12-31 23:59:59' group by a.app_gift_info_id)m
left join (select count(a.app_gift_info_id)usecount,b.name_cn usename from app_gift_log a,app_gift_info b where (b.gift_type=1 or b.gift_type=3) and a.app_gift_info_id = b.id and b.is_delete=0 and a.use_type = 1 and a.use_time between '2014-12-01' and '2014-12-31' group by a.app_gift_info_id)n on m.salename=n.usename)z on y.giftname=z.giftname group by giftname;

---销量
select y.giftname,ifnull(z.count,0)count from(select b.name_cn giftname from app_coupon_log a,app_gift_info b where (b.gift_type=1 or b.gift_type=3) and b.price=0 and a.app_gift_info_id = b.id and b.is_delete=0 and a.created_at between '2014-12-01' and '2014-12-31 23:59:59' group by a.app_gift_info_id)y left join
(select count(a.app_gift_info_id)count,b.name_cn giftname from app_gift_log a,app_gift_info b where (b.gift_type=1 or b.gift_type=3) and b.price=0 and a.app_gift_info_id = b.id and b.is_delete=0 and a.status_type in (1,2,4,5,6) and a.created_at between '2014-12-01' and '2014-12-31 23:59:59' group by a.app_gift_info_id)z on y.giftname=z.giftname group by giftname;

原文地址:https://www.cnblogs.com/fx2008/p/4212460.html