Skip to content

Instantly share code, notes, and snippets.

@carlineng
carlineng / nested_subtotals_bard.sql
Created May 14, 2023 01:08
Bard's answer to the nested subtotals SQL question
-- Prompt:
-- I want help writing a SQL query. I have a database with the two tables:
-- CREATE TABLE order_items (
-- created_at timestamp,
-- sale_price decimal,
-- inventory_item_id integer
-- );
--
-- CREATE TABLE inventory_items (
@carlineng
carlineng / nested_subtotals_gpt4.sql
Created May 14, 2023 00:54
GPT-4's answer to the nested subtotals SQL question
-- Prompt:
--
-- I want help writing a SQL query. I have a database with the two tables:
--
-- CREATE TABLE order_items (
-- created_at timestamp,
-- sale_price decimal,
-- inventory_item_id integer
-- );
--
@carlineng
carlineng / nested_subtotals.sql
Last active May 13, 2023 00:46
Nested sub-totals in SQL
-- Using DuckDB SQL dialect
-- Data for these queries can be found here: https://github.com/malloydata/malloy-samples/tree/main/duckdb/ecommerce
-- Question: I want to calculate year-over-year sales for this e-commerce dataset.
-- Then, for each product_department, I want to calculate year-over-year sales in that department,
-- and the contribution of that department to the overall year-over-year sales growth.
-- Then, for each product_category, I want to calculate year-over-year sales for that category,
-- and the contribution of that category to the departmental year-over-year sales growth.
-- How do I do that in SQL?
-- Two approaches. One with window functions, one with GROUP BY ROLLUP.
@carlineng
carlineng / tpch_graph.malloy
Created April 9, 2023 23:31
Explaining implicit context in queries in Sources
import "model.malloy"
/*
The file `model.malloy` (imported above) is defined as follows:
source: lineitem is table('duckdb:../data/lineitem.parquet') {
join_one:
orders is table('duckdb:../data/orders.parquet') on orders.o_orderkey = l_orderkey
customer is table('duckdb:../data/customer.parquet') on customer.c_custkey = orders.o_custkey
@carlineng
carlineng / tpcds21.malloy
Last active February 23, 2023 20:43
TPC-DS Query 21
// Malloy Implementation
// TPC-DS Query 21
import "tpcds.malloy"
query: inventory -> {
declare:
inv_before is sum(inv_quantity_on_hand) { where: date_dim.d_date < '2000-03-11' }
inv_after is sum(inv_quantity_on_hand) { where: date_dim.d_date >= '2000-03-11' }
@carlineng
carlineng / tpcds33.malloy
Created February 23, 2023 04:05
TPC-DS Query 33
query: all_sales + {
join_one: manufacturers is from(
item -> {
group_by: i_manufact_id
where: i_category = 'Electronics'
}
) on item.i_manufact_id = manufacturers.i_manufact_id
} -> {
group_by: item.i_manufact_id
aggregate: total_ext_sales_price
@carlineng
carlineng / tpcds4.malloy
Last active February 23, 2023 20:37
TPC-DS Query 4
// Malloy Implementation
// TPC-DS Query 4
import "tpcds.malloy"
query: all_sales -> {
declare:
catalog_total_sales_2001 is total_sales { where: date_dim.d_year = 2001 and channel_category = 'catalog channel'}
catalog_total_sales_2002 is total_sales { where: date_dim.d_year = 2002 and channel_category = 'catalog channel'}
web_total_sales_2001 is total_sales { where: date_dim.d_year = 2001 and channel_category = 'web channel'}
@carlineng
carlineng / tpcds36.malloy
Created February 23, 2023 03:29
TPC-DS Query 36
import "tpcds.malloy"
query: store_sales + {
query: margin_agg is {
aggregate:
gross_margin is total_profit / total_ext_sales
}
} -> {
nest: margin_agg
nest: by_category is {
SELECT
YEAR(d_date) AS d_year
, MONTH(d_date) AS d_month
, ROUND(SUM(total_sales), 2) AS total_sales
FROM sales_by_date
GROUP BY ROLLUP (d_year, d_month)
ORDER BY d_year NULLS FIRST, d_month NULLS FIRST
@carlineng
carlineng / q07.malloy
Created December 23, 2022 22:15
TPC-DS Query 7
import "tpcds.malloy"
query: all_sales -> {
group_by:
item.i_item_id
aggregate:
avg_quantity
avg_list_price
avg_coupon_amt