select count(*) as k,day, dealer_id,store_id, product_id from ydd_fw_scans group by day, dealer_id, store_id, product_id having k > 100 order by k desc limit 10;
select count(*) from (select day, product_id, dealer_id, count(distinct(member_id)) as user_num, count(if(is_first=1,1,0)) as first_scan_num from ydd_fw_scans where day = 20240212 group by day, product_id, dealer_id) as a;
+----------+
| count(*) |
+----------+
| 201 |
+----------+
```
```
select count(*) from (select out_day, day, dealer_id, product_id, count(distinct(member_id)) as user_num, count(if(is_first=1,1,0)) as first_scan_num from ydd_fw_scans where day = 20240212 group by day, out_day, dealer_id, product_id) as a;
left join ydd_fw_scan_awards as a on s.id = a.scan_id
left join ydd_member as b on a.member_id = b.id
where
a.day BETWEEN 20240101 AND 20240530 and
s.day BETWEEN 20240101 AND 20240530 and
a.out_day BETWEEN 20240101 AND 20240530 and
s.out_day BETWEEN 20240101 AND 20240530 and
(s.product_id = 111,
s.product_sku_sn = 'xxxxx') or ((s.product_id in (1111,2222))
a.created_time BETWEEN 20240101 AND 20240530 and
s.province_id in (130433, 153727, 163915, 195850, 195852, 195860, 195861) and
s.city_id in (280, 259) and
s.district_id in (111, 222)
s. is_first =1
order by a.id desc
limit 0, 20
;
```
# 二次统计方案
## 概述
- 把扫描数据拆分为两个表: 1. 扫描数据概述 2. 扫描数据明细
- 按日期:每天。进行数据写入。 写入的数据支持,覆写原有的数据
- 扫描数据概述的数据目的是进行每天统计写入
- 扫描数据明细为了缩减表字段和减少多表关联
## 数据写入SQL
### 扫描数据概况
以 scans_day(每天)、dealer_id、product_id 做唯一键。用于数据覆写
数据字段包括:
```
user_num,
grant_point_num,
grant_bonus_num,
grant_gift_number,
is_many_awards_num,
no_many_awards_num,
scans_day,
awards_day,
scans_outday,
awards_outday,
s.province_id
s.city_id
s.district_id
dealer_id、
product_id
```
## 数量扫描量
```
select count(*) as k,day, product_id, dealer_id from ydd_fw_scans group by day, product_id, dealer_id having k > 100 order by k desc limit 10;
```
```
+-------+----------+------------+-----------+
| k | day | product_id | dealer_id |
+-------+----------+------------+-----------+
| 28856 | 20240212 | 195853 | 0 |
| 27812 | 20240212 | 0 | 0 |
| 27127 | 20240211 | 0 | 0 |
| 26914 | 20240211 | 195853 | 0 |
| 26675 | 20240215 | 195853 | 0 |
| 25655 | 20240213 | 195853 | 0 |
| 23430 | 20240214 | 195853 | 0 |
| 22789 | 20240213 | 0 | 0 |
| 22514 | 20240209 | 195853 | 0 |
| 21629 | 20240209 | 0 | 0 |
+-------+----------+------------+-----------+
```
```
select count(*) from (select day, product_id, dealer_id, count(distinct(member_id)) as user_num, count(if(is_first=1,1,0)) as first_scan_num from ydd_fw_scans where day = 20240212 group by day, product_id, dealer_id) as a;