问题1
对于ID_CAR,如何从表中获得最低值(非null)?例如,对于ID_CAR 1,最小值为50;对于ID_CAR 2,最小值为50;对于ID_CAR 3,最小值为300。我不需要重复,一辆车只需要一个值。
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)。
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发布于 2018-06-15 21:23:10
您需要UNION:
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;https://stackoverflow.com/questions/50876464
复制相似问题