Advice: how to best incorporate a script to populate table

project description: dynamically populate a table made in xwiki
question: best way to incorporate the script

Not the best example:

#set ($sql = "select doc.fullName, email.value from XWikiDocument as doc, BaseObject as obj, StringProperty as email where doc.fullName = and obj.className = 'XWiki.XWikiUsers' and = and ='email' and doc.creationDate > '2019-02-04' order by doc.fullName")
(% class="table-condensed" style="auto;" %)
#set ($result = $services.query.hql($sql).execute())
#foreach ($item in $result)
|$xwiki.getUserName($item[0], false)|$item[1]|
 | |

or look at