Postgres join example

broken image
broken image

You can tell already from all the repeated 80’s that you may run into problems with double-counting… But wait! Symmetric Aggregates! Here is the result of that fan-out, omitting duplicate or id columns for legibility: This animation shows how that result set is built, and we can see the fan-out in action: LEFT JOIN managers ON managers.account=accounts.idīefore we fill in the rest of the query, let’s visualize the result set before any grouping or aggregation happens.

broken image

LEFT JOIN products ON products.account=accounts.id Assuming you had used the above explore definition, Looker would produce a query of the form. Let’s say a business user now wants to get counts of products and of managers by account. We informally call this situation “fan-out”. You probably already know that in SQL, this can lead to some unexpected results. Something along the lines of: explore accounts, left join products (1:M on accounts), left join managers (1:M on accounts). It feels very natural to write SQL like FROM accounts LEFT JOIN products or like FROM accounts LEFT JOIN managers, so many LookML developers default to writing an explore this way too. (For the time being, we’ll show de-normalized account names to make the examples easier to read) Let’s imagine we have the following three tables. Note: Although everything demonstrated in here is basic SQL, there are notable dialect considerations.

broken image