我有两张桌子。
表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
答案 0 :(得分:2)
您可以使用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
或者您可以使用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
答案 1 :(得分:2)
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 |
答案 2 :(得分: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
答案 3 :(得分: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