【问题标题】:Multi-column IN / NOT IN subquery on Azure SQL data warehouseAzure SQL 数据仓库上的多列 IN / NOT IN 子查询
【发布时间】:2016-12-14 16:06:41
【问题描述】:

Azure SQL 数据仓库是否支持带有子查询构造的多列 IN/NOT IN?

当运行如下查询时:

select
   *
from 
  schema_name.calendar
where
    gregorian_date > '1998-01-01'
and gregorian_date < '1999-01-01'
and (yr_wk, day_of_wk) not in ( select yr_wk, day_of_wk from schema_name.calendar where week = 25 )
;

select
  *
from 
  schema_name.calendar
where
    gregorian_date > '1998-01-01'
and gregorian_date < '1999-01-01'
and (yr_wk, day_of_wk) in ( select yr_wk, day_of_wk from schema_name.calendar where week = 25 )

;

收到错误。

SQL Error [103010] [S0001]: Parse error at line: 7, column: 14: Incorrect syntax near ','.
com.microsoft.sqlserver.jdbc.SQLServerException: Parse error at line: 7, column: 14: Incorrect syntax near ','.

是否有将查询重写为具有内部或外部联接的派生表的解决方法?

带有 IN / NOT IN 子查询的单列确实有效:

select
   *
from 
  schema_name.calendar
where
    gregorian_date > '1998-01-01'
and gregorian_date < '1999-01-01'
and (yr_wk) not in ( select yr_wk from schema_name.calendar where week = 25 )
;

select
   *
from 
  schema_name.calendar
where
    gregorian_date > '1998-01-01'
and gregorian_date < '1999-01-01'
and (yr_wk) in ( select yr_wk from schema_name.calendar where week = 25 )
;

【问题讨论】:

    标签: azure-sqldw


    【解决方案1】:

    SQL Server 从来不支持这种(方便的)语法。解决方法是使用 EXISTS/NOT EXISTS 子查询。

    例如

       select   *
    from 
      schema_name.calendar c
    where
        gregorian_date > '1998-01-01'
    and gregorian_date < '1999-01-01'
    and not exists
    ( 
      select * 
      from schema_name.calendar 
      where week = 25 
      and yr_wk = c.yr_wk
      and day_of_wk = c.yr_wk
    )
    ;
    

    大卫

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-20
      • 1970-01-01
      • 1970-01-01
      • 2012-01-17
      • 1970-01-01
      相关资源
      最近更新 更多