Skip to content

Commit 3501947

Browse files
committed
docs
1 parent df064ad commit 3501947

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

examples/official-site/highlightjs-tabler-theme.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
.hljs-prolog,
44
.hljs-meta,
55
.hljs-cdata {
6-
color: var(--tblr-gray-300);
6+
color: #a598a3;
77
}
88

99
/* Punctuation */

examples/official-site/sqlpage/migrations/01_documentation.sql

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -735,6 +735,58 @@ INSERT INTO example(component, description, properties) VALUES
735735
'table',
736736
'An empty table with a friendly message',
737737
json('[{"component":"table", "empty_description": "Nothing to see here at the moment."}]')
738+
),
739+
(
740+
'table',
741+
'# Dynamic column names in a table
742+
743+
In all the previous examples, the column names were hardcoded in the SQL query.
744+
This makes it very easy to quickly visualize the results of a query as a table,
745+
but it can be limiting if you want to include columns that are not known in advance.
746+
In situations when the number and names of the columns depend on the data, or on variables,
747+
you can use the `dynamic` component to generate the table columns dynamically.
748+
749+
For that, you will need to return JSON objects from your SQL query, where the keys are the column names,
750+
and the values are the cell contents.
751+
752+
Databases offer utilities to generate JSON objects from query results:
753+
- In PostgreSQL, you can use the [`json_build_object`](https://www.postgresql.org/docs/current/functions-json.html#FUNCTIONS-JSON-PROCESSING)
754+
function for a fixed number of columns, or [`json_object_agg`](https://www.postgresql.org/docs/current/functions-aggregate.html#FUNCTIONS-AGGREGATE) for a dynamic number of columns.
755+
- In SQLite, you can use the [`json_object`](https://www.sqlite.org/json1.html) function for a fixed number of columns,
756+
or the `json_group_object` function for a dynamic number of columns.
757+
- In MySQL, you can use the [`JSON_OBJECT`](https://dev.mysql.com/doc/refman/8.0/en/json-creation-functions.html#function_json-object) function for a fixed number of columns,
758+
or the [`JSON_OBJECTAGG`](https://dev.mysql.com/doc/refman/8.4/en/aggregate-functions.html#function_json-objectagg) function for a dynamic number of columns.
759+
- In Microsoft SQL Server, you can use the [`FOR JSON PATH`](https://docs.microsoft.com/en-us/sql/relational-databases/json/format-query-results-as-json-with-for-json-sql-server?view=sql-server-ver15) clause.
760+
761+
For instance, let''s say we have a table with three columns: store, item, and quantity_sold.
762+
We want to create a pivot table where each row is a store, and each column is an item.
763+
We will return a set of json objects that look like this: `{"store":"Madrid", "Item1": 42, "Item2": 7, "Item3": 0}`
764+
765+
```sql
766+
SELECT ''table'' AS component;
767+
with filled_data as (
768+
select
769+
stores.store, items.item,
770+
(select coalesce(sum(quantity_sold), 0) from store_sales where store=stores.store and item=items.item) as quantity
771+
from (select distinct store from store_sales) as stores
772+
cross join (select distinct item from store_sales) as items
773+
order by stores.store, items.item
774+
)
775+
SELECT
776+
''dynamic'' AS component,
777+
JSON_PATCH( -- SQLite-specific, refer to your database documentation for the equivalent JSON functions
778+
JSON_OBJECT(''store'', store),
779+
JSON_GROUP_OBJECT(item, quantity)
780+
) AS properties
781+
FROM
782+
filled_data
783+
GROUP BY
784+
store;
785+
```
786+
787+
This will generate a table with the stores in the first column, and the items in the following columns, with the quantity sold in each store for each item.
788+
789+
', NULL
738790
);
739791

740792

0 commit comments

Comments
 (0)