Migrate query Silverbullet V2 with subquery

V2.
Is possible subquery?

${query[[
    from p = index.tag "page"
      where p.type=="contact"
      select acction=query[[
          from p2 = index.tag "page"
          where table.includes(p2.contacts, p.name)
    ]]
    ]]
}

Action page has yaml

---
type: action
contacts: fred, peter
---

Contact page has yaml

---
type: contact
name: fred
---

My v1 query has each into each:

{{#each @p in {page where tipo and 'contacto' in tipo order by titulo}}} 
{{#each @p2 in {page where tipo and 'accion' in tipo and @p.name=contactos ) }}} 
[[{{@p2.name}}|{{@p.titulo}} -> {{@p2.titulo}}]] {{/each}} {{/each}}

Very thanks.

Subqueries are possible, the query[[]] is part of the Space Lua syntax (as of now it’s not a function call although it looks like it is).

${
#query[[
  from query[[
    from query[[
      from {1, 2, 3}
    ]]
  ]]
]]
}

Just works. :slight_smile:

Hello, is not exactly that.

I find make a query on select part.

Example:

Action page has yaml

Page name: 001
---
type: action
contacts: 0004
---

Page name: 002
---
type: action
contacts: 0005
---

Contact page has yaml

Page name: 0004
---
type: contact
pname: fred
---

Page name: 0005
---
type: contact
pname: peter
---

I find list actions pages replacing pagename contact for contact’s field pname.

Expected result, a list
action page name, contact page pname
001, fred
002, peter

V1 silverbullet allows this (query first comment) but I haven’t been able to do it since version 2.

${
query[[
  from query[[
    from p = index.tag "page"
    where p.type=="action" and p.contacts
    select {
      p.name,
      query[[
        from p2 = index.tag "page"
        where p2.type=="contact" and p2.name==p.contacts
        select p2.pname
      ]] 
      }
  ]]
]]
}

Expected result, a list
p.name, p2.pname
…,…

The select clause is not like a SQL clause in that it simply returns a Lua expression. What this means:

select {
      p.name,
      query[[
        from p2 = index.tag "page"
        where p2.type=="contact" and p2.name==p.contacts
        select p2.pname
      ]] 
      }

is: return a table (array in this case) with two values: p.name and the entirety of the query result of query[[...]]

What you are probably looking for instead is something along the lines of

select {
   name = p.name,
   pname = (query[[...]])[1]
}

not really in a position to test this exactly, but this should directionally work. Your sub-query returns a collection of strings, and you pick the first element (which in Lua has index 1).

Not work.
Example not work

${query[[
    from p=index.tag "page"
      where p.type=="accion"
      select 
      {p.name, query[[
    from p2.index.tag "page"
    where p2.name=="test"
    select p2.name
    ]]}
    ]]
}

This is possible?

${query[[
    from p=index.tag "page"
      where p.type=="accion"
      select 
      {p.name, query[[
    from p2.index.tag "page"
    where p2.name==p.related_name #attention to "p." not "p2." this is a yaml field of main query
    select p2.name
    ]]}
    ]]
}

It seems to me that you are not exactly applying Zef Hemel’s suggestion: the 2nd query is in parentheses and brackets then, ultimately, you have to choose an index position ([1]).

No diference:

Lua error: Parse error at pos 195

I give up on doing this.
Without documentation and having to bother others, it’s difficult for me to move forward.
Thank you very much anyway.