Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Project1 #70

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,4 @@ dmypy.json

# Pyre type checker
.pyre/
scratch/
102 changes: 102 additions & 0 deletions Project1/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
# Project 1

### How many users do we have?
130

```sql
SELECT COUNT(user_guid) FROM dev_db.dbt_danieloutschoolcom.stg_postgres__users;
```

### On average, how many orders do we receive per hour?
5.416668

```sql
WITH orders_received_hourly as (
SELECT date_trunc('hour', created_at) as hour_received
, COUNT(*) as num_received_this_hour
FROM dev_db.dbt_danieloutschoolcom.stg_postgres__orders
GROUP BY 1
)

SELECT AVG(num_received_this_hour) as average_num_orders_received_hourly
FROM orders_received_hourly;
```

### On average, how long does an order take from being placed to being delivered?

93.4

```sql
with delivery_hours as
(
SELECT created_at
, delivered_at
, datediff(hour, created_at, delivered_at) as hours_to_deliver
FROM dev_db.dbt_danieloutschoolcom.stg_postgres__orders
WHERE status = 'delivered'
)

SELECT round(AVG(hours_to_deliver), 2)
FROM delivery_hours
;
```

### How many users have only made one purchase? Two purchases? Three+ purchases?
1 purchase = 25 users
2 purchases = 28 users
3 or more purchass = 71

```sql
WITH orders_per_user_table as (
SELECT user_guid
, COUNT(distinct order_guid) as orders_per_user
FROM dev_db.dbt_danieloutschoolcom.stg_postgres__orders
GROUP BY user_guid
)

SELECT orders_per_user
, COUNT(distinct user_guid) as users_with_this_many_orders
FROM orders_per_user_table
GROUP BY orders_per_user
;
```

```sql
WITH orders_per_user_table as (
SELECT user_guid
, COUNT(distinct order_guid) as orders_per_user
FROM dev_db.dbt_danieloutschoolcom.stg_postgres__orders
GROUP BY user_guid
),

user_order_counts as (
SELECT orders_per_user
, COUNT(distinct user_guid) as num_users_with_this_many_orders
FROM orders_per_user_table
GROUP BY orders_per_user
)

SELECT SUM(num_users_with_this_many_orders) as num_users_with_three_or_more_orders
FROM user_order_counts
WHERE orders_per_user >= 3
;
```

### Note: you should consider a purchase to be a single order. In other words, if a user places one order for 3 products, they are considered to have made 1 purchase.

### On average, how many unique sessions do we have per hour?

16.33

```sql
WITH unique_sessions_per_hour as (
SELECT date_trunc(hour, created_at) as created_hour
, COUNT(distinct session_guid) as sessions_per_hour
FROM dev_db.dbt_danieloutschoolcom.stg_postgres__events
GROUP BY created_hour
)

SELECT round(AVG(sessions_per_hour), 2)
FROM unique_sessions_per_hour
;
```
4 changes: 4 additions & 0 deletions greenery/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

target/
dbt_packages/
logs/
15 changes: 15 additions & 0 deletions greenery/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Welcome to your new dbt project!

### Using the starter project

Try running the following commands:
- dbt run
- dbt test


### Resources:
- Learn more about dbt [in the docs](https://docs.getdbt.com/docs/introduction)
- Check out [Discourse](https://discourse.getdbt.com/) for commonly asked questions and answers
- Join the [chat](https://community.getdbt.com/) on Slack for live discussions and support
- Find [dbt events](https://events.getdbt.com) near you
- Check out [the blog](https://blog.getdbt.com/) for the latest news on dbt's development and best practices
Empty file added greenery/analyses/.gitkeep
Empty file.
38 changes: 38 additions & 0 deletions greenery/dbt_project.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@

# Name your project! Project names should contain only lowercase characters
# and underscores. A good package name should reflect your organization's
# name or the intended use of these models
name: 'greenery'
version: '1.0.0'
config-version: 2

# This setting configures which "profile" dbt uses for this project.
profile: 'greenery'

# These configurations specify where dbt should look for different types of files.
# The `model-paths` config, for example, states that models in this project can be
# found in the "models/" directory. You probably won't need to change these!
model-paths: ["models"]
analysis-paths: ["analyses"]
test-paths: ["tests"]
seed-paths: ["seeds"]
macro-paths: ["macros"]
snapshot-paths: ["snapshots"]

target-path: "target" # directory which will store compiled SQL files
clean-targets: # directories to be removed by `dbt clean`
- "target"
- "dbt_packages"


# Configuring models
# Full documentation: https://docs.getdbt.com/docs/configuring-models

# In this example config, we tell dbt to build all models in the example/
# directory as views. These settings can be overridden in the individual model
# files using the `{{ config(...) }}` macro.
models:
greenery:
# Config indicated by + and applies to all files under models/example/
example:
+materialized: view
Empty file added greenery/macros/.gitkeep
Empty file.
27 changes: 27 additions & 0 deletions greenery/models/example/my_first_dbt_model.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

/*
Welcome to your first dbt model!
Did you know that you can also configure models directly within SQL files?
This will override configurations stated in dbt_project.yml

Try changing "table" to "view" below
*/

{{ config(materialized='table') }}

with source_data as (

select 1 as id
union all
select null as id

)

select *
from source_data

/*
Uncomment the line below to remove records with null `id` values
*/

-- where id is not null
6 changes: 6 additions & 0 deletions greenery/models/example/my_second_dbt_model.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

-- Use the `ref` function to select from other models

select *
from {{ ref('my_first_dbt_model') }}
where id = 1
20 changes: 20 additions & 0 deletions greenery/models/example/schema.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

version: 2

models:
- name: my_first_dbt_model
description: "A starter dbt model"
columns:
- name: id
description: "The primary key for this table"
tests:
- unique

- name: my_second_dbt_model
description: "A starter dbt model"
columns:
- name: id
description: "The primary key for this table"
tests:
- unique
- not_null
Loading