H2 SQL 在范围内找不到查询列

H2 SQL Query column not found in scope

关于:SQL: Find unavailability per minute of a ressource in an appointment

((点击上方 link 获取架构 + 信息))

我正在尝试在 H2 SQL 数据库中 运行 此查询。我对 H2 语法有点陌生。我注意到导致问题的 WHERE 子句中的 num 列。

错误:

Column "NUM" not found; SQL statement: CREATE FORCE VIEW ( SELECT "A"."APPOINTMENT_ID", "A"."APPOINTMENT_START_TIME", "A"."APPOINTMENT_END_TIME", "C"."COMPONENT_HOST_NAME", 'unavailable' AS "STATE" FROM "PUBLIC"."APPOINTMENT" "A" LEFT OUTER JOIN "PUBLIC"."APPOINTMENT_COMPONENT" "AC" ON "A"."APPOINTMENT_ID" = "AC"."APPOINTMENT_ID" INNER JOIN "PUBLIC"."COMPONENT" "C" ON "C"."COMPONENT_ID" = "AC"."COMPONENT_ID" WHERE ((CONVERT("A"."APPOINTMENT_START_TIME", TIME) <= DATEADD('minute', "NUM", CAST('00:00:00' AS TIME))) AND (CONVERT("A"."APPOINTMENT_END_TIME", TIME) >= DATEADD('minute', "NUM", CAST('00:00:00' AS TIME)))) AND ("C"."COMPONENT_ID" IN(1)) FETCH FIRST ROW ONLY ) AS SELECT "A"."APPOINTMENT_ID", "A"."APPOINTMENT_START_TIME", "A"."APPOINTMENT_END_TIME", "C"."COMPONENT_HOST_NAME", 'unavailable' AS "STATE" FROM "PUBLIC"."APPOINTMENT" "A" LEFT OUTER JOIN "PUBLIC"."APPOINTMENT_COMPONENT" "AC" ON "A"."APPOINTMENT_ID" = "AC"."APPOINTMENT_ID" INNER JOIN "PUBLIC"."COMPONENT" "C" ON "C"."COMPONENT_ID" = "AC"."COMPONENT_ID" WHERE ((CONVERT("A"."APPOINTMENT_START_TIME", TIME) <= DATEADD('minute', "NUM", CAST('00:00:00' AS TIME))) AND (CONVERT("A"."APPOINTMENT_END_TIME", TIME) >= DATEADD('minute', "NUM", CAST('00:00:00' AS TIME)))) AND ("C"."COMPONENT_ID" IN(1)) FETCH FIRST ROW ONLY [42122-200] 42S22/42122 (Help)


我的代码:

with times(num) as 
(
  select 30 as num
  union all select (num + 30)
  from times where num < (24*60)
)
select dateadd('minute', num, cast('00:00:00' as time)) as datetimeinterval, unavailabilities.state from times
outer join(
select top 1 a.appointment_id, a.appointment_start_time, a.appointment_end_time, c.component_host_name, 'unavailable' as state
    from  appointment a 
    left join appointment_component ac on a.appointment_id = ac.appointment_id
    inner join component c on c.component_id = ac.component_id
    where
            dateadd('minute', -->num<--, cast('00:00:00' as time)) between convert(a.appointment_start_time, time) and convert(a.appointment_end_time, time)
    and 
        c.component_id in (1)
) unavailabilities 

TLDR:尝试按分钟或按一定范围(此处为 30 分钟)获取组件列表的不可用性。在这种情况下,Num 应该 return 30 的倍数,具体取决于选择的时间范围,它将检查组件是否被采用。

N.B。我从上面的 link

更改了 machine=component 和 appmach=appointment_component(交叉 table)

我不确定语法。我对H2不是很熟悉。但是 num 不应该用在 on 子句中而不是子查询吗?请检查:

with recursive times(num) as 
(
  select 30 as num
  union all select (num + 30)
  from times where num < (24*60)
)
select dateadd('minute', num, cast('00:00:00' as time)) as datetimeinterval, unavailabilities.state from times
outer join(
select top 1 a.appointment_id, a.appointment_start_time, a.appointment_end_time, c.component_host_name, 'unavailable' as state
    from  appointment a 
    left join appointment_component ac on a.appointment_id = ac.appointment_id
    inner join component c on c.component_id = ac.component_id
    where
            dateadd('minute', -->num<--, cast('00:00:00' as time)) between convert(a.appointment_start_time, time) and convert(a.appointment_end_time, time)
    and 
        c.component_id in (1)


   ) unavailabilities 
on dateadd('minute', num, cast('00:00:00' as time)) between convert(appointment_start_time, time) and convert(appointment_end_time, time)