Skip to content

Instantly share code, notes, and snippets.

View billwallis's full-sized avatar
🧙
Mathemagicianing

Bill billwallis

🧙
Mathemagicianing
View GitHub Profile
@billwallis
billwallis / ddl.sql
Created August 10, 2025 07:51
SQL's ASOF JOIN alternative
drop table if exists users;
create table users (
user_id int primary key,
username varchar(50) not null,
last_review_ts datetime not null
);
insert into users (user_id, username, last_review_ts)
values
(1, 'Alex', '2023-12-01 10:00:00'),
(2, 'Blake', '2024-01-20 21:30:00'),
@billwallis
billwallis / ddl.sql
Created August 5, 2025 06:59
SQL's GROUPS BETWEEN alternative
/*
SQL's GROUPS BETWEEN alternative
Bill Wallis, 2025-08-05
*/
drop table if exists trials;
create table trials (
trial_id int,
trial_date date,
@billwallis
billwallis / ddl.sql
Last active August 10, 2025 08:49
SQL's RANGE BETWEEN alternative
/*
SQL's RANGE BETWEEN alternative
Bill Wallis, 2025-08-02
*/
drop table if exists events;
create table events (
event_id int primary key,
user_id int not null,
@billwallis
billwallis / validate_sql.py
Created July 27, 2025 07:48
Validate a SQL query's objects against the database information schema
# /// script
# dependencies = [
# "duckdb>=1.3.1",
# "sqlglot[rs]>=27.2.0",
# ]
# ///
import duckdb
import sqlglot
import sqlglot.optimizer.qualify
@billwallis
billwallis / logic-to-data.sql
Last active July 24, 2025 21:22
Move logic to data
/*
Moving logic to data
DuckDB version: v1.3.1
Bill Wallis, 2025-07-24
*/
select version();
@billwallis
billwallis / filter-over.sql
Created May 12, 2025 06:51
Using FILTER in a window (OVER)
/*
Using FILTER in a window (OVER)
PostgreSQL version: 16.2
Bill Wallis, 2025-05-12
*/
drop table if exists events;
create table events (
@billwallis
billwallis / keep-awake
Created April 15, 2025 10:20
Keep screen awake script using wakepy and uv
#!/your-path-to/.local/bin/uv run --script
# /// script
# requires-python = ">=3.11"
# dependencies = [
# "wakepy",
# ]
# ///
"""
@billwallis
billwallis / duckdb-cross-data-source-query.sql
Last active August 1, 2025 12:34
DuckDB cross-data-source query
/*
DuckDB cross-data-source query
DuckDB version: v1.3.2
Bill Wallis, 2024-12-15
This is just for illustration: the files are purposefully not included in this Gist
*/
@billwallis
billwallis / csv-1.csv
Created December 4, 2024 09:29
DuckDB CSV diff
id col_1 col_2 col_3
1 a b 0.1
2 c d -11
3 e f 111
@billwallis
billwallis / connect-by-oracle.sql
Last active October 21, 2024 07:03
Recursive CTEs for graph traversal
/* flatten bus route */
with bus_stops(from_stop, to_stop) as (
select 'BM', 'WQS' from dual
union all select 'CR', 'BM' from dual
union all select 'CS', 'LL' from dual
union all select 'GG', 'CS' from dual
union all select 'LL', 'OS' from dual
union all select 'OS', 'CR' from dual
union all select 'WQS', 'GG' from dual
)