forked from malloydata/learn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transform.malloysql
70 lines (64 loc) · 1.45 KB
/
transform.malloysql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
>>>markdown
# Flatten the ecommerce dataset into unnested tables
>>>malloy
source: users is duckdb.table('old/users.parquet') {
rename: user_id is id
}
source: inventory_items is duckdb.table('old/inventory_items.parquet') {
accept: cost, created_at, sold_at, id, product_id
rename: inventory_item_id is id
}
source: products is duckdb.table('old/products.parquet') {
rename: product_id is id
}
source: order_items is duckdb.table('old/order_items.parquet') {
rename: item_id is id
join_one: inventory_items is inventory_items {
join_one: products on product_id = products.product_id
} on inventory_items.inventory_item_id = inventory_item_id
join_one: users on user_id = users.user_id
}
query: orders is order_items -> {
group_by:
order_id is order_id:::number,
status,
created_at
returned_at
delivered_at
shipped_at
user_id
}
query: items is order_items -> {
project:
item_id
order_id,
inventory_item_id
sale_price
inventory_items.product_id
}
>>>sql
-- connection: duckdb
COPY (
%{orders}%
) to 'data/orders.parquet'
>>>sql
COPY (
%{items}%
) to 'data/items.parquet'
>>>sql
COPY (
%{inventory_items ->{project: *}}%
) to 'data/inventory_items.parquet'
>>>sql
COPY (
%{products ->{project: *}}%
) to 'data/products.parquet'
>>>sql
COPY (
%{users ->{project: *}}%
) to 'data/users.parquet'
>>>sql
SELECT
*
FROM 'data/orders.parquet'
qualify row_number() over (partition by order_id) = 2