0

Hello ALL,

I have a table contains words extracted from a few documents.

DocID         Word     Tag
1                Book     noun
1                Pen       noun
1                Pencil    noun

2                There     prep
2                Hand     noun
2                Have     verb

3                Oil         noun
3                Thorough    adv
3                Drive      verb

4                Red        adj
4                Blue        adj
4                Pike        adj

I would like to select those (noun or verb) records in the documents which contains more than or equal to two nouns or verbs. Actually I want the following result

DocID         Word     Tag
1                Book     noun
1                Pen       noun
1                Pencil    noun

2                Hand     noun
2                Have     verb

3                Oil         noun
3                Drive      verb

How to do this with a SQL query. Thanks

2
Contributors
1
Reply
2
Views
9 Years
Discussion Span
Last Post by blocblue
0

I'm not sure if you've solved your problem yet... but please find below something I came up with...

Note: I've assumed you have a document table, and a documentTag table.

SELECT * 
FROM `document` AS `t1`
JOIN (
[INDENT]SELECT COUNT(*) AS `tagsMatched`, `DocId`
FROM (
[INDENT]SELECT `docId` FROM `docTags` WHERE `Tag` IN ('noun', 'verb')[/INDENT]
) WHERE `tagsMatched` >= '2' GROUP BY `docId`
[/INDENT]) AS `t2`
ON `t1`.`id` = `t2`.`DocId`

Let me know if this works :)

R.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.