zoukankan      html  css  js  c++  java
  • sql使用中的一些固定语法

    #sql求分组后 组内排名前几--每个uid在9月份登录的前七天,与后7天(分别计算,不足7天的取全部)

    SELECT * from ( SELECT  
     uid,
     day,
     @ROW := case when @cid=uid then @ROW+1 else 1 END rn, 
     @cid := uid
    from 
    (
      SELECT uid,day  from 
      ods_app_action_login_h 
      where day>="2019-09-01" and day <="2019-09-30" and uid <>''  
          GROUP BY day,uid ORDER BY uid,day desc
    ) l1,(SELECT @ROW := 0,@cid :=0) l2) l5
    where rn <=7
    
    --第二种
    SELECT uid,day 
    from ods_app_action_login_h as o1 
    where day>="2019-09-27" and day<="2019-09-30" and 
     (
        SELECT count(*) from 
        (
           SELECT uid,day from ods_app_action_login_h where 
          day>="2019-09-27" and day<="2019-09-30" GROUP BY uid,day
          ) as o 
        where  o1.uid= o.uid and o.day <=o1.day   --分组排序
     ) <= 7 

     #从上一结果中筛选出isguest<>1的uid(当天只要有一条数据isguest=1,则此uid视为isguest=1,前7天后7天可任选一个)

    SELECT * from 
    (
        SELECT 
         uid,
         day,
         @ROW := case when @cid=uid then @ROW+1 else 1 END rn,
         @cid := uid
        from 
        (
            SELECT uid,day  from 
            ods_app_action_login_h 
            where day>="2019-09-01" and day <="2019-09-30" and uid <>''  GROUP BY day,uid ORDER BY uid,day desc
        ) l1,(SELECT @ROW := 0,@cid :=0) l2
    ) l5
    INNER JOIN(
        SELECT uid,day from (        
           SELECT uid,sum(if(isguest!=1,0,1)) is_isguest,day from ods_app_action_login_h 
            where day>="2019-09-01" and day <="2019-09-30" GROUP BY uid,day
                ) s1 where s1.is_isguest=0
    ) l6 on l5.uid = l6.uid
    where rn <=7


    #下面效率不高

    -- SELECT d1.uid,d1.isguest,d1.day from ods_app_action_login_h as d1
    -- LEFT JOIN
    -- (
    -- SELECT * from (
    -- SELECT uid,isguest,day from
    -- ods_app_action_login_h
    -- where day>="2019-09-29" and day <="2019-09-30" and uid <>'' GROUP BY uid,isguest,day) as o3 WHERE isguest !=1
    -- ) l1 on l1.uid = d1.uid and l1.day =d1.day
    -- where d1.day>="2019-09-29" and d1.day <="2019-09-30" GROUP BY uid
    --

     
  • 相关阅读:
    css3 奇技淫巧
    html 转义处理
    从前端中的IOC理念理解koa中的app.use()
    树形操作2-或、且关系组合
    树形操作1-树形拖拽选择
    vscode 编辑器常用快捷键
    日期相关的一些简单计算:格式化,上个月,前一天
    spring整合Mybati时,只报空指针异常问题
    使用Mybatis Generator插件自动生成映射文件(cmd无法进入文件,dns服务器对区域没有权威等问题)遇到问题
    spring整合mybatis接口无法注入问题
  • 原文地址:https://www.cnblogs.com/HugJun/p/11731385.html
Copyright © 2011-2022 走看看