Add a unique column (key) to an existing table in DuckDBSQL
We can use DuckDB's `SEQUENCE` to add a unique key to a table.Many source data files (such as CSV files) do not include a unique key.
Execute this SQL
CREATE TABLE tbl (s VARCHAR); INSERT INTO tbl VALUES ( 'hello'),('world'); CREATE SEQUENCE id_sequence; ALTER TABLE tbl ADD COLUMN id INTEGER DEFAULT nextval ('id_sequence'); SELECT * FROM tbl;
Copy code
Expand
Share link
Execute this SQL
-- duckdb has only weekday-name not number -- create enum in the right order create type Weekday as enum ('Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'); -- cast the string to enum for sorting select count(*), cast(dayname(CreationDate) as Weekday) as day from posts where posttypeid = 1 and tags like '%>sql<%' group by all order by day asc; -- doing the cast in order by causes a bug -- ┌──────────────┬───────────┐ -- │ count_star() │ day │ -- │ int64 │ weekday │ -- ├──────────────┼───────────┤ -- │ 103937 │ Monday │ -- │ 115575 │ Tuesday │ -- │ 119825 │ Wednesday │ -- │ 119514 │ Thursday │ -- │ 103445 │ Friday │ -- │ 47139 │ Saturday │ -- │ 47390 │ Sunday │ -- └──────────────┴───────────┘
Copy code
Copy code
Expand
Share link
Execute this SQL
-- list and array_agg take in their own ORDER BY clause, so that you -- can sort the aggregate. The statements order by cannot be used -- as the columns that are used for sorting then need to be a grouping -- key and cannot be used in the aggregate SELECT name, -- Order the aggregated list by another column from line_items list(item_name ORDER BY pos ASC) items FROM orders JOIN line_items ON order_id = id GROUP BY ALL -- Order by grouping keys is ofc possible ORDER BY name;
Copy code
Copy code
Expand
Share link
Allow unsigned extensions (own extensions or untrusted third-parties)Bash
Editor's note: While DuckDB has 20+ official extensions, it also allows you to write your own extensions. In order load extensions from unofficial repositories, you'll need to let DuckDB know that it's safe to do so. This can be done using the allow_unsigned_extensions flag or -unsigned for the CLI.
Execute this Bash
duckdb -unsigned
Copy code
Expand
Share link
Boxplot quantiles all at onceSQL
Editor's note: with a variety of supported statistical functions, including things like sampling and producing quantiles, DuckDB provides great analytical capabilities not always present in other databases.
Execute this SQL
select unnest([.1,.25,.5,.75,.9]) as quantile, unnest(quantile_cont(i, [.1,.25,.5,.75,.9])) as value from range(101) t(i); /* This returns: | quantile | value | | 0.10 | 10 | | 0.25 | 25 | | 0.50 | 50 | | 0.75 | 75 | | 0.90 | 90 | */
Copy code
Expand
Share link
Convert CSV to Parquet and provide schema to useBash
Editor's note: while there are other snippets showing file conversion, Parth's shows you how to convert from CSV to Parquet files using DuckDB with specification of the entire schema (columns) and compression codec.
Execute this Bash
duckdb -c "COPY (SELECT * FROM read_csv('pageviews-sanitized-20230101-000000.csv', delim=' ', header=False, columns={'domain_code': 'VARCHAR', 'page_title': 'VARCHAR', 'count_views': 'UINTEGER', 'total_response_size': 'UINTEGER'})) TO 'pageviews-sanitized-20230101-000000.parquet' (FORMAT 'PARQUET', CODEC 'zstd')"
Copy code
Expand
Share link
Compute a metric for each numeric column and return the values in a long table (requires 0.7.2+)SQL
Editor's note: if your data originates as different types or in a format like CSV, you might want to do so without risking throwing an error for oddly-typed values. You can do so with TRY_CAST(), which will attempt a CAST but return NULL if not possible.
Execute this SQL
with computed as ( select sum(try_cast(columns(*) as double)) from read_csv_auto('aapl.csv') ) select -- restore original column names trim(list_element(regexp_extract_all(name,'\.(.*?) AS',1),1),'"') as name, value from (pivot_longer computed on *)
Copy code
Expand
Share link
Read an MS Excel File with the spatial extensionSQL
Editor's note: It might seem a bit odd, but the DuckDB spatial extension includes a function for reading Microsoft Excel XLSX files into DuckDB. This is because a lot of geospatial files are shared this way, but you can take advantage of this capability even if you have no spatial data!
Execute this SQL
install spatial; load spatial; from st_read('file.xlsx',layer='sheet_name');
Copy code