How to group a Dataview query by certain inlinks and not others?

What I’m trying to do

I am building a Vault for my TTRPG campaign and I got a lot of scenes as individual notes. If a scene is played in a particular session, I will from the session’s note to the scene’s note E.g. We play scene “talk to the shopkeeper” in session 5, then the note on session 5 will contain a link to that scene.

So far, so good. In order to use Dataview to see the relations between these (and a bunch of other notes on factions, NPCs, etc.), I opted not to use metadata fields for each kind of relation between notes (e.g. having a field NPCs in a scene note that I have to manually fill with all the NPCs occuring in that scene). Instead, I am filtering by the note’s inlinks and outlinks and that works fine for almost all my needs.

I would like to generate a Dataview table where all my scene notes are listed and grouped by the sessions they linked to. Each scene note is linked to at maximum one session note.

Things I have tried

To this end, I have constructed the query below.

TABLE string( AS Scenes
FROM "Scenes"
WHERE contains(, "Session")
GROUP BY string(file.inlinks) AS Sessions

It takes in all files in the “Scenes” folder whose inlinks come from a note with “Session” in the name. This way, I only get scene notes that have been linked to from a session note, i.e. that have been played out already.

I have them grouped by their inlinks and this is my trouble. This way, all inlinks are used for grouping. However, I would like to only use the session notes that link to the scene notes for the grouping.

Currently, I get a result like this one below, in which the “Meeting a Caravan” scene is grouped by both the “Session 2” note and the “Arrival on Radanaar” note which both link to it. The scenes for “Session 1” are grouped correctly because no links in other notes than the “Session 1” note point to them.

I have read the documentation of the GROUP BY data command and looked for solutions both on search engines and in this forum, but I have not found a specific enough solution for my case.

I’d be glad if someone could help me out here. Also, if you have any input how to do this whole setup differently so it works better, I’d also be happy to hear that. :slight_smile:

I’m not quite sure that I follow your logic on which files links to where, but I might be able to give you a hint on how to proceed still:

TABLE as Scenes
FROM "Scenes"
FLATTEN file.inlinks as inlink
WHERE contains(, "Session")
GROUP BY inlink

What this query does is that it flattens, or splits up, all the inlinks into separate entities, and then instead of checking against the list of inlinks whether it contains “Session”, it only checks a single inlink if it contains “Session”.

In theory this should therefore only report back the inlinks related to scenes and session.

Disclaimer: Don’t have test files set up like your setup, so the code is untested, but hopefully it should get you going towards your goal.

1 Like

Thank you very much for the quick reply! This works for me.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.