2016-12-14 52 views
1

Azure SQL数据仓库是否支持具有子查询结构的多列IN/NOT IN?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 ','. 

解决方法是使用内连接还是外连接将查询重写为派生表?

在单柱/ 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) 
; 

回答

3

SQL服务器从来没有支持这一(方便)语法。解决方法是使用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 
) 
; 

大卫