【问题标题】:Left join with a select dependent on rows value左连接,选择取决于行值
【发布时间】:2013-10-23 14:36:48
【问题描述】:

我有一张桌子,tableA

Foo  bar  matcher
a    b    456
c    d    123
e    f    789
…

我还有另一张非常大的桌子,tableB

Count matcher
1     123
2     456
2     123
...

我想从 tableB 中找到特定匹配器的详细信息

  count   matcher
  1       123
  2       123

但我只想使用最大计数的行

  count   matcher
  2       123

然后我希望将表 B 左连接到表 A 上

  foo   bar   matcher   count
  a     b     456       10
  c     d     123       2
  e     f     789       5
  ...

我该怎么做?

【问题讨论】:

  • 这是哪个数据库和版本?
  • 我正在使用 Oracle Database 11g 版本 11.2.0.1.0 - 64 位生产

标签: sql


【解决方案1】:
select a.Foo, a.bar, b.matcher, max(b.Count) Count
from tableB b
left join tableA a
on a.matcher = b.matcher
group by a.Foo, a.bar, b.matcher

【讨论】:

    猜你喜欢
    • 2021-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多