• mysql函数


    目录

    聚合函数

    group_concat()

    数学函数

    字符串函数

    日期函数

    控制流函数

    case_when函数

    窗口函数

    序号函数

    开窗聚合函数-sum,avg,min,max

    分布函数-cume_dist()和persent_rank

    前后函数-lag和lead

    头尾函数-first_value和last_value

    其他函数-nth_value(expr,n)、ntile(n)


    聚合函数

    group_concat()

    1. -- 将所有员工的名字合并成一行
    2. select group_concat(emp_name) from emp;
    3. -- 指定分隔符合并
    4. select department,group_concat(emp_name separator ';' ) from emp group by department;
    5. -- 指定排序方式和分隔符
    6. select department,group_concat(emp_name order by salary desc separator ';' ) from emp group by department;

    数学函数

    字符串函数

    日期函数

    控制流函数

    case_when函数

    1. -- 方式1
    2. select
    3. * ,
    4. case
    5. when payType=1 then '微信支付'
    6. when payType=2 then '支付宝支付'
    7. when payType=3 then '银行卡支付'
    8. else '其他支付方式'
    9. end as payTypeStr
    10. from orders;
    11. -- 方式2
    12. select
    13. * ,
    14. case payType
    15. when 1 then '微信支付'
    16. when 2 then '支付宝支付'
    17. when 3 then '银行卡支付'
    18. else '其他支付方式'
    19. end as payTypeStr
    20. from orders;

    窗口函数

    序号函数

    1. -- 对每个部门的员工按照薪资排序,并给出排名
    2. select
    3. dname,
    4. ename,
    5. salary,
    6. row_number() over(partition by dname order by salary desc) as rn
    7. from employee;
    8. -- 对每个部门的员工按照薪资排序,并给出排名 rank
    9. select
    10. dname,
    11. ename,
    12. salary,
    13. rank() over(partition by dname order by salary desc) as rn
    14. from employee;
    15. -- 对每个部门的员工按照薪资排序,并给出排名 dense-rank
    16. select
    17. dname,
    18. ename,
    19. salary,
    20. dense_rank() over(partition by dname order by salary desc) as rn
    21. from employee;
    22. --求出每个部门薪资排在前三名的员工- 分组求TOPN
    23. select
    24. *
    25. from
    26. (
    27. select
    28. dname,
    29. ename,
    30. salary,
    31. dense_rank() over(partition by dname order by salary desc) as rn
    32. from employee
    33. )t
    34. where t.rn <= 3
    35. -- 对所有员工进行全局排序(不分组)
    36. -- 不加partition by表示全局排序
    37. select
    38. dname,
    39. ename,
    40. salary,
    41. dense_rank() over( order by salary desc) as rn
    42. from employee;

    开窗聚合函数-sum,avg,min,max

    1. select
    2. dname,
    3. ename,
    4. salary,
    5. sum(salary) over(partition by dname order by hiredate) as pv1
    6. from employee;
    7.  
    8. select cookieid,createtime,pv,
    9. sum(pv) over(partition by cookieid) as pv3
    10. from itcast_t1; -- 如果没有order by排序语句 默认把分组内的所有数据进行sum操作
    11. select
    12. dname,
    13. ename,
    14. salary,
    15. sum(salary) over(partition by dname order by hiredate rows between unbounded preceding and current row) as c1
    16. from employee;
    17.  
    18. select
    19. dname,
    20. ename,
    21. salary,
    22. sum(salary) over(partition by dname order by hiredate rows between 3 preceding and current row) as c1
    23. from employee;
    24. select
    25. dname,
    26. ename,
    27. salary,
    28. sum(salary) over(partition by dname order by hiredate rows between 3 preceding and 1 following) as c1
    29. from employee;
    30. select
    31. dname,
    32. ename,
    33. salary,
    34. sum(salary) over(partition by dname order by hiredate rows between current row and unbounded following) as c1
    35. from employee;

    分布函数-cume_dist()和persent_rank

    1. /*
    2. rn1: 没有partition,所有数据均为1组,总行数为12,
    3. 第一行:小于等于3000的行数为3,因此,3/12=0.25
    4. 第二行:小于等于4000的行数为5,因此,5/12=0.4166666666666667
    5. rn2: 按照部门分组,dname='研发部'的行数为6,
    6. 第一行:研发部小于等于3000的行数为1,因此,1/6=0.16666666666666666
    7. */
    8. select
    9. dname,
    10. ename,
    11. salary,
    12. cume_dist() over(order by salary) as rn1, -- 没有partition语句 所有的数据位于一组
    13. cume_dist() over(partition by dept order by salary) as rn2
    14. from employee;
    15. /*
    16. rn2:
    17. 第一行: (1 - 1) / (6 - 1) = 0
    18. 第二行: (1 - 1) / (6 - 1) = 0
    19. 第三行: (3 - 1) / (6 - 1) = 0.4
    20. */
    21. select
    22. dname,
    23. ename,
    24. salary,
    25. rank() over(partition by dname order by salary desc ) as rn,
    26. percent_rank() over(partition by dname order by salary desc ) as rn2
    27. from employee;

    前后函数-lag和lead

    1. -- lag的用法
    2. /*
    3. last_1_time: 指定了往上第1行的值,default为'2000-01-01'
    4. 第一行,往上1行为null,因此取默认值 '2000-01-01'
    5. 第二行,往上1行值为第一行值,2021-11-01
    6. 第三行,往上1行值为第二行值,2021-11-02
    7. last_2_time: 指定了往上第2行的值,为指定默认值
    8. 第一行,往上2行为null
    9. 第二行,往上2行为null
    10. 第四行,往上2行为第二行值,2021-11-01
    11. 第七行,往上2行为第五行值,2021-11-02
    12. */
    13. select
    14. dname,
    15. ename,
    16. hiredate,
    17. salary,
    18. lag(hiredate,1,'2000-01-01') over(partition by dname order by hiredate) as last_1_time,
    19. lag(hiredate,2) over(partition by dname order by hiredate) as last_2_time
    20. from employee;
    21. -- lead的用法
    22. select
    23. dname,
    24. ename,
    25. hiredate,
    26. salary,
    27. lead(hiredate,1,'2000-01-01') over(partition by dname order by hiredate) as last_1_time,
    28. lead(hiredate,2) over(partition by dname order by hiredate) as last_2_time
    29. from employee;

    头尾函数-first_value和last_value

    1. -- 注意, 如果不指定ORDER BY,则进行排序混乱,会出现错误的结果
    2. select
    3. dname,
    4. ename,
    5. hiredate,
    6. salary,
    7. first_value(salary) over(partition by dname order by hiredate) as first,
    8. last_value(salary) over(partition by dname order by hiredate) as last
    9. from employee;

    其他函数-nth_value(expr,n)、ntile(n)

    1. -- 查询每个部门截止目前薪资排在第二和第三的员工信息
    2. select
    3. dname,
    4. ename,
    5. hiredate,
    6. salary,
    7. nth_value(salary,2) over(partition by dname order by hiredate) as second_score,
    8. nth_value(salary,3) over(partition by dname order by hiredate) as third_score
    9. from employee
    10. -- 根据入职日期将每个部门的员工分成3组
    11. select
    12. dname,
    13. ename,
    14. hiredate,
    15. salary,
    16. ntile(3) over(partition by dname order by hiredate ) as rn
    17. from employee;
    18. -- 取出每个部门的第一组员工
    19. select
    20. *
    21. from
    22. (
    23. SELECT
    24. dname,
    25. ename,
    26. hiredate,
    27. salary,
    28. NTILE(3) OVER(PARTITION BY dname ORDER BY hiredate ) AS rn
    29. FROM employee
    30. )t
    31. where t.rn = 1;

  • 相关阅读:
    【区块链 | Solidity】您应该查看的 18 个 Solidity GitHub 存储库-智能合约
    【VS Code】使用 VS Code 登陆远程服务器上的 Docker 容器
    【幂等幂等幂等,重要的知识说三遍!】常见的九种解决方案汇总
    Golang【Web 入门】 08 集成 Gorilla Mux
    富士康推进印度制造的计划倍速,中国制造iPhone占比下滑较快
    go sync.Map Range 的同时进行 Store,Range 的遍历结果如何?(源码分析)
    vue3+vite如何兼容低版本的白屏问题(安卓7/ios11)
    vscode中Chinese (Simplified)汉化无效解决方法
    webpack的插件webpack-dev-server
    VBA技术资料MF53:VBA_突出显示大于某值的单元格
  • 原文地址:https://blog.csdn.net/liyunyang2000/article/details/125473024