如何从表中获取最小值

8ulbf1ek  于 2021-06-20  发布在  Mysql
关注(0)|答案(5)|浏览(366)

问题1
如何从表(非空)中为id\u car获取最小值?例如,对于id车1,最小值为50;对于id车2,最小值为50;对于id车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\ u日期和得到最低。例如,id车1的最低日期为2018-01-03(第2列),id车2的最低日期为2018-01-15(第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
vuktfyat

vuktfyat1#

如果期望值大于999999999999999999,请尝试此操作,然后使用更高的值

select id_car,
min(least(coalesce(col_1,9999999999999999999),coalesce(col_2,9999999999999999999),coalesce(col_3,9999999999999999999),
          coalesce(col_4,9999999999999999999),coalesce(col_5,9999999999999999999),coalesce(col_6,9999999999999999999)
        )
    ) as min_val 
from your_table
group by id_car
gfttwv5a

gfttwv5a2#

下面的查询将为您提供所需的结果

select tab.ID_CAR, min(tab.val) as lowest_value from
(
      (select ID_CAR,min(col_1) val
      from table
      group by ID_CAR)
  union
      (select ID_CAR,min(col_2) val
      from table
      group by ID_CAR)
  union
      (select ID_CAR,min(col_3) val
      from table
      group by ID_CAR)
  union
      (select ID_CAR,min(col_4) val
      from table
      group by ID_CAR)
  union
      (select ID_CAR,min(col_5) val
      from table
      group by ID_CAR)
  union
      (select ID_CAR,min(col_6) val
      from table
      group by ID_CAR)
) tab
group by tab.ID_CAR
btqmn9zl

btqmn9zl3#

你需要 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;
x6492ojm

x6492ojm4#

没有 union 您可以简单地组合最小值和最小值函数:

select
  ID_CAR,min(least(col_1,col_2,col_3,col_4,col_5,col_6)) lowest_value
from
  table
group by
  ID_CAR

或者如果你有 null 你需要的价值观 ifnull 或者 coalesce 功能

select
  ID_CAR,
  min(least(
    ifnull(col_1,~0),
    ifnull(col_2,~0),
    ifnull(col_3,~0),
    ifnull(col_4,~0),
    ifnull(col_5,~0),
    ifnull(col_6,~0)
  )) as lowest_value
from
  table
group by
  ID_CAR
``` `~0` 是mysql中的最大bigint
相反的功能 `least` 是 `greatest` 相反的功能 `min` 是 `max` ;-)
与mysql、oracle、postgres、hive一起使用。。。
问题2,类似这样:

select
ID_CAR,
min(least(
DATE_ADD(col_date, INTERVAL ifnull(col_1,0) DAY),
DATE_ADD(col_date, INTERVAL ifnull(col_2,0) DAY),
DATE_ADD(col_date, INTERVAL ifnull(col_3,0) DAY),
DATE_ADD(col_date, INTERVAL ifnull(col_4,0) DAY),
DATE_ADD(col_date, INTERVAL ifnull(col_5,0) DAY),
DATE_ADD(col_date, INTERVAL ifnull(col_6,0) DAY)
)) as lowest_date
from
table
group by
ID_CAR

或类似于此(除非所有列都可以为null):

select
ID_CAR,
DATE_ADD(col_date, INTERVAL min(least(
ifnull(col_1,~0),
ifnull(col_2,~0),
ifnull(col_3,~0),
ifnull(col_4,~0),
ifnull(col_5,~0),
ifnull(col_6,~0)
)) DAY) as lowest_date
from
table
group by
ID_CAR

o7jaxewo

o7jaxewo5#

最简单的方法是使用最少的:

SELECT ID_CAR, least(t.col_1, t.col_2, t.col_3, t.col_4, t.col_5, t.col_6)
FROM
(SELECT ID_CAR, min(col_1) as col_1, min(col_2) as col_2, min(col_3) as col_3, min(col_4) as col_4, min(col_5) as col_5, min(col_6) as col_6
FROM YOUR_TABLE GROUP BY ID_CAR) t;

但是:如果least的任何参数为null,它将返回null。您要么需要将空值转换为一个高值(这是一个hack,但在实践中会起作用,请参阅其他答案)。
也就是说做这样的事:

SELECT ID_CAR, LEAST(col_1, col_2, col_3,
                     col_4, col_5, col_6) as l
FROM
(SELECT ID_CAR, 
   IFNULL(min(col_1), 9999)  as col_1, 
   IFNULL(min(col_2), 9999)  as col_2,
   IFNULL(min(col_3), 9999)  as col_3,
   IFNULL(min(col_4), 9999)  as col_4,
   IFNULL(min(col_5), 9999)  as col_5,
   IFNULL(min(col_6), 9999)  as col_6
FROM YOUR_TABLE GROUP BY ID_CAR) t;

但是,最好使用技巧将表转换为以下形式的三行表:

car_id | attr | value
     1      1    NULL   ; or use strings such as "size"
     1      2     250

相关问题