2016-12-14 53 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 
) 
; 

大衛