我具有以下oracle语法,该语法使我可以在IN语句(Source)中使用两列。不幸的是,该语句与MSSQL不兼容:
SELECT s.period, s.year, s.amount
FROM salaries s
where (s.year, s.period) in (select year, period from periods)
我通过串联两个列找到了解决方案。但是,我想知道是否没有更专业的解决方案?
SELECT s.period, s.year, s.amount
FROM salaries s
where (s.year + ' ' + s.period) in (select year + ' ' + period from periods)
答案 0 :(得分:3)
您可以为此使用WHERE EXISTS。
SELECT s.period, s.year, s.amount
FROM salaries s
where exists (
SELECT *
from periods
where year = s.year and period = s.period
)
答案 1 :(得分:2)
在MS SQL中,可以使用where存在方法,这种方法效率更高。
SELECT
s.period
, s.year
, s.amount
FROM salaries s
where exists(
select year, period from periods p where s.year = p.year and s.period = p.period
)
答案 2 :(得分:1)
下面的代码将使用并行处理来消除[periods]表中不存在的记录:
SELECT DISTINCT s.[period]
,s.[year]
,s.amount
FROM salaries s
CROSS APPLY
(SELECT *
FROM [periods]
WHERE [year] = s.[year]
AND [period] = s.[period]
)Res1