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

writing large SQL queries #1

Closed
edublancas opened this issue Jul 27, 2022 · 1 comment
Closed

writing large SQL queries #1

edublancas opened this issue Jul 27, 2022 · 1 comment

Comments

@edublancas
Copy link

edublancas commented Jul 27, 2022

The best way to write large SQL queries is via CTEs:

WITH something AS (
  SELECT * FROM some_table
  WHERE column = 1
)
SELECT *
FROM something
WHERE another_column = 2

However, when writing CTEs, we lose the ability to run the sub-expressions interactively.

We could add something like this:

%%sql --name something
SELECT * FROM some_table
WHERE column = 1

Then, to re-use the previous query:

%%sql
SELECT *
-- refer to the previous query
FROM ':something'
WHERE another_column = 2

And have the extension automatically expand the expression above to:

WITH something AS (
  SELECT * FROM some_table
  WHERE column = 1
)
SELECT *
FROM something
WHERE another_column = 2

Before submitting to the database.

@vikjam
Copy link

vikjam commented Aug 1, 2022

Copying Slack thread over to this GitHub issue.

Instead of converting a subquery to a CTE for the user, it might be more explicit to have them specify the CTEs in the magics function. For example,

%%sql --with something
SELECT *
FROM something
WHERE another_column = 2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants