Question

I am getting data from a third party in form of below tables in MS Access 2000 file format

  • Papers and
  • PaperTags

Below is the sample data from these tables.

Papers table and sample data

+----+----------+
| ID |  PaperID |
+----+----------+
| 1  |  658     |
| 2  |  659     |
| 3  |  660     |
| 4  |  661     |
| 5  |  662     |
| 6  |  663     |
| 7  |  664     |
+----+----------+

PaperTags table and sample data

+----+----------+----------------------------------------+
| ID |  PaperID |                   TagID                |
+----+----------+----------------------------------------+
| 1  |  663     |   3                                    |
| 2  |  663     |   15 --Y                               |
| 3  |  663     |   17                                   |
| 4  |  663     |   18 --Y                               |
| 5  |  664     |   14                                   |
| 62 |  658     |   9                                    |
| 63 |  658     |   14                                   |
| 64 |  658     |   17                                   |
| 65 |  659     |   15 --Y                               |
| 66 |  659     |   17                                   |
| 67 |  659     |   18 --Y                               |
| 68 |  660     |   17                                   |
| 69 |  660     |   18 --N as it has only 18 and not 15  |
| 70 |  661     |   10                                   |
| 71 |  661     |   17                                   |
| 72 |  661     |   18 --N as it has only 18 and not 15  |
| 73 |  662     |   18 --N as it has only 18 and not 15  |
| 74 |  662     |   14                                   |
| 75 |  662     |   17                                   |
| 76 |  662     |   18 --N as it has only 18 and not 15  |
+----+----------+----------------------------------------+

Now my end user will pass one or more TagIDs for example 15 and 18 my goal is to find all the PaperIDs which have all of these TagIDs. In these example I need to return 663 and 659

I have tried below query but if there is any glitch in data then it doesn't work. For example PaperID 662 appears twice in the table with the same TagID so count(PaperID) = 2 turns out to be true and it will end up in my result.

select Count(PaperID), PaperID from PaperTags
group by TagID, PaperID
having TagID = 15 or TagID = 18
and count(PaperID) = 2

The other query that I tried is

select * from Papers
where Papers.PaperID
in 
(
select PaperTags.PaperID from PaperTags
where (PaperTags.Tagid = 15 or PaperTags.Tagid = 18)
and PaperTags.PaperID = Papers.PaperID 
)

I have gone thru below article but as I am using MSAccess I can't use this approach.

Select records from a table where all other records with same foreign key have a certain value

I think there has to be better way to filter. Any help is much appreciated.

Was it helpful?

Solution

Something like this:

SELECT G.ContentID
FROM (
SELECT PT.ContentID, PT.TagID
FROM PaperTags AS PT
WHERE PT.TagID IN (15, 18)
GROUP BY PT.ContentID, PT.TagID
) AS G
GROUP BY G.ContentId
HAVING Count(*) = 2
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top