如何在不使用 join 或 cte 的情况下在同一查询中使用动态生成的列

How to use the dynamically generated column in the same query without using join or cte

我正在学习 SQL。我有一种情况,我必须使用我在 运行 时间创建的同一列。 table 是 patient,只有 2 列,分别是 dateOfCheckupduration 分钟。

+---------------+-----------------+
| dateOfCheckup |   duration      | 
+---------------+-----------------+
|   2020-05-28  |  30 min         | 
|   2020-05-29  |  30 min         |
|   2020-05-30  |  1 hour         | 
|   2020-06-03  |  1 hour 30 min  | 
|   2020-06-05  |  30 min         |
|   2020-07-21  |  1 hour         |
|   2020-07-22  |  1 hour 30 min  | 
|   2020-07-28  |  1 hour 30 min  | 
+---------------+-----------------+

现在我将在 运行 时间内再创建 1 个列,即 分钟 (这只是 持续时间 转换为总计分钟作为整数值)与此查询:

select dateOfCheckup, duration,
((case when duration like '% hour%' then substring_index(duration, ' hour', 1) * 60 else 0 end) + (case when duration like '%min%' then substring_index(substring_index(duration, ' min', 1), ' ', -1) + 0 else 0 end)) as minutes from patient;

此查询运行良好。查看结果:

+---------------+-----------------+-----------------+
| dateOfCheckup |  duration       |   minutes       |
+---------------+-----------------+-----------------+
|   2020-05-28  |  30 min         |   30            | 
|   2020-05-29  |  30 min         |   30            |
|   2020-05-30  |  1 hour         |   60            |
|   2020-06-03  |  1 hour 30 min  |   90            |
|   2020-06-05  |  30 min         |   30            |
|   2020-07-21  |  1 hour         |   60            |
|   2020-07-22  |  1 hour 30 min  |   90            |
|   2020-07-28  |  1 hour 30 min  |   90            |
+---------------+-----------------+-----------------+

我的问题是,如果我想在同一个查询中使用这个新创建的列 minutes 来执行其他任务,例如 group by、order by、sum、avg 等.我只是想问如果有这样的场景,那我们怎么实现呢。我尝试使用 sum(minutes)。错误是:

Unknown column 'minutes' in 'field list'

这是一个db<>fiddle。请帮助我。

您必须将当前查询视为子查询,然后对结果求和

SELECT SUM(minutes) AS total_time 
FROM (
    select dateOfCheckup, duration, 
    ((case when duration like '% hour%' then substring_index(duration, ' hour', 1) * 60 else 0 end) + 
    (case when duration like '%min%' then substring_index(substring_index(duration, ' min', 1), ' ', -1) + 0 else 0 end)
    ) as minutes 
from patient
) AS a;

不能在同一查询的另一个表达式中使用列别名。

在子查询中定义别名,以便您可以在外部查询中引用它们:

SELECT t.dateOfCheckup, SUM(t.minutes)
FROM (
    select dateOfCheckup, duration,
      (
       (case when duration like '% hour%' 
             then substring_index(duration, ' hour', 1) * 60 
             else 0 end) 
     + (case when duration like '%min%' 
             then substring_index(substring_index(duration, ' min', 1), ' ', -1) + 0 
             else 0 end)
      ) as minutes 
    from patient
) AS t
GROUP BY t.dateOfCheckup

我不知道您想执行什么具体操作,但是使用一级子查询,您可以以任何您想要的方式使用这个新的派生列,例如

select dateOfCheckup,duration,max(minutes) over() maxminutes
  from (select dateOfCheckup, duration, ((case when duration like '% hour%' then substring_index(duration, ' hour', 1) * 60 else 0 end) + (case when duration like '%min%' then substring_index(substring_index(duration, ' min', 1), ' ', -1) + 0 else 0 end)) as minutes 
          from patient
       ) p;