DuckDB has a json_extract()
function that extracts JSON data from a JSON document. It enables us to get JSON values from within the JSON document, rather than returning the whole document itself. This article takes a quick look at the function along with some examples of usage.
Tag: what is
Examples of EPOCH_US() in DuckDB
DuckDB provides us with a bunch of epoch...()
functions that enable us to get the Unix epoch time from a given date/time value. Different functions return their result using different units (for example seconds, milliseconds, etc). The epoch_us()
function returns its result in microseconds.
Unix epoch time is typically expressed as the number of seconds that have elapsed since January 1, 1970 (UTC), but epoch_us()
function returns the equivalent amount in microseconds.
DuckDB JSON_EXISTS(): Check if a Path Exists in a JSON Document
Most DuckDB distributions are shipped with the json
extension, which enables us to work with JSON documents. DuckDB provides a bunch of JSON functions, including JSON_EXISTS()
. The JSON_EXISTS()
function allows us to check whether a specific path exists within a JSON document.
This article explores how this function works, along with examples.
Continue readingA Quick Look at EPOCH_MS() in DuckDB
In DuckDB, the epoch_ms()
function serves a dual purpose. It converts timestamp values into Unix epoch time in milliseconds and also performs the reverse operation, transforming Unix epoch time values back into timestamps.
Unix epoch time is typically expressed as the number of seconds that have elapsed since January 1, 1970 (UTC), but this function returns the equivalent amount in milliseconds.
The function is similar to the epoch()
function, which returns its result in seconds. However, the epoch()
function only works in one direction; it converts a timestamp value to epoch time, but it doesn’t work the other way around like epoch_ms()
can.
Using DuckDB’s FSUM() Function for More Accurate Results
DuckDB has a fsum()
function that can be used instead of the regular sum()
function in order to get more accurate results. fsum()
calculates the sum using a floating point summation method known as Kahan summation (or compensated summation).
This method helps reduce the accumulation of rounding errors that can occur when summing many floating point numbers when using the regular sum
() function.
A Quick Look at DuckDB’s WEIGHTED_AVG() Function
In analytical SQL workloads, expressing weighted averages can sometimes involve verbose expressions such as combining the sum()
function with other operators. DuckDB streamlines this with its native weighted_avg()
aggregate function, allowing us to compute weighted averages directly and efficiently. The weighted_avg()
function enhances both clarity and speed when dealing with data where values contribute unequally — such as population-adjusted metrics or revenue-weighted scores.
This article explores the weighted_avg()
in DuckDB, along with examples to demonstrate its usage.
An Overview of DuckDB’s TYPEOF() Function
DuckDB offers a variety of utility functions to help us better understand the data that we need to work with. Among these functions, typeof()
serves as a useful tool for type inspection and validation. In this article, we’ll explore how this function works, along with examples that demonstrate its usage.
A Quick Look at the LIST_AGGREGATE() Function in DuckDB
DuckDB’s list_aggregate()
function is a handy tool for performing grouped aggregations over lists. It allows us to apply any aggregate function (like sum
, avg
, min
, max
, count
, etc.) to a list column as if each list item were a row.
The way it works is that we pass the list as the first argument, followed by the name of an aggregate function we want to apply to that list. The function will return its result as if it were the named aggregate function.
Continue readingDuckDB CAST(): Converting Between Data Types
Most SQL databases provide a cast()
function and DuckDB is no exception. The purpose of the cast()
function is to convert a value from one data type to another. This can also be referred to as casting the value as the other data type.
A Quick Look at LIST_SLICE() in DuckDB
DuckDB has a list_slice()
function that enables us to select multiple list items from a list, based on their index range within the list. For example, we can select all list items between position 5 and 10 in a list.