3

我有两张桌子。

表格1:

ID   SENTENCE
1    The shoes are good shoes.
2    There is a tree.
3    This is nice, nice, nice!

表2:

ID   WORD
1    The
1    shoes
1    are
1    good
1    shoes
2    There
2    is
2    a
2    tree
3    This
3    is
3    nice
3    nice
3    nice

我需要计算 Table1 中每个句子中每个单词的出现次数。如果任何单词出现多次(>1),则计算它,否则跳过它。最后生成的表应如下所示:

ID   SENTENCE                   CNT
1    The shoes are good shoes.  2
2    There is a tree.
3    This is nice, nice, nice!  3
4

4 回答 4

3

您可以使用count() over()

select distinct t1.id,
  t1.sentence,
  coalesce(t2.cnt, 0) cnt
from table1 t1
left join 
(
  select t1.id, 
    t1.sentence,
    t2.word,
    count(t2.word) over(partition by t1.id, t2.word) cnt
  from table1 t1
  left join table2 t2
    on t1.id = t2.id
) t2
  on t1.id = t2.id
  and t2.cnt > 1
order by t1.id

请参阅SQL Fiddle with Demo

或者您可以使用count()

select t1.id,
  t1.sentence,
  coalesce(t2.cnt, 0) cnt
from table1 t1
left join 
(
  select t1.id, 
    t1.sentence,
    t2.word,
    count(t2.word) cnt
  from table1 t1
  left join table2 t2
    on t1.id = t2.id
  group by t1.id, t1.sentence, t2.word
  having count(t2.word) > 1
) t2
  on t1.id = t2.id
order by t1.id 

请参阅带有演示的 SQL Fiddle

于 2013-02-27T11:09:48.847 回答
3

SQL 演示

select t1.id, t1.sentence, 
coalesce(t2.cnt,0) as counts
from table1 t1
left join
(select id, word, count(id) cnt
from table2
group by id, word
having count(id) > 1)t2
on t1.id = t2.id
order by t1.id
;

| ID |                  SENTENCE | COUNTS |
-------------------------------------------
|  1 | The shoes are good shoes. |      2 |
|  2 |          There is a tree. |      0 |
|  3 | This is nice, nice, nice! |      3 |
于 2013-02-27T11:19:30.807 回答
0
SELECT table1.id, table1.sentence, COUNT(word) as cnt FROM table2 JOIN table1 ON table1.id = table2.id GROUP BY table2.word HAVING COUNT(word) > 1

我的答案是 mysql,我现在正在验证它是否也适用于 sql

于 2013-02-27T11:02:32.500 回答
0

有很多连接示例,因此,我将仅添加字数示例:

Select REGEXP_COUNT('The shoes are good shoes.', ' ')+1 words_count
 From dual
/

WORDS_COUNT
-----------
5

SELECT id
     , LISTAGG(word, ' ') WITHIN GROUP (ORDER BY id, word) AS words
     , count(*) word_cnt
  FROM your_table2
GROUP BY id
/

ID    WORDS                    WORD_CNT
---------------------------------------
1    The are good shoes shoes    5
2    There a is tree             4
3    This is nice nice nice      5
于 2013-02-27T14:26:44.873 回答