Skip to content

Commit

Permalink
update slides 23 and 24 to include ARRAY-based SQL equivalents
Browse files Browse the repository at this point in the history
  • Loading branch information
agentm committed Mar 15, 2024
1 parent a5f820a commit f83e707
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
12 changes: 10 additions & 2 deletions TutDForSQLDevelopers/23.slidedata
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,16 @@ TutorialD (master/main): :showexpr (p : {suppliers := (sp rename {p# as pid} whe
│ │└────────┘ │
└────────┴──────────────────────────────┘

Not supported.

SELECT p."p#", ARRAY(SELECT sp."s#" FROM sp WHERE sp."p#" = p."p#") FROM p;

p# | array
----+---------------
P6 | {S1}
P3 | {S1}
P4 | {S1,S4}
P1 | {S2,S1}
P2 | {S2,S4,S3,S1}
P5 | {S4,S1}
P7 | {}

Show each product mapped to its suppliers. Note that "P7" does not have any suppliers; this is represented by an empty relation in the "suppliers" column.
17 changes: 15 additions & 2 deletions TutDForSQLDevelopers/24.slidedata
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,21 @@ TutorialD (master/main): :showexpr ((p : {suppliers := (sp rename {p# as pid} wh
│"P5" │"S1" │
└────────┴────────┘

Not supported.

WITH p_grouped AS (SELECT p."p#", ARRAY(SELECT sp."s#" FROM sp WHERE sp."p#" = p."p#") AS ss FROM p) SELECT "p#",UNNEST(ss) AS "s#" FROM p_grouped;

p# | s#
----+----
P6 | S1
P3 | S1
P4 | S1
P4 | S4
P1 | S2
P1 | S1
P2 | S2
P2 | S4
P2 | S3
P2 | S1
P5 | S4
P5 | S1

Map all products to their constituent suppliers (group), then flatten (ungroup) the mapping. Note that product "P7" is missing.

0 comments on commit f83e707

Please sign in to comment.