Last active
January 26, 2025 21:16
-
-
Save Bilbottom/4e6919b447c320ed8631f3a4cb6aa930 to your computer and use it in GitHub Desktop.
DuckDB cross-data-source query
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
DuckDB cross-data-source query | |
DuckDB version: v1.1.1 | |
Bill Wallis, 2024-12-15 | |
This is just for illustration: the files are purposefully not included in this Gist | |
*/ | |
attach '...\daily_tracker\tracker.db' as tracker (type sqlite, read_only); | |
attach 'dbname=... host=... user=... password=...' as work_hr (type postgres, read_only); | |
install spatial; | |
load spatial; | |
with | |
dates(work_date) as ( | |
values | |
('2024-01-01'::date), | |
('2024-01-02'::date), | |
('2024-01-03'::date), | |
('2024-01-04'::date), | |
('2024-01-05'::date), | |
), | |
/* SQLite: roll the work logs up to a daily grain */ | |
worked_hours(work_date, hours_worked) as ( | |
select | |
date_time::date, | |
(sum(interval) / 60)::decimal(4, 2), | |
from tracker.tracker | |
group by all | |
), | |
/* PostgreSQL: keep only my absences */ | |
absences(work_date, absence_hours) as ( | |
select absence_date, hours | |
from work_hr.employee_management.absences | |
where employee_id = 54775 | |
), | |
/* Excel: keep only my billable hours */ | |
billable_hours(work_date, billable_hours) as ( | |
select bill_date, billable_hours | |
from st_read('...\Billable Hours MASTER v2.xlsx') | |
where employee_id = 54775 | |
), | |
/* Web: explode the UK bank holiday JSON into a table */ | |
bank_holidays(title, work_date, notes, bunting) as ( | |
select unnest("england-and-wales".events, recursive:=true) | |
from 'https://www.gov.uk/bank-holidays.json' | |
) | |
select | |
work_date, | |
coalesce(worked_hours.hours_worked, 0) as hours_worked, | |
coalesce(absences.absence_hours, 0) as absence_hours, | |
billable_hours.billable_hours, | |
bank_holidays.work_date is not null as is_bank_holiday, | |
from dates | |
left join worked_hours using (work_date) | |
left join absences using (work_date) | |
left join billable_hours using (work_date) | |
left join bank_holidays using (work_date) | |
order by work_date | |
; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is part of the following LinkedIn post: