-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement a SQL view to make it easier to query files in a nested folder #11
Labels
enhancement
New feature or request
Comments
Here's a query that returns all notes in folder 1, including notes in descendant folders: with recursive nested_folders(folder_id, descendant_folder_id) as (
-- base case: select all immediate children of the root folder
select id, id from folders where parent is null
union all
-- recursive case: select all children of the previous level of nested folders
select nf.folder_id, f.id from nested_folders nf
join folders f on nf.descendant_folder_id = f.parent
)
-- Find notes within all descendants of folder 1
select *
from notes
where folder in (
select descendant_folder_id from nested_folders where folder_id = 1
); With assistance from ChatGPT. Prompts were:
Then I tweaked it a bit, then ran this:
|
I improved the readability by removing some unnecessary table aliases: with recursive nested_folders(folder_id, descendant_folder_id) as (
-- base case: select all immediate children of the root folder
select id, id from folders where parent is null
union all
-- recursive case: select all children of the previous level of nested folders
select nested_folders.folder_id, folders.id from nested_folders
join folders on nested_folders.descendant_folder_id = folders.parent
)
-- Find notes within all descendants of folder 1
select *
from notes
where folder in (
select descendant_folder_id from nested_folders where folder_id = 1
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Working with nested data in SQL is tricky, can I make it easier with a view or canned query?
The text was updated successfully, but these errors were encountered: