|
| 1 | +#### Dynamic column names in a table |
| 2 | + |
| 3 | +In all the previous examples, the column names were hardcoded in the SQL query. |
| 4 | +This makes it very easy to quickly visualize the results of a query as a table, |
| 5 | +but it can be limiting if you want to include columns that are not known in advance. |
| 6 | +In situations when the number and names of the columns depend on the data, or on variables, |
| 7 | +you can use the `dynamic` component to generate the table columns dynamically. |
| 8 | + |
| 9 | +For that, you will need to return JSON objects from your SQL query, where the keys are the column names, |
| 10 | +and the values are the cell contents. |
| 11 | + |
| 12 | +Databases [offer utilities to generate JSON objects from query results](/blog.sql?post=JSON%20in%20SQL%3A%20A%20Comprehensive%20Guide) |
| 13 | + - In PostgreSQL, you can use the [`json_build_object`](https://www.postgresql.org/docs/current/functions-json.html#FUNCTIONS-JSON-PROCESSING) |
| 14 | +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. |
| 15 | + - In SQLite, you can use the [`json_object`](https://www.sqlite.org/json1.html) function for a fixed number of columns, |
| 16 | +or the `json_group_object` function for a dynamic number of columns. |
| 17 | + - 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, |
| 18 | +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. |
| 19 | + - 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. |
| 20 | + |
| 21 | +For instance, let's say we have a table with three columns: store, item, and quantity_sold. |
| 22 | +We want to create a pivot table where each row is a store, and each column is an item. |
| 23 | +We will return a set of json objects that look like this: `{"store":"Madrid", "Item1": 42, "Item2": 7, "Item3": 0}` |
| 24 | + |
| 25 | +```sql |
| 26 | +SELECT 'table' AS component; |
| 27 | +with filled_data as ( |
| 28 | + select |
| 29 | + stores.store, items.item, |
| 30 | + (select coalesce(sum(quantity_sold), 0) from store_sales where store=stores.store and item=items.item) as quantity |
| 31 | + from (select distinct store from store_sales) as stores |
| 32 | + cross join (select distinct item from store_sales) as items |
| 33 | + order by stores.store, items.item |
| 34 | +) |
| 35 | +SELECT |
| 36 | + 'dynamic' AS component, |
| 37 | + JSON_PATCH( -- SQLite-specific, refer to your database documentation for the equivalent JSON functions |
| 38 | + JSON_OBJECT('store', store), |
| 39 | + JSON_GROUP_OBJECT(item, quantity) |
| 40 | + ) AS properties |
| 41 | +FROM |
| 42 | + filled_data |
| 43 | +GROUP BY |
| 44 | + store; |
| 45 | +``` |
| 46 | + |
| 47 | +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. |
0 commit comments