I want to create a table with literature data from Zotero using the DataView plugin. If there are multiple co-authors, I want to list only the first author.
Things I have tried
It works with the following query, but only if there are multiple co-authors. However, if only one author is present, it doesn’t work: instead of the complete author name, only the first letter of the author name appears.
table
Author[0] as "Autor",
titel as "Titel",
Year as Jahr,
itemType as Item
From #physik
sort Author asc
How can I display only the first author regardless of the number of co-authors?
This is a tricky issue. I happened to run into it earlier today, so I’m happy to share my discovery.
To solve this, we’ll use FLATTEN to create a custom field called FirstAuthor based on Author and use that to display the first author of each book.
This is tricky, because the Author field can be either a single object or an array. Since we have to treat them differently, we have to first detect which kind we have using the typeof() function:
typeof(Author) = "array"
Then we combine that with choice() to return the specific value we want; either the first author if it’s an array, or the author itself if it’s not:
FLATTEN choice(
typeof(Author) = "array",
Author[0],
Author
) as FirstAuthor
Put these together and we get this DQL query:
```dataview
LIST FirstAuthor
FROM "Test/Books"
FLATTEN choice(
typeof(Author) = "array",
Author[0],
Author
) as FirstAuthor
```
In my vault this returns the first listed author whether it’s an array or not:
A variant over this theme, which I’ve found useful, is to force the entry into a list, so that I later can use list methods related to how I want to process it.
Then the same query becomes:
```dataview
LIST Authors[0]
FROM "Test/Books"
FLATTEN choice(
typeof(Author) = "array",
Author,
list(Author)
) as Authors
```
This subtle change, would allow for using Authors in WHERE clauses, like contains(Authors, "Shakespeare") or similar filter operations.
Just thought I would mention this, since it’s likely that one would need to do more work related to authors other than just listing the first one.