| 4.1计算每月总收入 4.2计算每个季度的总收入 4.3按年计算总收入 4.4按工作日计算总收入 4.5按时间段计算总收入(需要清理数据) 4.6按时间段计算平均消费 4.4按工作日计算平均消费 4.8计算年、月、日的交易总数 4.9找出交易量最大的10个客户 4.10找出消费最多的前10位顾客 purchase_month, sum(price) as monthSUM
concat_ws('-',substr(purchase_date,1,4),cast(ceil(month(purchase_date)/3.0) as string))as year_quarter from transaction_details) select year_quarter, sum(price) as year_quarterSUM from base group by year_quarter; substr(purchase_date,1,4), group by substr(purchase_date,1,4); `dayofweek`(purchase_date), group by `dayofweek`(purchase_date) having `dayofweek`(purchase_date) in (2,3,4,5,6); select price,purchase_time, if(purchase_time like '%M',from_unixtime(unix_timestamp(purchase_time,'hh:mm aa'),'HH:mm'),purchase_time) as time_format from transaction_details), select price, purchase_time, time_format, (cast(split(time_format,':')[0] as decimal(4,2))+ cast(split(time_format,':')[1] as decimal(4,2))/60) as purchase_time_in_hr from base), t1 as (select price, purchase_time, time_format, purchase_time_in_hr, `if`(purchase_time_in_hr > 5 and purchase_time_in_hr <= 8, 'early morning', `if`(purchase_time_in_hr > 8 and purchase_time_in_hr <= 11, 'morning', `if`(purchase_time_in_hr > 11 and purchase_time_in_hr <= 13, 'noon', `if`(purchase_time_in_hr > 13 and purchase_time_in_hr <= 18, 'afternoon', `if`(purchase_time_in_hr > 18 and purchase_time_in_hr <= 22, 'evening', 'night'))))) as time_bucket select time_bucket, sum(price) bucketSUM, avg(price) avgprice from t1 group by time_bucket ;
 `dayofweek`(purchase_date), where `dayofweek`(purchase_date) in (2,3,4,5,6) group by `dayofweek`(purchase_date); year(purchase_date) currentYear, group by year(purchase_date); substring(purchase_date,1,7) currentMonth, group by substring(purchase_date,1,7); day(purchase_date) currentDay, group by day(purchase_date); select customer_id,count(transaction_id) as trans_cnt,sum(price) as customerSUM from transaction_details group by customer_id), select concat_ws(' ', cd.first_name, '***') as cust_name, base.* , dense_rank() over (order by trans_cnt desc ) rn from base join vw_customer_details cd on base.customer_id=cd.customer_id) select * from cust_detail where rn<=10; select customer_id,sum(price) as customerSUM from transaction_details group by customer_id), select concat_ws(' ', cd.first_name, '***') as cust_name, base.* , dense_rank() over (order by customerSUM desc ) rn from base join vw_customer_details cd on base.customer_id=cd.customer_id) select * from cust_detail where rn<=10;
  |
| Mysql中建表查询 7.11统计该期间交易数量最少的用户 7.12计算每个季度的独立客户总数 7.13计算每周的独立客户总数 7.14计算整个活动客户平均花费的最大值 7.15统计每月花费最多的客户 7.16统计每月访问次数最多的客户 7.17按总价找出最受欢迎的5种产品 7.18根据购买频率找出最畅销的5种产品 7.19根据客户数量找出最受欢迎的5种产品 t1 AS(SELECT customer_id,COUNT(1) c FROM transaction_details GROUP BY customer_id ORDER BY c), t2 AS(SELECT t1.*,DENSE_RANK() over(ORDER BY t1.c) as rn FROM t1) SELECT CONCAT(vcd.first_name,'***'), t2.* FROM t2 JOIN vw_customer_details vcd ON t2.customer_id=vcd.customer_id WHERE rn=1; SELECT DISTINCT COUNT(customer_id), concat_ws('-',substr(purchase_date,1,4),cast(ceil(month(purchase_date)/3.0) as CHAR(20)))as year_quarter ORDER BY (year_quarter+0); SELECT DISTINCT COUNT(customer_id), concat_ws('-',substr(purchase_date,1,4),cast(WEEKOFYEAR(purchase_date) as CHAR(20)))as week_year t1 AS(SELECT customer_id, avg(price) as costAVG FROM transaction_details GROUP BY customer_id), t2 AS(SELECT t1.*,DENSE_RANK() over(ORDER BY costAVG DESC) as rn FROM t1) SELECT * FROM t2 WHERE rn=1; t1 AS(SELECT purchase_month,customer_id, sum(price) as costSUM FROM transaction_details GROUP BY purchase_month,customer_id), t2 AS(SELECT t1.*,DENSE_RANK() over(PARTITION by purchase_month ORDER BY costSUM DESC) as rn FROM t1) SELECT CONCAT(vcd.first_name,'***'), t2.* FROM t2 JOIN vw_customer_details vcd ON t2.customer_id=vcd.customer_id WHERE rn=1; t1 AS(SELECT purchase_month,customer_id, count(customer_id) as costCount FROM transaction_details GROUP BY purchase_month,customer_id), t2 AS(SELECT t1.*,DENSE_RANK() over(PARTITION by purchase_month ORDER BY costCount DESC) as rn FROM t1) SELECT CONCAT(vcd.first_name,'***'), t2.* FROM t2 JOIN vw_customer_details vcd ON t2.customer_id=vcd.customer_id WHERE rn=1; t1 AS(SELECT product,sum(price) as proSUM FROM transaction_details GROUP BY product), t2 AS(SELECT t1.*,DENSE_RANK() over(ORDER BY proSUM DESC) as rn FROM t1) SELECT * FROM t2 WHERE rn BETWEEN 1 AND 5; t1 AS(SELECT product,COUNT(product) as proCount FROM transaction_details GROUP BY product), t2 AS(SELECT t1.*,DENSE_RANK() over(ORDER BY proCount DESC) as rn FROM t1) SELECT * FROM t2 WHERE rn BETWEEN 1 AND 5; t1 AS(SELECT product,COUNT(customer_id) as proCount FROM transaction_details GROUP BY product), t2 AS(SELECT t1.*,DENSE_RANK() over(ORDER BY proCount DESC) as rn FROM t1) SELECT * FROM t2 WHERE rn BETWEEN 1 AND 5;
 |