zoukankan      html  css  js  c++  java
  • mysql面试题一

    题目1: 有如下数据:
    A,2015-01,5
    A,2015-01,15
    B,2015-01,5
    A,2015-01,8
    B,2015-01,25
    A,2015-01,5
    C,2015-03,20
    
    字段说明:
    username: 姓名
    date : 日期
    cost_money : 消费金额
    需求:
    1.计算出每个人每月的消费金额?
    (1)Select username,sum(cost_money)
        From tab1
        Group by date,username
        Order by username
    2.计算出每个人截止到当月的消费总金额?
    (1)Select tmp.username,sum(tmp. su) over(partition by tmp.username order by tmp.date ) ‘合计’
        From  (
            Select username,sum(cost_money) su,date
            From tab1
            Group by date,username
                ) tmp
        Group by tmp.date,tmp.username,su
        Order by tmp.username;
    
    
    题目2:
    有50W个京东店铺,每个顾客访客访问任何一个店铺的任何一个商品时都会产生一条访问日志,访问日志存储的表名为Visit,访客的用户id为user_id,被访问的店铺名称为shop,请统计:
    1)每个店铺的UV(访客数)
    2)每个店铺访问次数top3的访客信息。输出店铺名称、访客id、访问次数
    
    
    create table Second_Visit (user_id string,shop string);
    
    insert into table second_visit values ('1','a');
    insert into table second_visit values ('1','b');
    insert into table second_visit values ('2','a');
    insert into table second_visit values ('3','c');
    insert into table second_visit values ('1','a');
    insert into table second_visit values ('1','a');
    1)Select  shop ,cout(1)
    From Second_Visit
    Group by shop;
    2)SELECT*
    from(
    Select tmp.shop,tmp.user_id,cu,row_number() over(partition by tmp.shop ORDER BY             tmp.cu  desc) rn
    From (select user_id,shop,count(1) cu
    From Second_Visit
    Group by user_id,shop) tmp
    )tmp1
    Where tmp1.rn < 4;
    
    
    题目3: 有一个5000万的用户文件(user_id,name,age),一个2亿记录的用户看电影的记录文件(user_id,url),根据年龄段观看电影的次数进行排序?
    
    1.建表
    create table forth_user(user_id string,name string,age int);
    create table forth_log(user_id string,url string);
    
    insert into table forth_user values('001','wt',10);
    insert into table forth_user values('002','ls',18);
    insert into table forth_user values('003','zz',30);
    insert into table forth_user values('004','zz',50);
    
    insert into table forth_log values('001','sdf');
    insert into table forth_log values('001','wss');
    insert into table forth_log values('002','sdf');
    insert into table forth_log values('003','sdf');
    insert into table forth_log values('004','sdf');
    2.分析需求
    先求出每个人看了几次电影,t1
    然后t1和user表join,拼接age字段 t2表
    划分年龄段,0-2020-4040-6060--
    按年龄段分组,按照次数排序
    Select user_id ,name,
    CASE
    WHEN age >=0 AND age <=20 THEN '10-20'
    WHEN age >=21 AND age <=40 THEN '21-30'
    WHEN age >=41 AND age <=60 THEN '41-50'
    WHEN age >=61 THEN '61+'
    END AS ageband
    From 
    (Select user_id id,name,count(1) ct
    From  forth_use fu
    Join  forth_log fl
    On fu.user_id = fl.user_id
    Group by fu.user_id) tab1
    Join forth_user tab2 
    on tab1.id=tab2.user_id
    Group by ageband
    Order by ct desc;
  • 相关阅读:
    JavaScript-Runoob-JS函数:JavaScript 函数调用
    JavaScript-Runoob-JS函数:JavaScript 函数参数
    JavaScript-Runoob-JS函数:JavaScript 函数定义
    JavaScript-Runoob:JavaScript 代码规范
    Serverless架构的前世今生
    【华为云技术分享】STM32L476移植华为LiteOS系列教程(二)---开发前准备
    8分钟为你详解React、Angular、Vue三大前端技术
    【华为云技术分享】云容器引擎 CCE权限管理实践
    【极客思考】设计模式:你确定你真的理解了单例模式吗?
    最终,我决定将代码迁出x86架构!
  • 原文地址:https://www.cnblogs.com/zhipeng-wang/p/14528224.html
Copyright © 2011-2022 走看看