【问题标题】:H2 SQL Query column not found in scope范围内未找到 H2 SQL 查询列
【发布时间】:2021-08-24 20:42:20
【问题描述】:

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

((点击以上链接查看架构+信息))

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

错误:

未找到列“NUM”; SQL 语句: 创建力视图( 选择 "A"."APPOINTMENT_ID", "A"."APPOINTMENT_START_TIME", "A"."APPOINTMENT_END_TIME", "C"."COMPONENT_HOST_NAME", “不可用”作为“状态” 来自“公众”。“任命”“A” 左外连接“公共”。“APPOINTMENT_COMPONENT”“AC” ON "A"."APPOINTMENT_ID" = "AC"."APPOINTMENT_ID" 内连接“公共”。“组件”“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 ("C"."COMPONENT_ID" IN(1)) 仅获取第一行 ) 作为 选择 "A"."APPOINTMENT_ID", "A"."APPOINTMENT_START_TIME", "A"."APPOINTMENT_END_TIME", "C"."COMPONENT_HOST_NAME", “不可用”作为“状态” 来自“公众”。“任命”“A” 左外连接“公共”。“APPOINTMENT_COMPONENT”“AC” ON "A"."APPOINTMENT_ID" = "AC"."APPOINTMENT_ID" 内连接“公共”。“组件”“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 ("C"."COMPONENT_ID" IN(1)) 仅获取第一行 [42122-200] 42S22/42122(帮助)


我的代码:

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 应该返回 30 的倍数,具体取决于选择的时间范围,它将检查组件是否被使用。

注意我从上面的链接更改了 machine=component 和 appmach=appointment_component(交叉表)

【问题讨论】:

    标签: sql h2


    【解决方案1】:

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

    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)
    

    【讨论】:

    • 感谢您的回答。仍然得到同样的错误。我将在帖子中更新完整的错误
    • 我没有 H2 数据库。如果您知道任何 H2 的在线编译器,请告诉我?
    • 我发现的最接近的事情是:jooq.org/translate 需要选择 H2,但它不能作为编译器工作。我用的是安装好的在线界面,没找到在线界面
    • 我想你解决了我的问题!!!!谢谢!我只是将外连接更改为左外连接并删除了我的 where 子句。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-23
    • 1970-01-01
    相关资源
    最近更新 更多