Sql server 如何将Oracle多参数子查询转换为MS SQL子查询

Sql server 如何将Oracle多参数子查询转换为MS SQL子查询,sql-server,oracle,Sql Server,Oracle,我们正在将一些旧的Oracle内容转换为MS SQL Server,Oracle有多参数子查询,我正在尝试找出如何在MS SQL Server中实现这一点。我对Oracle语法不是很熟悉,我很难理解如何转换它 原始where条款的相关部分为: and (rate.tax_code, rate.effect_date) in (select tax_code, max(effect_date) from v_txtaxrate where

我们正在将一些旧的Oracle内容转换为MS SQL Server,Oracle有多参数子查询,我正在尝试找出如何在MS SQL Server中实现这一点。我对Oracle语法不是很熟悉,我很难理解如何转换它

原始where条款的相关部分为:

and (rate.tax_code, rate.effect_date) in 
        (select tax_code, max(effect_date)
         from   v_txtaxrate
         where  effect_date <= '10-JAN-14'
         group by tax_code)   
我尝试将其分为两个子查询:

and rate.tax_code in 
    (select tax_code
         from   v_txtaxrate
         where  effect_date <= '10-JAN-14'
         group by tax_code)
and rate.effect_date in 
    (select max(effect_date)
         from   v_txtaxrate
         where  effect_date <= '10-JAN-14'
         group by tax_code);

但结果表明,这两者并不等同

我将使用EXISTS而不是IN:

and exists (
  select 1 
  from (select tax_code, max(effect_date) effect_date
         from   v_txtaxrate
         where  effect_date <= '10-JAN-14'
         group by tax_code) a
  where rate.tax_code = a.tax_code
    and rate.effect_date = a.effect_date
)
试试这个

and exists
(
    select tax_code, effect_date
    from
    (
        select tax_code, max(effect_date) as effect_date
        from   v_txtaxrate
        where  effect_date <= '10-JAN-14'
        group by tax_code
    ) t
    where tax_code = rate.tax_code
    and effect_date =  rate.effect_date
)

oracle代码要求[rate].[tax_code]行的[effect_date]与视图中最近的行相同

对吗

因此,到派生表的内部联接可以简单地解决这个问题。派生表获取每个税码的最近日期

-- TSQL SNIPPET
select 
  *
from 
  rate inner join 
  (
    select tax_code, max(effect_date) as max_effect_date
    from  v_txtaxrate
    where  effect_date <= '20140110'
    group by tax_code
  )  d_tax
on
  rate.tax_code = d_tax.tax_code and
  rate.effect_date = d_tax.max_effect_date
因为我没有要测试的模式,所以我在代码和生效日期将[rate]表内部连接到派生的[d_tax]tax表。在此联接中不匹配的任何记录都将被删除

请参阅我关于派生表的博客文章

此外,请使用通用的日期文字YYYYMMDD’就是这样一种格式。请参阅MSDN上的矩阵,其中显示了最常用的格式


和你一起去,因为你是第一个回答的人,这很有效,对我来说很有意义。现在我真正理解了查询的作用。谢谢大家的回答!感谢您提供替代解决方案和更多详细信息。给了你一张赞成票,但是给了第一个答案,这也起了作用。我只是把日期放在那里测试。它实际上是代码中的一个参数。
-- TSQL SNIPPET
select 
  *
from 
  rate inner join 
  (
    select tax_code, max(effect_date) as max_effect_date
    from  v_txtaxrate
    where  effect_date <= '20140110'
    group by tax_code
  )  d_tax
on
  rate.tax_code = d_tax.tax_code and
  rate.effect_date = d_tax.max_effect_date