如何计算具有最小值的列的日期差异
How to calculate difference in dates for column with smallest value
我有一个 table 包含 3 列日期:CREATED、CLOSED 和 EXPIRED。
我需要计算 CLOSED 或 EXPIRED 与 CREATED 之间的日期差异。
我需要 select CLOSED 和 EXPIRED 之间的较小值,然后计算与 CREATED 的差值。
如果他们有相同的日期,我需要 select 过期。
在上面的例子中:
For account_id =1, the difference in date should be: EXPIRED - CREATED.
For account_id = 2, the difference in date should be EXPIRED - CREATED.
For account_id = 3, the difference in date should be CLOSED - EXPIRED.
有没有办法做到selectCLOSED和EXPIRED之间的较小值,并据此计算与CREATED的日期差异?
LEAST 或 GREATEST 是您在值之间选择 min/max 的方式。
SELECT
created, expired, closed,
LEAST(expired, closed) as min_exp_clo,
DATEDIFF('days', created, min_exp_clo) as date_diff_days
FROM table
并且有一个额外的专栏免费版本,只是混在一起:
SELECT
created, expired, closed,
DATEDIFF('days', created, LEAST(expired, closed)) as date_diff_days
FROM table
我有一个 table 包含 3 列日期:CREATED、CLOSED 和 EXPIRED。 我需要计算 CLOSED 或 EXPIRED 与 CREATED 之间的日期差异。 我需要 select CLOSED 和 EXPIRED 之间的较小值,然后计算与 CREATED 的差值。 如果他们有相同的日期,我需要 select 过期。
在上面的例子中:
For account_id =1, the difference in date should be: EXPIRED - CREATED.
For account_id = 2, the difference in date should be EXPIRED - CREATED.
For account_id = 3, the difference in date should be CLOSED - EXPIRED.
有没有办法做到selectCLOSED和EXPIRED之间的较小值,并据此计算与CREATED的日期差异?
LEAST 或 GREATEST 是您在值之间选择 min/max 的方式。
SELECT
created, expired, closed,
LEAST(expired, closed) as min_exp_clo,
DATEDIFF('days', created, min_exp_clo) as date_diff_days
FROM table
并且有一个额外的专栏免费版本,只是混在一起:
SELECT
created, expired, closed,
DATEDIFF('days', created, LEAST(expired, closed)) as date_diff_days
FROM table