2 Commits

  1. 2
      README.md
  2. 20
      docs/practices.md
  3. 15
      test/test_example.py

2
README.md

@ -1,6 +1,8 @@
SQL test SQL test
=== ===
Nathan's SQL answers. Tests past in `test/test_example.py`
### Prerequisistes ### Prerequisistes
```bash ```bash

20
docs/practices.md

@ -11,10 +11,30 @@ SQL practices
- Count the number of subordinate 'ENTERPRISE\_CUSTOMER' organizations for each organization. - Count the number of subordinate 'ENTERPRISE\_CUSTOMER' organizations for each organization.
```sql
SELECT
COUNT(customers.*) AS "subordinates_count",
organizations.id AS "id"
FROM organizations
LEFT OUTER JOIN enterprise_sales_enterprise_customers customers ON customers.sales_organization_id = organizations.id
GROUP BY organizations.id
ORDER BY organizations.id ASC
;
```
# Practice 2 # Practice 2
- Calculate the center of each `japan_segment`. - Calculate the center of each `japan_segment`.
```sql
SELECT
id,
ST_X(ST_Centroid(bounds)) AS "longitude",
ST_Y(ST_Centroid(bounds)) AS "latitude"
FROM japan_segments
;
```
# Practice 3 # Practice 3
- Select `japan_segments` within the bounds represented as the following GeoJSON. - Select `japan_segments` within the bounds represented as the following GeoJSON.

15
test/test_example.py

@ -43,6 +43,14 @@ class TestExample(DbTest):
) )
sql = """ sql = """
SELECT
COUNT(customers.*) AS "subordinates_count",
organizations.id AS "id"
FROM organizations
LEFT OUTER JOIN enterprise_sales_enterprise_customers customers ON customers.sales_organization_id = organizations.id
GROUP BY organizations.id
ORDER BY organizations.id ASC
;
""" """
with conn.cursor(cursor_factory=RealDictCursor) as cur: with conn.cursor(cursor_factory=RealDictCursor) as cur:
cur.execute(sql) cur.execute(sql)
@ -87,7 +95,14 @@ class TestExample(DbTest):
os.path.join(PATH_TO_SQL_DIR, "japan_segments.sql") os.path.join(PATH_TO_SQL_DIR, "japan_segments.sql")
) )
# PostGIS is very useful!
sql = """ sql = """
SELECT
id,
ST_X(ST_Centroid(bounds)) AS "longitude",
ST_Y(ST_Centroid(bounds)) AS "latitude"
FROM japan_segments
;
""" """
with conn.cursor(cursor_factory=RealDictCursor) as cur: with conn.cursor(cursor_factory=RealDictCursor) as cur:
cur.execute(sql) cur.execute(sql)

Loading…
Cancel
Save