# Dataview: reuse DQL queries?

**URL:** <https://forum.obsidian.md/t/dataview-reuse-dql-queries/44370>\
**Category:** Plugins ideas (Archival in progress)\
**Tags:** dataview\
**Created:** [October 3, 2022, 6:59pm UTC](https://forum.obsidian.md/t/dataview-reuse-dql-queries/44370 "2022-10-03T18:59:08Z")\
**Posts on this page:** 10\
**Page:** 2

<div class="post-metadata">

**Author:** ![Craig](https://forum.obsidian.md/user_avatar/forum.obsidian.md/craig/32/8831_2.png) [@Craig](https://forum.obsidian.md/u/Craig)\
**Post date:** [April 25, 2023, 6:29pm UTC](https://forum.obsidian.md/t/dataview-reuse-dql-queries/44370/21 "2023-04-25T18:29:00Z")

</div>

Hi @anon63144152, great question. I’m generally a fan of more atomic notes, so I would probably keep it to one file per query and then refer to them individually.

Of course, if the two queries depend on each other, or would always be together, then it might make sense to keep them in the same file.

---

<div class="post-metadata">

**Author:** ![anon63144152](https://forum.obsidian.md/letter_avatar_proxy/v4/letter/a/a698b9/32.png) [@anon63144152](https://forum.obsidian.md/u/anon63144152)\
**Post date:** [April 25, 2023, 8:15pm UTC](https://forum.obsidian.md/t/dataview-reuse-dql-queries/44370/22 "2023-04-25T20:15:04Z")

</div>

I think I am going to have to go for one query in each JS file as I can’t get two queries in the same file to run. I would ideally like to add some inline JS snippets in the JS file as well but not been able to make those work either. Baby steps. Lots of reading to do.

But brilliant idea. Loving the paths it is taking my mind along. 😻🤯🧘🏼

---

<div class="post-metadata">

**Author:** ![ShaneNZ](https://forum.obsidian.md/user_avatar/forum.obsidian.md/shanenz/32/4048_2.png) [@ShaneNZ](https://forum.obsidian.md/u/ShaneNZ)\
**Post date:** [April 26, 2023, 7:19am UTC](https://forum.obsidian.md/t/dataview-reuse-dql-queries/44370/23 "2023-04-26T07:19:22Z")

</div>

Inline JS snippets don’t work correctly in live preview from memory, have a look in reading mode - you might find they’re working there.

---

<div class="post-metadata">

**Author:** ![anon63144152](https://forum.obsidian.md/letter_avatar_proxy/v4/letter/a/a698b9/32.png) [@anon63144152](https://forum.obsidian.md/u/anon63144152)\
**Post date:** [April 26, 2023, 9:04am UTC](https://forum.obsidian.md/t/dataview-reuse-dql-queries/44370/24 "2023-04-26T09:04:40Z")

</div>

Something like this works when placed directly in a MD note in both live preview and reading mode:

```auto
`$= moment(dv.current().file.day.toISODate()).diff(moment('2022-11-14'), 'years').toLocaleString('en-en') + " years"` / `$= moment(dv.current().file.day.toISODate()).diff(moment('2022-11-14'), 'days').toLocaleString('en-en') + " days"`   

```

But if I put it in its own JS file—exactly as it is written above—and then call it in a MD note it doesn’t work:

````auto
> [!INFO] Quote  
> ```dataviewjs
>dv.view("/js/t1");
>```

````

I am also trying to put two DQL tables in one JS file but can’t get that to work as yet.

```auto
dv.execute(`

TABLE
file.tags as Tags
WHERE file.cday = this.file.day
or file.mday = this.file.day
SORT file.mtime desc
LIMIT 10

`);

dv.execute(`

dataview
TABLE
file.tags as Tags
FROM #obsidian
SORT file.mtime desc
LIMIT 10

`);

```

Busy reading the effing manual 🥲

---

<div class="post-metadata">

**Author:** ![anon63144152](https://forum.obsidian.md/letter_avatar_proxy/v4/letter/a/a698b9/32.png) [@anon63144152](https://forum.obsidian.md/u/anon63144152)\
**Post date:** [April 26, 2023, 12:03pm UTC](https://forum.obsidian.md/t/dataview-reuse-dql-queries/44370/25 "2023-04-26T12:03:32Z")

</div>

The following example is given on the [Dataview help pages](https://blacksmithgu.github.io/obsidian-dataview/api/code-reference/#dvexecutesource):

 ![rtfm](https://forum.obsidian.md/uploads/default/original/3X/f/6/f6be7351bfda286c5e5cb65961a1262f24391cd7.jpeg)

If I create a JS file (called t1) with:

```auto
dv.execute("LIST FROM #obsidian");
dv.execute("TABLE title, author FROM #css");

```

…only the second query is executed when t1 is embedded in a MD file:

````auto
```dataviewjs
dv.view("/_inbox/t1");
```

````

Shouldn’t both execute in line with the help pages example?

Thanks for any suggestions.

---

<div class="post-metadata">

**Author:** ![alltagsverstand](https://forum.obsidian.md/user_avatar/forum.obsidian.md/alltagsverstand/32/4265_2.png) [@alltagsverstand](https://forum.obsidian.md/u/alltagsverstand)\
**Post date:** [April 26, 2023, 1:46pm UTC](https://forum.obsidian.md/t/dataview-reuse-dql-queries/44370/26 "2023-04-26T13:46:54Z")

</div>

Tried a couple of different solutions but always get some (varying) error messages.

I have the following dataview query:

```auto
TABLE WITHOUT ID Schlagwort, map(rows, (r) ="* " + link(r.file.link, r.title) + "<br>" + " (" + r.kategorie + ")") AS Einträge
FROM !"bibliothek"
WHERE file.path !=this.file.path
FLATTEN file.tags as Schlagwort
GROUP BY Schlagwort
WHERE econtains(this.file.etags, Schlagwort)

```

It works absolutely fine when put directly into a note. When put into a separate note and called (in the active note) via

`$= dv.execute(await dv.io.load("path/to/file.md"))`

(as @orand suggested [here](https://forum.obsidian.md/t/dataview-reuse-dql-queries/44370/10)), it says “No implementation of ‘map’ found for arguments: array, boolean”.

What am I doing wrong?

---

<div class="post-metadata">

**Author:** ![holroy](https://forum.obsidian.md/user_avatar/forum.obsidian.md/holroy/32/77699_2.png) [@holroy](https://forum.obsidian.md/u/holroy)\
**Post date:** [April 26, 2023, 5:34pm UTC](https://forum.obsidian.md/t/dataview-reuse-dql-queries/44370/27 "2023-04-26T17:34:30Z")

</div>

> [@alltagsverstand](#):
>
> `map(rows, (r) ="* "`

This part needs to become

```auto
map(rows, (r) => "* "

```

for starters. Don’t know if there are other errors, as well.

---

<div class="post-metadata">

**Author:** ![alltagsverstand](https://forum.obsidian.md/user_avatar/forum.obsidian.md/alltagsverstand/32/4265_2.png) [@alltagsverstand](https://forum.obsidian.md/u/alltagsverstand)\
**Post date:** [April 26, 2023, 5:47pm UTC](https://forum.obsidian.md/t/dataview-reuse-dql-queries/44370/28 "2023-04-26T17:47:26Z")

</div>

> [@holroy](#):
>
> ```auto
> map(rows, (r) => "* "
> 
> ```

That’s it - you’re my hero! 🥳

---

<div class="post-metadata">

**Author:** ![holroy](https://forum.obsidian.md/user_avatar/forum.obsidian.md/holroy/32/77699_2.png) [@holroy](https://forum.obsidian.md/u/holroy)\
**Post date:** [April 26, 2023, 7:18pm UTC](https://forum.obsidian.md/t/dataview-reuse-dql-queries/44370/29 "2023-04-26T19:18:36Z")

</div>

> [@anon63144152](#):
>
> ```auto
> diff(moment('2022-11-14'), 'days').toLocaleString('en-en') + " days"`   
> 
> ```
> 
> But if I put it in its own JS file—exactly as it is written above—and then call it in a MD note it doesn’t work:

Essentially that code is:

```javascript
`$= moment(....) + " text " ` / `$= moment( ... ) + "text"`

```

Which will not execute within javascript either, or more correct it’ll not produce any sensible output. Remember that inline _dataviewjs_ query is kind of surrounded by `dv.span()` in order to produce its output.

So if you change the file to include to something like:

```javascript
dv.span(moment( ...) + "text " + moment( ... ) + "text")

```

It’ll happily execute it.

Regarding the double DQL queries to be executed, that’s another matter it seems. There is an extra `dataview` before the second query, which makes it not compile, and give an error message on the console. But even with that fixed, it’s a little tricky to get it to work properly.

There are concurrency issues related to what runs when, and also related to how the `dv.execute` (or `dv.query` which I also tested) and the other `dv.functions()` calls are executed and hook into the DOM. At least, when I added the javascript above, two `dv.header()` calls, and the two `dv.execute()` the order got messed up, and only one of the two queries (seemingly random which one) was executed.

> **My full javascript-file**
>
> ```javascript
> dv.span(
> moment(dv.current().file.day.toISODate())
> .diff(moment('2022-11-14'), 'years')
> .toLocaleString('en-en') + 
> " years / " +
> moment(dv.current().file.day.toISODate())
> .diff(moment('2022-11-14'), 'days')
> .toLocaleString('en-en')
> + " days" )
>                  
> dv.header(2, "First query")
> 
> dv.execute(`
> TABLE
> file.tags as Tags1
> WHERE file.tags 
> WHERE file.cday = this.file.day
> OR file.mday = this.file.day
> SORT file.mtime desc
> LIMIT 10
> `); /* */
> 
> dv.header(2, "Second query")
> 
> dv.execute(`
> TABLE 
> file.tags as Tags2 , file.cday
> WHERE file.tags
> SORT file.mtime desc 
> LIMIT 10
> `);          
> 
> ```

So I’m thinking it’s not wise to include too much of this stuff, especially in the context of calling through `dv.view()`. I’ve never experience this order issue, or stuff being left out when doing it directly within _dataviewjs_.

---

<div class="post-metadata">

**Author:** ![anon63144152](https://forum.obsidian.md/letter_avatar_proxy/v4/letter/a/a698b9/32.png) [@anon63144152](https://forum.obsidian.md/u/anon63144152)\
**Post date:** [April 29, 2023, 11:46am UTC](https://forum.obsidian.md/t/dataview-reuse-dql-queries/44370/30 "2023-04-29T11:46:05Z")

</div>

> [@holroy](#):
>
> So if you change the file to include to something like:
> 
> ```auto
> dv.span(moment( ...) + "text " + moment( ... ) + "text")
> 
> ```
> 
> It’ll happily execute it.

Apologies for the tardy reply—not been online for a few days. 😱

Thanks for the help. Realize that this is beyond what I can work with at the moment. Either need to learn more or to just keep things simple.

Right now the fun of Obsidian is bleeding away over the frustration of things I can’t tame. Time to take a break. 🛑 👋🏽 😿

[Previous page](https://forum.obsidian.md/t/dataview-reuse-dql-queries/44370.md?page=1)
