I’m trying to create a dataview table that shows only my MOST RECENT review of any given restaurant (along with the date, etc). Earlier reviews would be excluded.
Date
Restaurant
Review
1/5/22
ABC
Loved this place
1/8/22
DEF
Ugh - wouldn’t eat here again
I’ve combed through the Dataview literature thinking that the answer was likely in a WHERE or FLATTEN argument, but haven’t had luck. I’m guessing there’s a simple answer but not sure ! Thanks…
Hi @Twita, could you please give a couple examples of what your source Markdown files look like? Are they organized by day, by restaurant, by something else? What does your current query look like?
@Twita I could not figure out how to do this in DQL (“regular dataview”) but if you instead of a dataview codeblock you use a dataviewjs one, then something like the following should work:
let finalTbl = [];
dv.pages("").where(p => p.Restaurant).groupBy(p => p.Restaurant).forEach(
group => finalTbl.push(group.rows.sort(note => note.file.mtime, 'desc').first())
); /* end forEach */
dv.table(
["Restaurant", "Rating", "Summary"],
dv.array(finalTbl).map(row => [row.restaurant, row.rating, row.summary])
);
You’ll have to enable Javascript in your Dataview Settings page if you haven’t before.
One note: the /* end forEach */ is a comment because I was having issues lining up parentheses. You can remove it without any effect on the code (though before you do, might as well double-check my parenthesis counting from the first use of forEach to the comment!).
I’d be happy to go through and explain any or all bits of this code later today, when I have a few more minutes. In the meantime, would love to see if any experts have a way to simplify it or to do this in regular DQL!
Thanks for posting your data. It’s a little hacky, but sorting by date, using GROUP BY, and then picking the first row might get you what you’re looking for. Here’s my query:
```dataview
TABLE
rows[0].Rating as Rating,
rows[0].Summary as Summary,
rows[0].file.day as Date
FROM "Scratch"
WHERE Restaurant
SORT file.day DESC
GROUP BY Restaurant
```
Kudos to all of you for taking time to help educate those of us who are still learning ! Obsidian gets more useful / indispensable for me with every Help post I read…
Do you have a way to make LIMIT 1 work per-group? (Or in this case per-restaurant?) When I use it I only get one answer back at all, not one per-restaurant. Thanks!