【问题标题】:join and count occurrence加入并计算出现次数
【发布时间】:2018-07-19 12:12:14
【问题描述】:

我是 SQL 新手,我有两个这样的表

标签1:

+---+---+
| ID|som|
+---+---+
|  e|  1|
|  d|  j|
|  c|  1|
|  b|  1|
|  a|  p|
+---+---+

tab2:

+------+---+
|SK_CUR|som|
+------+---+
|     b|  d|
|     a|  c|
|     a|  i|
+------+---+

我只想计算两个表中字母的出现次数。所以输出应该是这样的:

+------+---+
|    ID| oc|
+------+---+
|     a|  3|
|     b|  2|
|     c|  1|
|     d|  1|
|     e|  1|
+------+---+

【问题讨论】:

    标签: sql join count


    【解决方案1】:

    我想你只想要union allgroup by

    select id, count(*)
    from ((select id from t1) union all
          (select SK_CUR from t2)
         ) t
    group by id;
    

    【讨论】:

      【解决方案2】:

      试试下面的查询,

      select ID , count(*) from (
      select ID from tab1 where like '%[a-z]%'
      union all
      select som as ID from tab1 where som like '%[a-z]%'
      union all
      select SK_CUR as ID from tab2 where SK_CUR like '%[a-z]%'
      union all
      select som as ID from tab2 where som like '%[a-z]%')
      group by ID
      

      由于您只需要两个表的所有列中的字母,因此我使用了过滤器 比如'%[a-z]%'

      【讨论】:

        猜你喜欢
        • 2013-10-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-10-22
        • 1970-01-01
        • 2011-11-15
        相关资源
        最近更新 更多