You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

81 lines
1.5 KiB

2 years ago
2 years ago
  1. SQL practices
  2. ===
  3. ### ToC
  4. - [Practice 1](#practice-1)
  5. - [Practice 2](#practice-2)
  6. - [Practice 3](#practice-3)
  7. # Practice 1
  8. - Count the number of subordinate 'ENTERPRISE\_CUSTOMER' organizations for each organization.
  9. ```sql
  10. SELECT
  11. COUNT(customers.*) AS "subordinates_count",
  12. organizations.id AS "id"
  13. FROM organizations
  14. LEFT OUTER JOIN enterprise_sales_enterprise_customers customers ON customers.sales_organization_id = organizations.id
  15. GROUP BY organizations.id
  16. ORDER BY organizations.id ASC
  17. ;
  18. ```
  19. # Practice 2
  20. - Calculate the center of each `japan_segment`.
  21. ```sql
  22. SELECT
  23. id,
  24. ST_X(ST_Centroid(bounds)) AS "longitude",
  25. ST_Y(ST_Centroid(bounds)) AS "latitude"
  26. FROM japan_segments
  27. ;
  28. ```
  29. # Practice 3
  30. - Select `japan_segments` within the bounds represented as the following GeoJSON.
  31. ```json
  32. {
  33. "type": "FeatureCollection",
  34. "features": [
  35. {
  36. "type": "Feature",
  37. "properties": {},
  38. "geometry": {
  39. "type": "Polygon",
  40. "coordinates": [
  41. [
  42. [
  43. 130.27313232421875,
  44. 30.519681272749402
  45. ],
  46. [
  47. 131.02020263671875,
  48. 30.519681272749402
  49. ],
  50. [
  51. 131.02020263671875,
  52. 30.80909017893796
  53. ],
  54. [
  55. 130.27313232421875,
  56. 30.80909017893796
  57. ],
  58. [
  59. 130.27313232421875,
  60. 30.519681272749402
  61. ]
  62. ]
  63. ]
  64. }
  65. }
  66. ]
  67. }
  68. ```
  69. ![Kagoshima segments](./img/kagoshima_segments.png)