小明需要处理某支付app会在客户端打印的日志。其中客户端有表user_client_log(trace_id,uid,logtime,step,product_id,pay_method)分别代表trace_id订单号,uid(用户id)、logtime(客户端事件发生时间)、step(客户端步骤)、product_id(商品id)、pay_method(支付方式), 例如:完整的下单流程是select选择支付方式、order下单、start开始支付、failed支付失败、end支付结束)。 其中有product_info(product_id,price,type,product_name)分别代表product_id商品id、price商品价格、type商品类型、product_name商品名称(表中数据已去重)。 【问题】需要小明展示商品名称为'anta'的商品各个支付渠道的分布,以支付渠道的次数的逆序排列返回。 【知识点】:关联查询、条件查询、聚合查询、窗口查询、排序函数、判断函数 【示例结果】 返回2列:销售渠道和销售数量 【示例解析】 根据商品名称筛选出来记录并根据商品名称的销售渠道分组聚合统计销售数量,最后排序返回结果 注: 只有select步骤的数据有pay_method字段; 如果select中pay_method为''则以error标记pay_method为脏数据; 无需考虑返回为空的异常处理
示例1

输入

DROP TABLE IF EXISTS `user_client_log`;
CREATE TABLE IF NOT EXISTS `user_client_log`(
    trace_id varchar(255) NOT NULL,
    uid varchar(32) DEFAULT NULL,
    logtime varchar(32) DEFAULT NULL,
    step varchar(32) DEFAULT NULL,
    product_id varchar(32) DEFAULT NULL,
    pay_method varchar(32) DEFAULT NULL,
    UNIQUE uni_trace(trace_id,step)
);
DROP TABLE IF EXISTS `product_info`;
CREATE TABLE IF NOT EXISTS `product_info`(
    product_id varchar(32) NOT NULL,
    price int(32) DEFAULT 0,
    type varchar(32) NOT NULL,
    product_name varchar(255) NOT NULL,
    UNIQUE uni_product(product_id)
);
INSERT IGNORE `user_client_log` VALUES 
    ('0001','user_0001','2022-01-01 00:00:00','select','p100','wx'),
    ('0001','user_0001','2022-01-01 00:00:04','order','p100',''),
    ('0001','user_0001','2022-01-01 00:00:04','start','p100',''),
    ('0001','user_0001','2022-01-01 00:00:08','end','p100',''),
    ('0002','user_0002','2022-01-01 10:00:00','select','p599','alipay'),
    ('0002','user_0002','2022-01-01 10:00:00','order','p599',''),
    ('0002','user_0002','2022-01-01 10:00:01','start','p599',''),
    ('0002','user_0002','2022-01-01 10:00:10','failed','p599',''),
    ('0003','user_0003','2022-01-01 10:00:00','select','p599','alipay'),
    ('0003','user_0003','2022-01-01 10:00:00','order','p599',''),
    ('0004','user_0004','2022-01-01 10:00:00','select','p100','wx'),
    ('0004','user_0004','2022-01-01 10:00:00','order','p100',''),
    ('0004','user_0004','2022-01-01 10:00:01','start','p100',''),
    ('0005','user_0005','2022-01-01 10:00:00','select','p100','meituan'),
    ('0005','user_0005','2022-01-01 10:00:00','order','p100',''),
    ('0005','user_0005','2022-01-01 10:00:01','start','p100',''),
    ('0006','user_0006','2022-01-01 10:00:00','select','p599','alipay'),
    ('0006','user_0006','2022-01-01 10:00:00','order','p599',''),
    ('0006','user_0006','2022-01-01 10:00:01','start','p599','');
INSERT IGNORE `product_info` VALUES 
    ('p100',100,'shoes','anta'),
    ('p599',5000,'telephone','huawei'),
    ('p233',2,'water','wahaha');

输出

wx|2
meituan|1
加载中...