zoukankan      html  css  js  c++  java
  • 【原创】MySQL同时取出最大值和最小值所在整行

    老规矩;直接上群友问题

    这是原求助者的答案

     下面放我的方法

    为了说明这个问题:

    先创建一张表,并插入数据

    CREATE TABLE overtime (
        employee_name VARCHAR(50) NOT NULL,
        department VARCHAR(50) NOT NULL,
        hours INT NOT NULL,
        PRIMARY KEY (employee_name , department)
    );
    
    INSERT INTO overtime(employee_name, department, hours)
    VALUES('Diane Murphy','Accounting',37),
    ('Mary Patterson','Accounting',74),
    ('Jeff Firrelli','Accounting',40),
    ('William Patterson','Finance',58),
    ('Gerard Bondur','Finance',47),
    ('Anthony Bow','Finance',66),
    ('Leslie Jennings','IT',90),
    ('Leslie Thompson','IT',88),
    ('Julie Firrelli','Sales',81),
    ('Steve Patterson','Sales',29),
    ('Foon Yue Tseng','Sales',65),
    ('George Vanauf','Marketing',89),
    ('Loui Bondur','Marketing',49),
    ('Gerard Hernandez','Marketing',66),
    ('Pamela Castillo','SCM',96),
    ('Larry Bott','SCM',100),
    ('Barry Jones','SCM',65); 

    **Schema (MySQL v8.0)**

     | employee_name | department | hours |

    | ----------------- | ---------- | ----- |
    | Anthony Bow | Finance | 66 |
    | Barry Jones | SCM | 65 |
    | Diane Murphy | Accounting | 37 |
    | Foon Yue Tseng | Sales | 65 |
    | George Vanauf | Marketing | 89 |
    | Gerard Bondur | Finance | 47 |
    | Gerard Hernandez | Marketing | 66 |
    | Jeff Firrelli | Accounting | 40 |
    | Julie Firrelli | Sales | 81 |
    | Larry Bott | SCM | 100 |
    | Leslie Jennings | IT | 90 |
    | Leslie Thompson | IT | 88 |
    | Loui Bondur | Marketing | 49 |
    | Mary Patterson | Accounting | 74 |
    | Pamela Castillo | SCM | 96 |
    | Steve Patterson | Sales | 29 |
    | William Patterson | Finance | 58 |

    ---

    下面是查询sql

    with t1 as (SELECT
        1 as id,
        hours min_hours,
        FIRST_VALUE(employee_name) OVER (
            ORDER BY hours
        ) least_emp
        
    FROM
        overtime 
        limit 1),
        
    t2 as (SELECT
        1 as id,
        hours max_hours,
        FIRST_VALUE(employee_name) OVER (
            ORDER BY hours desc
        ) first_emp
        
    FROM
        overtime 
        limit 1)
    select t1.min_hours,t1.least_emp,t2.max_hours,t2.first_emp
    from t1 ,t2
    where t1.id=t2.id    
        
        ; 

    具体代码查看本人创建的

    https://www.db-fiddle.com/f/91TmSU9SwVwFBqCTHPPyh8/1

    https://www.db-fiddle.com/f/91TmSU9SwVwFBqCTHPPyh8/18 

     函数介绍

     

    【特别说明】转载请注明,原创不易,请勿用作商业用途。

    ------------------------------- ********厚德达理,励志勤工******** -------------------------------
  • 相关阅读:
    mybatis-config.xml
    log4j.properties
    applicationContext.xml
    对象mapper
    面向接口,定义接口
    Java实现Excel导入数据库,数据库中的数据导入到Excel。。转载
    判断浏览器类型
    大小写字母,数字,下划线,中横线和中文 正则表达式
    Androidpn的使用[找不到org.androidpn.client.PersistentConnectionListener]
    沙扬娜拉---徐志摩
  • 原文地址:https://www.cnblogs.com/hightech/p/14843431.html
Copyright © 2011-2022 走看看