DatumHueEnterprise v0.0.24datumhue.com
Contents

123Your first data view · Step 2 of 3

Ask with SQL

You'll learn10 min
Time
10 min
Builds on
Step 1: Load a table
File
view.lua

Which item sold most this week? In this step your app asks the sales table that question in SQL, and shows the answer beside the rows.

Ask a question

Add a query to view.lua, and print each row of its answer:

view.lualua+10 new lines
local ui = datumhue.ui
local data = datumhue.args.dirs.data
local sales = data:read("sales.csv"):csv()

New line: local totals = sales:query([[
New line:     SELECT item, SUM(sold) AS total
New line:     FROM data
New line:     GROUP BY item
New line:     ORDER BY total DESC
New line: ]])
New line: for _, row in totals:rows() do
New line:     print(row.item, row.total)
New line: end
New line: 
local page = ui.root:panel({
    width = "100%",
    height = "100%",
    flex_direction = "column",
    padding = 16,
    gap = 12,
})
page:label({
    text = "Bakery sales: " .. sales:count() .. " rows",
    font_size = 20,
})
page:table({
    data = sales,
    columns = {
        { column = "day", label = "Day" },
        { column = "item", label = "Item" },
        { column = "sold", label = "Sold" },
    },
    flex_grow = 1,
})

In this example: datumhue.ui datumhue.args Dir:read Bytes:csv DataHandle:query DataHandle:rows datumhue.ui.root UiElement:panel UiElement:label DataHandle:count UiElement:table

Run it as before:

sh
$ datumhue run view.lua --dir data:data

It prints the answer, the best seller first:

text
Bread   333
Buns    192
Cake    85
  • DataHandle:query runs SQL against the table, which the query calls data. It returns a new DataHandle, the answer, at once.
  • SUM(sold) adds up each item's sales, GROUP BY item makes one row per item, and ORDER BY total DESC puts the largest total first.
  • DataHandle:rows steps through the answer one row at a time. Each row is a Lua table keyed by column name.

Nothing runs when you call DataHandle:query. The query runs the first time something reads its answer, here the for loop, and keeps the result. A table, a chart or DataHandle:count that reads it later gets the kept result without running it again.

Show the answer

The answer is a DataHandle like the sales, so a table can show it. Put the two tables side by side, and drop the loop:

view.lualua+14 new, 4 removed
local ui = datumhue.ui
local data = datumhue.args.dirs.data
local sales = data:read("sales.csv"):csv()

local totals = sales:query([[
    SELECT item, SUM(sold) AS total
    FROM data
    GROUP BY item
    ORDER BY total DESC
]])

local page = ui.root:panel({
    width = "100%",
    height = "100%",
    flex_direction = "column",
    padding = 16,
    gap = 12,
})
page:label({
    text = "Bakery sales: " .. sales:count() .. " rows",
    font_size = 20,
})
New line: local row = page:panel({
New line:     flex_direction = "row",
New line:     flex_grow = 1,
New line:     gap = 12,
New line: })
New line: row:table({
    data = sales,
    columns = {
        { column = "day", label = "Day" },
        { column = "item", label = "Item" },
        { column = "sold", label = "Sold" },
    },
New line:     width = 300,
New line: })
New line: row:table({
New line:     data = totals,
New line:     columns = {
New line:         { column = "item", label = "Item" },
New line:         { column = "total", label = "Sold this week" },
New line:     },
    flex_grow = 1,
})

In this example: datumhue.ui datumhue.args Dir:read Bytes:csv DataHandle:query datumhue.ui.root UiElement:panel UiElement:label DataHandle:count UiElement:table

The week's rows sit on the left, and the three totals on the right.

  • row is a panel that lays out what it holds side by side.
  • The rows' table keeps a width of 300 UI px; the answer's table grows into the rest.
  • The answer's table reads totals itself, which runs the query.

SQL functions lists every function a query can call, such as AVG, with what it takes and returns.

Try this

  • Show the average sold per item instead of the total, with AVG.
  • Order the answer by item name.
  • Ask which day sold the most: group by day instead of item.

Recap

  • DataHandle:query returns a new DataHandle at once. In the SQL, the table it asks is data.
  • A query runs the first time something reads its answer, and keeps the result for every later reader.
  • The answer is a DataHandle, so anything that shows a table shows the answer too.

New in this step

datumhue.com

(c) 2026 Shuusei AB. Quote excerpts of this manual, with attribution to DatumHue, in your own documentation, tutorials, course materials, reviews, and other writing or video. Republishing it in whole or in substantial part needs written permission: support@datumhue.com.

Victor Mono is licensed under the SIL Open Font License 1.1
Copyright (c) 2024, Rune Bjørnerås (https://github.com/rubjo)

This Font Software is licensed under the SIL Open Font License, Version 1.1.
This license is copied below, and is also available with a FAQ at:
http://scripts.sil.org/OFL


-----------------------------------------------------------
SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007
-----------------------------------------------------------

PREAMBLE
The goals of the Open Font License (OFL) are to stimulate worldwide
development of collaborative font projects, to support the font creation
efforts of academic and linguistic communities, and to provide a free and
open framework in which fonts may be shared and improved in partnership
with others.

The OFL allows the licensed fonts to be used, studied, modified and
redistributed freely as long as they are not sold by themselves. The
fonts, including any derivative works, can be bundled, embedded,
redistributed and/or sold with any software provided that any reserved
names are not used by derivative works. The fonts and derivatives,
however, cannot be released under any other type of license. The
requirement for fonts to remain under this license does not apply
to any document created using the fonts or their derivatives.

DEFINITIONS
"Font Software" refers to the set of files released by the Copyright
Holder(s) under this license and clearly marked as such. This may
include source files, build scripts and documentation.

"Reserved Font Name" refers to any names specified as such after the
copyright statement(s).

"Original Version" refers to the collection of Font Software components as
distributed by the Copyright Holder(s).

"Modified Version" refers to any derivative made by adding to, deleting,
or substituting -- in part or in whole -- any of the components of the
Original Version, by changing formats or by porting the Font Software to a
new environment.

"Author" refers to any designer, engineer, programmer, technical
writer or other person who contributed to the Font Software.

PERMISSION & CONDITIONS
Permission is hereby granted, free of charge, to any person obtaining
a copy of the Font Software, to use, study, copy, merge, embed, modify,
redistribute, and sell modified and unmodified copies of the Font
Software, subject to the following conditions:

1) Neither the Font Software nor any of its individual components,
in Original or Modified Versions, may be sold by itself.

2) Original or Modified Versions of the Font Software may be bundled,
redistributed and/or sold with any software, provided that each copy
contains the above copyright notice and this license. These can be
included either as stand-alone text files, human-readable headers or
in the appropriate machine-readable metadata fields within text or
binary files as long as those fields can be easily viewed by the user.

3) No Modified Version of the Font Software may use the Reserved Font
Name(s) unless explicit written permission is granted by the corresponding
Copyright Holder. This restriction only applies to the primary font name as
presented to the users.

4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font
Software shall not be used to promote, endorse or advertise any
Modified Version, except to acknowledge the contribution(s) of the
Copyright Holder(s) and the Author(s) or with their explicit written
permission.

5) The Font Software, modified or unmodified, in part or in whole,
must be distributed entirely under this license, and must not be
distributed under any other license. The requirement for fonts to
remain under this license does not apply to any document created
using the Font Software.

TERMINATION
This license becomes null and void if any of the above conditions are
not met.

DISCLAIMER
THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE
COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM
OTHER DEALINGS IN THE FONT SOFTWARE.

Try

    ↑↓ selectEnter openTab filter

    Settings

    Theme
    Text
    Print

    Keyboard shortcuts

    / or Ctrl KSearch
    ↑ ↓ EnterPick a result
    TabNext result filter
    j kNext or previous entry
    [ ]Previous or next page
    yCopy a link to the current entry
    ?These shortcuts
    EscClose

    On this page