I need to filter on a table based on the existence of a value in a foreign table. For example, suppose we have two tables: posts and tags
posts:
post_id | text
---------+-------------
1 | Please help
2 | New idea
tags:
tag_id | tag
--------+---------
1 | bug
2 | urgent
3 | feature
posts_tags:
post_id | tag_id
---------+--------
1 | 1
1 | 2
2 | 3
I’d like to query all posts based on associated tags (e.g., "All posts tagged as urgent, all posts tagged as feature or bug, etc.).
As far as I know, there’s no way to natively query posts and filter on tag_id or tag. I’m perfectly happy to denormalize or pre-aggregate my data, but the unique values of tags are not known ahead of time so I can’t easily create a column for each possible tag (or create a base question per this suggestion ).
I’m using PostgreSQL as my datasource, so if Metabase supported querying on Postgres array values, hstores, or items in a json(b) array, I could aggregate the tags into a single column and then filter, e.g.:
post_id | text | tags
---------+-------------+-------------------
1 | Please help | ["bug", "urgent"]
2 | New idea | ["feature"]
I suppose I could pre-aggregate the values into a string and search that field:
post_id | text | tags
---------+-------------+------------
1 | Please help | bug;urgent
2 | New idea | feature
…but I’m wondering if this is the best solution.
This seems like it would be a somewhat common use case, so I’m curious to know if anyone has a workaround.
Related github issues:
