首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何从表中获取最低值

如何从表中获取最低值
EN

Stack Overflow用户
提问于 2018-06-15 21:18:41
回答 5查看 143关注 0票数 2

问题1

对于ID_CAR,如何从表中获得最低值(非null)?例如,对于ID_CAR 1,最小值为50;对于ID_CAR 2,最小值为50;对于ID_CAR 3,最小值为300。我不需要重复,一辆车只需要一个值。

代码语言:javascript
运行
复制
ID_CAR | col_1 | col_2 | col_3 | col_4 | col_5 | col_6 

1      | null  | 250   | 300   | null  | 900   | null
2      | 100   | null  | 300   | 600   | 200   | 100
1      | 300   | 100   | 800   | 100   | 50    | 900
3      | 300   | 4000  | null  | null  | null  | null
2      | null  | null  | null  | 50    | null  | 100
4      | 400   | 900   | 500   | 700   | 800   | 500

问题2在此示例中,col_*中的值是天。我需要将天数添加到col_date并得到最低天数。例如,ID_CAR 1的最低日期是2018-01-03 (col_2),ID_CAR 2的最低日期是2018-01-15 (col_4)。

代码语言:javascript
运行
复制
ID_CAR | col_1 | col_2 | col_3 | col_4 | col_5 | col_6 | col_date

1      | null  | 2     | 3     | null  | 5     | null  | 2018-01-01
2      | 1     | null  | 3     | 6     | 10    | 10    | 2018-01-13
1      | 3     | 20    | 80    | 10    | 50    | 90    | 2018-01-02
3      | 30    | 40    | null  | null  | null  | null  | 2018-01-03
2      | null  | null  | null  | 5     | null  | 10    | 2018-01-10
4      | 10    | 9     | 5     | 70    | 8     | 50    | 2018-01-07
EN

Stack Overflow用户

发布于 2018-06-15 21:23:10

您需要UNION

代码语言:javascript
运行
复制
select id_car, min(val) as lowest_value
from (select id_car, col_1 as Val
      from table union 
      select id_car, col_2
      from table
      . . .
     ) t 
group by id_car;
票数 2
EN
查看全部 5 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50876464

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档