【问题标题】:Alternative to cross apply (join on X in())替代交叉应用(加入 X in())
【发布时间】:2017-07-24 19:38:37
【问题描述】:

经过大量工作,我提出了这个(正确的)查询:

select count(distinct t1.code+t1.flname+t1.col1+t2.flname) 
from #t4 t1 cross apply (select flname from #t4 sq where sq.col1=t1.col1)t2 

经过大量工作后,我未能在不使用交叉应用的情况下生成等效查询。是否可以?也许与WITH?不使用 WITH 怎么样?

【问题讨论】:

  • 您的“正确”查询可能不正确。将 4 列连接在一起可能会产生冲突。
  • 由于我所知道的字段的性质,这不太可能。

标签: sql sql-server tsql subquery cross-apply


【解决方案1】:

一个简单的join 应该是等价的:

select count(distinct t1.code + t1.flname + t1.col1 + sq.flname) 
from #t4 t1 join
     #t4 sq
     on sq.col1 = t1.col1;

【讨论】:

  • 口头上我想要做的是:对于 #t4 的每条记录,给我一个与所有与该记录共享 col1 的 flnames 的叉积。因此,t1.col1=t2.col2 不应用于内部连接,而是作为返回一组行以进行交叉连接的一种方式。
猜你喜欢
  • 1970-01-01
  • 2014-11-19
  • 1970-01-01
  • 2017-07-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-11
  • 1970-01-01
相关资源
最近更新 更多