SQL复杂的连接

时间:2016-01-08 16:30:00

标签: sql database oracle

我需要加入以下2个表格,结果如下 这个数据基本上是2个表,它们通过某种类型的组从两个表上的多个键上连接起来,但是我正在绘制一个空白,如何实现这一点 我正在使用Oracle数据库。

感谢

<table border="0">
<tr><td><b>Table 1</b></td><td></td><td><b>Table 2</b></td></tr>
  <tr>
    <td>
      <table border="1">
  <tr>
    <th>Group</th>
    <th>Key</th>
  </tr>
  <tr>
    <td>A</td>
    <td>1</td>
  </tr>
  <tr>
    <td>A</td>
    <td>2</td>
  </tr>
  <tr>
    <td>B</td>
    <td>1</td>
  </tr>
  <tr>
    <td>B</td>
    <td>3</td>
  </tr>
  <tr>
    <td>C</td>
    <td>1</td>
  </tr>
  <tr>
    <td>C</td>
    <td>2</td>
  </tr>
  <tr>
    <td>C</td>
    <td>3</td>
  </tr>
  <tr>
    <td>D</td>
    <td>2</td>
  </tr>
  <tr>
    <td>D</td>
    <td>4</td>
  </tr>
</table>

    </td>
    <td>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    </td>
    <td>
            <table border="1">
  <tr>
    <th>Users</th>
    <th>Key</th>
  </tr>
  <tr>
    <td>U1</td>
    <td>1</td>
  </tr>
  <tr>
    <td>U2</td>
    <td>1</td>
  </tr>
  <tr>
    <td>U2</td>
    <td>3</td>
  </tr>
  <tr>
    <td>U3</td>
    <td>1</td>
  </tr>
  <tr>
    <td>U3</td>
    <td>2</td>
  </tr>
  <tr>
    <td>U3</td>
    <td>4</td>
  </tr>
  <tr>
    <td>U4</td>
    <td>1</td>
  </tr>
  <tr>
    <td>U4</td>
    <td>2</td>
  </tr>
  <tr>
    <td>U4</td>
    <td>3</td>
  </tr>
  <tr>
    <td>U4</td>
    <td>4</td>
  </tr>
  <tr>
    <td>U5</td>
    <td>1</td>
  </tr>
  <tr>
    <td>U5</td>
    <td>2</td>
  </tr>
  <tr>
    <td>U5</td>
    <td>4</td>
  </tr>
</table>
    </td>
  </tr>
  <tr>
    <td><b>RESULT</b></td>
    </tr>
  <tr>
    <td>
      <table border="1">
          <tr>
    <th>Users</th>
    <th>Groups</th>
  </tr>
        <tr><td>U1</td><td></td></tr>
        <tr><td>U2</td><td>B</td></tr>
        <tr><td>U3</td><td>A</td></tr>
        <tr><td>U4</td><td>A, B, C, D</td></tr>
        <tr><td>U5</td><td>A, D</td></tr>
        </table>
    </td>
  </tr>
</table>

1 个答案:

答案 0 :(得分:1)

这是一种方式:

with grps as (select 'A' grp, 1 key from dual union all
              select 'A' grp, 2 key from dual union all
              select 'B' grp, 1 key from dual union all
              select 'B' grp, 3 key from dual union all
              select 'C' grp, 1 key from dual union all
              select 'C' grp, 2 key from dual union all
              select 'C' grp, 3 key from dual union all
              select 'D' grp, 2 key from dual union all
              select 'D' grp, 4 key from dual),
     usrs as (select 'U1' usr, 1 key from dual union all
              select 'U2' usr, 1 key from dual union all
              select 'U2' usr, 3 key from dual union all
              select 'U3' usr, 1 key from dual union all
              select 'U3' usr, 2 key from dual union all
              select 'U4' usr, 1 key from dual union all
              select 'U4' usr, 2 key from dual union all
              select 'U4' usr, 3 key from dual union all
              select 'U4' usr, 4 key from dual union all
              select 'U5' usr, 1 key from dual union all
              select 'U5' usr, 2 key from dual union all
              select 'U5' usr, 4 key from dual)
-- end of mimicking your tables; see SQL below
select usr,
       listagg(case when usr_grp_cnt = grp_cnt then grp end, ', ') within group (order by grp) grps
from   (select u.usr,
               g.grp,
               g.grp_cnt,
               count(g.key) usr_grp_cnt
        from   usrs u
               left outer join (select grp,
                                       key,
                                       count(*) over (partition by grp) grp_cnt
                                from   grps) g on (g.key = u.key)
        group by u.usr,
                 g.grp,
                 g.grp_cnt)
group by usr;

USR GRPS           
--- ---------------
U1                 
U2  B              
U3  A              
U4  A, B, C, D     
U5  A, D

这首先找到每个组的键数(假设它是唯一的),然后它对usrs“table”进行外连接(这里,它是WITH子句中定义的子查询; google如果您不熟悉子查询因子。您可能有一个与该子查询等效的表,所以您只需在主sql语句中使用它。)

接下来,我们通过检查用户+组密钥计数与组密钥计数是否相同来找到每个用户每组的密钥数,然后我们可以使用它计算出哪些组匹配。然后我们使用listagg函数将其聚合到一个列表中。

N.B。我不得不更改用户U3的数据,因为最初,您的数据具有与用户U5相同的密钥。