Skip to content

Instantly share code, notes, and snippets.

@carlineng
carlineng / q06.malloy
Last active December 24, 2022 01:44
TPC-DS Query 6
import "tpcds.malloy"
source: category_avg is from(
item -> {
group_by:
i_category
aggregate:
avg_category_price is avg(i_current_price)
}
@carlineng
carlineng / q05.malloy
Last active December 22, 2022 05:41
TPC-DS Query 5
import "tpcds.malloy"
sql: daily_profit_loss_sql is {
select: """
SELECT * FROM (%{ all_sales -> daily_profit_loss }%)
UNION ALL
SELECT * FROM (%{ all_returns -> daily_profit_loss }%)
"""
connection: "duckdb"
}
@carlineng
carlineng / queries.sql
Last active December 8, 2022 01:33
Nested Query Example
-- Option 1:
-- Multiple sub-queries joined together:
WITH yearly_sales AS (
SELECT
YEAR(d_date) AS d_year
, ROUND(SUM(total_sales), 2) AS yearly_sales
FROM sales_by_date
GROUP BY 1
),
@carlineng
carlineng / q04.malloy
Last active December 4, 2022 07:01
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 sale_type = 'c'}
catalog_total_sales_2002 is total_sales { where: date_dim.d_year = 2002 and sale_type = 'c'}
web_total_sales_2001 is total_sales { where: date_dim.d_year = 2001 and sale_type = 'w'}
web_total_sales_2002 is total_sales { where: date_dim.d_year = 2002 and sale_type = 'w'}
store_total_sales_2001 is total_sales { where: date_dim.d_year = 2001 and sale_type = 's'}
store_total_sales_2002 is total_sales { where: date_dim.d_year = 2002 and sale_type = 's'}
@carlineng
carlineng / q03.malloy
Created December 3, 2022 13:48
TPC-DS Query 3
query: store_sales -> {
group_by:
d_year is date_dim.d_year
brand_id is item.i_brand_id
brand is item.i_brand
aggregate:
total_sales_price
where:
@carlineng
carlineng / q02.malloy
Last active December 2, 2022 01:40
TPC-DS Query 2
query: yoy_weekly_sales is weekly_sales {
join_many: date_dim on d_week_seq = date_dim.d_week_seq
join_one: weekly_sales on d_week_seq = weekly_sales.d_week_seq - 53
} -> {
group_by:
d_week_seq
r_sun is sun_sales / weekly_sales.sun_sales
r_mon is mon_sales / weekly_sales.mon_sales
r_tue is tue_sales / weekly_sales.tue_sales
r_wed is wed_sales / weekly_sales.wed_sales
import "tpcds.malloy";
query: store_returns -> {
group_by:
sr_customer_sk
sr_store_sk
customer.c_customer_id
aggregate:
customer_total_returns
avg_store_return is all(customer_total_returns / count(distinct sr_customer_sk), sr_store_sk)

Keybase proof

I hereby claim:

  • I am carlineng on github.
  • I am carlineng (https://keybase.io/carlineng) on keybase.
  • I have a public key ASBnc3twEchumoCv2udoNgq015asXhcF5lI7mA-GNNSj-Ao

To claim this, I am signing this object: