美文网首页
打卡第1天 -- 3S2A1P : 三道sql,2道算法,1道简

打卡第1天 -- 3S2A1P : 三道sql,2道算法,1道简

作者: CnLearn | 来源:发表于2020-09-07 23:53 被阅读0次

打卡第1天 -- 3S2A1P : 三道sql,2道算法,1道简答

sql-1:

https://www.nowcoder.com/practice/4a052e3e1df5435880d4353eb18a91c6?tpId=82&&tqId=29764&rp=1&ru=/ta/sql&qru=/ta/sql/question-ranking

获取所有部门中当前(dept_emp.to_date = '9999-01-01')员工当前(salaries.to_date='9999-01-01')薪水最高的相关信息,给出dept_no, emp_no以及其对应的salary,按照部门升序排列。

拆分

​ 所有部门:需要按部门分组;

​ 薪水最高:获取最大值

​ 员工信息:【这是让整个题目难度提升的关键内容】

-- 方法1,通过开窗函数
-- 思路:
-- 分组相关: partition by, group by
-- 最大值相关: max(),   order by xx desc 通过排序
-- 表1, salaries 获取员工薪水信息
(select * from salaries where to_date='9999-01-01') s
-- 表2, dept_empo 获取部门员工信息
(select * from dept_emp where to_date='9999-01-01') d
-- 信息联合
(s inner join d on s.empo_no=d.empo_no) t
-- 分组排序查询,并按薪水降序
(select t.dept_no,t.empo_no,t.salary,
 row_mumber() over(partition by t.dept_no order by t.salary desc) as rn from t) r
-- 最终的查询结果
select r.dept_no,r.empo_no,r.salary from r where r.rn=1
-- 方法2:关联子查询, 外表主查询固定一个部分,内表子查询查询最大薪水
-- 主查询,查询全部数据
select d1.dept_no,d1.emp_no,s1.salary 
from dept_emp as d1 inner join salaries as s1
on d1.emp_no=s1.emp_no and d1.to_date='9999-01-01' and s1.to_date='9999-01-01'
where s1.salary in(子查询) order by d1.dept_no
-- 子查询,将主查询的部分类别,传入子查询
select max(s2.salary)
from dept_emp as d2 inner join salaries as s2
on d2.emp_no=s2.emp_no and d2.to_date='9999-01-01' and s2.to_date='9999-01-01'
    and d2.edpt_no=d1.dept_no --最关键的一步,将主查询的结果传入子查询

sql-2:

https://www.nowcoder.com/practice/72ca694734294dc78f513e147da7821e?tpId=82&&tqId=29765&rp=1&ru=/ta/sql&qru=/ta/sql/question-ranking

从titles表获取按照title进行分组,每组个数大于等于2,给出title以及对应的数目t。

拆分

​ 分组:group by ,partition by

​ 统计数目:count()

-- 方法1
select title,count(*) from titles group by title;
-- 方法2:纯属无聊,练习开窗函数
select distinct title,count() over(partition by title) from titles
-- 注意 title 前加个distinct,否则会查询重复的title

sql-3

https://www.nowcoder.com/practice/c59b452f420c47f48d9c86d69efdff20?tpId=82&&tqId=29766&rp=1&ru=/ta/sql&qru=/ta/sql/question-ranking

从titles表获取按照title进行分组,每组个数大于等于2,给出title以及对应的数目t。
注意对于重复的emp_no进行忽略(即emp_no重复的title不计算,title对应的数目t不增加)

拆分

​ 分组:group by, partition by

​ 去重: distinct, group by

-- 方法1
select title,count(distinct emp_no) as c from titles group by title;
-- 方法2:纯属无聊,练习开窗
select title,max(r) 
from
( 
    select title,dense_rank() over(partition by title order by emp_no) as r 
    from titles
) group by title

algorithm-1

leetcode 50

实现 pow(x, n),即计算 x 的 n 次幂函数

// 方法1 快速幂+递归
public class Main{
    public static void main(String[] args){
        // 测试案例
    }
    public static double myPow(double x,int n){
        long N=n; 
        if(n>0){
            return quick(x,N);
        }else{
            return 1.0/quick(x,-N);
        }
    }
    public static double quick(double x,long n){
        // 使用快速幂+递归的方式
        if(n==0){
            return 1.0;
        }
        double res=quick(x,n>>1);
        if((n&1)==0){
            // n 为偶数
            return res*res;
        }else{
            // n 奇数
            return res*res*x;
        }
    }
}
// 方法2:快速幂+迭代
public class Main{
    public static void main(String[] args){
        
    }
    public static double myPow(double x,int n){
        long N=n; 
        if(n>0){
            return quick(x,N);
        }else{
            return 1.0/quick(x,-N);
        }
    }
    public static double quick(double x,long N){
        double res=1.0;
        double _x_base=x;
        while(N>0){
            if((N&1)==1){
                // 如果是奇数
                res*=_x_base;
            }
            _x_base*=_x_base;
            N>>=1;
        }
        return res;
    }
}

Algorithm-2

leetcode 9

判断一个整数是否是回文数。回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。

// 方法1:转成字符串,双指针比较 (比较简单,不考虑)
// 方法2:利用整数的特点
public class Main{
    public static void main(String[] args){
        
    }
    public static boolean isPalindrome(int x){
        if(x<0||(x%10==0&&x!=0)){
            return false;
        }
        int _right=0;
        while(x>_right){
            _right = _right*10+x%10;
            x/=10;
        }
        return x==_right||_right/10==x;
    }
}

Problem-1

Hive与Hbase的异同点
  1. hive 将结构化的数据映射为一张表,并提供简单的sql查询功能。将sql语句转为MapReduce任务运行。

  2. hbase 是一个分布式、面向列的开源数据库

  3. 默认情况下两者的存储层都是HDFS

  4. hive 是用来对大数据的分析查询,不能进行实时查询;不应对其数据进行修改或插入; 需要数据库如mysql 存储元数据

  5. hbase是一种在Hadoop之上的NoSQL 的Key/vale数据库。适合用来进行大数据的实时查询;需要zookeeper 进行分布式协调

相关文章

网友评论

      本文标题:打卡第1天 -- 3S2A1P : 三道sql,2道算法,1道简

      本文链接:https://www.haomeiwen.com/subject/zairektx.html