如何在Oracle SQL中再次使用MAX和MIN函数获取值?

时间:2018-12-03 07:41:51

标签: sql oracle oracle10g

通过此SQL查询,我得到了以下结果:

SELECT Code,
 CASE
      WHEN MAX(running) = MIN(running) THEN MAX(running)
      ELSE MAX(running) - MIN(running)
 END
"RUNNING",
 date
FROM table
GROUP BY date;

结果:

Code    Running     Date
CC4       123       1/1/2018
CC4       12        1/11/2018
CC4       12000     4/2/2018
CC4       1000      4/10/2018

用户选择从日期(1/1/2018)到日期(4/10/2018)

“代码”列中的代码相同。我想在上面的查询中再次使用“最大和最小函数”,以仅实现“代码”列的一个结果

如何在oracle SQL中实现此目标?

1 个答案:

答案 0 :(得分:0)

I think you just want to GROUP BY code:

SELECT Code,
       (CASE WHEN MAX(running) = MIN(running) THEN MAX(running)
             ELSE MAX(running) - MIN(running)
        END) as "RUNNING"
FROM table
GROUP BY code;