1

当我将遗留连接语法重构为 ANSI 连接语法时遇到问题,因为我们数据库中的大多数过程都使用遗留连接。这是我需要更改它以使用 SQL 'JOIN' 语法而不是普通的旧版连接的代码。有人可以建议我怎么做吗?

    select 
          a.userkey, 
          a.username,
          c.monthd ,
          b.currencykey 
     from 
         #users a,
         #invoicedata b,
          #revdate c
     where 
         (a.userkey >= b.userkey or a.userkey <= b.userkey) 
          and b.idate between c.startdate and c.enddate
      group by 
           a.userkey,
           a.username,
           c.monthd,
           b.currencykey

       order by c.id,a.username,b.currencykey
4

1 回答 1

3

这部分(a.userkey >= b.userkey or a.userkey <= b.userkey)似乎不正确,您不能order by列(至少没有agregate函数)不在group by.

所以,可能是这样的:

select 
      a.userkey, 
      a.username,
      c.monthd ,
      b.currencykey 
 from #users a
 inner join #invoicedata b on a.userkey = b.userkey
 inner join #revdate c on b.idate between c.startdate and c.enddate
 group by 
      a.userkey,
      a.username,
      c.monthd,
      b.currencykey

我也用内部连接替换了嵌套连接,但这不是必需的:)


另一方面,您可能需要交叉连接:

select 
      a.userkey, 
      a.username,
      c.monthd ,
      b.currencykey 
 from #users a
 cross join #invoicedata b
 inner join #revdate c on b.idate between c.startdate and c.enddate
 group by 
      a.userkey,
      a.username,
      c.monthd,
      b.currencykey
于 2012-06-15T09:19:59.557 回答