Tasks in Dataview: Sorting groups and tasks independently

Given notes Project A and Project B which both contain these lines:

---
tags: project
---

* [ ] Task 1 [priority:: 2]
* [ ] Task 2 [priority:: 1]

Is it possible to construct a DQL (ideally not DataviewJS) query which will group the tasks by note and sort the groups and tasks independently, for example to yield:

Project A

  • Task 2
  • Task 1

Project B

  • Task 2
  • Task 1

The following query sorts the groups (notes) correctly, but fails to sort the tasks by the value of priority, which makes sense because SORT is acting on the products of GROUP BY:

TASK

FROM
	#project 

GROUP BY
	file.link as foo

SORT
	foo ASC,
	rows.priority ASC

In this post, @mnvwvnm suggests concatenating the two sort criteria in the GROUP BY expression, for example like this:

GROUP BY
	(file.link + " - " + priority) as foo

SORT
	foo ASC

which yields:

Project A - 1

  • Task 2

Project A - 2

  • Task 1

Project B - 1

  • Task 2

Project B - 2

  • Task 1

This is very close to the desired output above, but cluttered, and wouldn’t do in the case of other sort criteria (e.g. if the notes were sorted by mtime).

Why don’t you just write two simple queries?

### Project A

```dataview
TASK FROM "Project A" SORT priority ASC```

### Project B

```dataview
TASK FROM "Project B" SORT priority ASC```

Thanks for your suggestion.

I tried to reduce the example query to the minimum necessary to describe the problem. I didn’t make clear that in my actual use case there are many project notes which may or may not have open tasks associated with them at any given time. I’d like the query to render headings just for those projects with pending tasks.

OK, I see.

Then try:

TASK
FROM #project
WHERE !completed
SORT priority ASC
GROUP BY file.name
2 Likes

Thanks very much.

I just learned by applying your example that SORT can be invoked twice to apply to tasks, then groups:

SORT
	priority ASC

GROUP BY
	file.link <or a more complex query> as foo

SORT
	foo ASC

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