根据一系列列返回最新日期

ki1q1bka  于 2021-08-01  发布在  Java
关注(0)|答案(2)|浏览(331)

我有一个查询,它统计已完成任务的数量并返回原始值。
我想添加一个新的列,返回最近的日期(在本例中 task_1_completed_date 或者 task_2_completed_date 但实际上有20个任务域)

(CASE WHEN task_1_completed_date IS NOT NULL THEN 1 ELSE 0 END +
        CASE WHEN task_2_completed_date IS NOT NULL THEN 1 ELSE 0 END
       ) AS task_completed_total 
from (select JSON_EXTRACT_SCALAR(data, '$.task1.date') as task_1_completed_date
             JSON_EXTRACT_SCALAR(data, '$.task2.date') as task_2_completed_date
      from table
      WHERE pet_store = 'london'
     )

不知道如何继续,我应该在这里使用子查询来排序任务完成日期吗?

cu6pst1q

cu6pst1q1#

使用订单依据

(CASE WHEN task_1_completed_date IS NOT 
  NULL THEN 1 ELSE 0 END +
    CASE WHEN task_2_completed_date IS NOT 
     NULL THEN 1 ELSE 0 END
   ) AS task_completed_total 
    from (select JSON_EXTRACT_SCALAR(data, 
      '$.task1.date') as task_1_completed_date
         JSON_EXTRACT_SCALAR(data, 
     '$.task2.date') as task_2_completed_date
   from table
  WHERE pet_store = 'london'
  )where rownum=1 order by 
  task_completed_total desc 
  -- if rownum doesn't work use Limit 1
7jmck4yq

7jmck4yq2#

我想你可以用groupby和max来获得最近的约会。请看这里:https://docs.microsoft.com/en-us/sql/t-sql/queries/select-group-by-transact-sql?view=sql-server-ver15

相关问题