在sql中插入带Max的列

时间:2015-11-27 20:14:38

标签: sql oracle greatest-n-per-group

我不知道如何使用max。

插入列
Select id,MAX(salary),Min(Salary)
from C 
GROUP BY id;

它给我的全部id是最大的  而且我想要最高和最低工资的身份证!!

3 个答案:

答案 0 :(得分:3)

您只需要对表格进行一次扫描即可获得几个选项:

SQL Fiddle

Oracle 11g R2架构设置

CREATE TABLE C ( ID, SALARY ) AS
          SELECT 1, 100 FROM DUAL
UNION ALL SELECT 2, 110 FROM DUAL
UNION ALL SELECT 3, 100 FROM DUAL
UNION ALL SELECT 4, 110 FROM DUAL
UNION ALL SELECT 5,  90 FROM DUAL

查询1 - 获取单个ID

SELECT *
FROM (
   SELECT ID, SALARY
   FROM   c
   ORDER BY SALARY DESC
)
WHERE ROWNUM = 1

<强> Results

| ID | SALARY |
|----|--------|
|  2 |    110 |

查询2 - 获取单个ID(将获得最小和最大ID的替代方法)

SELECT MAX( ID ) KEEP ( DENSE_RANK LAST ORDER BY SALARY ) AS MAX_SALARY_ID,
       MAX( SALARY ) AS MAX_SALARY,
       MIN( ID ) KEEP ( DENSE_RANK FIRST ORDER BY SALARY ) AS MIN_SALARY_ID,
       MIN( SALARY ) AS MIN_SALARY
FROM   C

<强> Results

| MAX_SALARY_ID | MAX_SALARY | MIN_SALARY_ID | MIN_SALARY |
|---------------|------------|---------------|------------|
|             4 |        110 |             5 |         90 |

查询3 - 获取所有具有最高薪水的ID

SELECT ID, SALARY
FROM   (
  SELECT ID,
         SALARY,
         RANK() OVER ( ORDER BY SALARY DESC ) AS RNK
  FROM   C
)
WHERE  RNK = 1

<强> Results

| ID | SALARY |
|----|--------|
|  2 |    110 |
|  4 |    110 |

查询4 - 获取最低和最高工资的所有ID

SELECT LISTAGG( CASE MIN_RANK WHEN 1 THEN ID END, ',' ) WITHIN GROUP ( ORDER BY ID ) AS MIN_SALARY_IDS,
       MAX( CASE MIN_RANK WHEN 1 THEN SALARY END ) AS MIN_SALARY,
       LISTAGG( CASE MAX_RANK WHEN 1 THEN ID END, ',' ) WITHIN GROUP ( ORDER BY ID ) AS MAX_SALARY_IDS,
       MAX( CASE MAX_RANK WHEN 1 THEN SALARY END ) AS MAX_SALARY
FROM (
  SELECT ID,
         SALARY,
         RANK() OVER ( ORDER BY SALARY  ASC ) AS MIN_RANK,
         RANK() OVER ( ORDER BY SALARY DESC ) AS MAX_RANK
  FROM   C
)

<强> Results

| MIN_SALARY_IDS | MIN_SALARY | MAX_SALARY_IDS | MAX_SALARY |
|----------------|------------|----------------|------------|
|              5 |         90 |            2,4 |        110 |

查询5

SELECT ID,
       SALARY,
       CASE WHEN MIN_RANK = 1 THEN 'MIN'
            WHEN MAX_RANK = 1 THEN 'MAX' END AS MIN_MAX  
FROM (
  SELECT ID,
         SALARY,
         RANK() OVER ( ORDER BY SALARY  ASC ) AS MIN_RANK,
         RANK() OVER ( ORDER BY SALARY DESC ) AS MAX_RANK
  FROM   C
)
WHERE MIN_RANK = 1 OR MAX_RANK = 1

<强> Results

| ID | SALARY | MIN_MAX |
|----|--------|---------|
|  2 |    110 |     MAX |
|  4 |    110 |     MAX |
|  5 |     90 |     MIN |

答案 1 :(得分:0)

Select id,
salary 
from C 
where salary = (select MAX(salary) 
                  from C)

答案 2 :(得分:0)

您可以使用first_valuelast_value

  

FIRST_VALUE分析函数类似于FIRST分析   函数,允许您从有序集中返回第一个结果。

https://oracle-base.com/articles/misc/first-value-and-last-value-analytic-functions

create table C (id int, salary int);
insert into c values(1, 1);
insert into c values(2, 2);
insert into c values(3, 3);
insert into c values(4, 4);
insert into c values(5, 5);

Select distinct first_value(id) over ( order by salary desc)
from C 

    FIRST_VALUE(ID)OVER(ORDERBYSAL
1   5