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

Format Query within Column List #170

Open
kfordaccela opened this issue May 27, 2021 · 2 comments
Open

Format Query within Column List #170

kfordaccela opened this issue May 27, 2021 · 2 comments
Labels
bug Something isn't working

Comments

@kfordaccela
Copy link

kfordaccela commented May 27, 2021

Describe the bug

When formatting a query that has a query within the column list

To Reproduce

python
.from sql_formatter.core import format_sql

print( format_sql( "SELECT Column1, Column2, (SELECT column3 from SubTable) Column 4 FROM TABLE" ) )

output:
SELECT_column1,
_______column2,
_______(SELECT_column3
_FROM___subtable)_column4
FROM___table

Expected behavior

SELECT
_____column1,
_____column2,
_____(
__________SELECT
_______________column3
__________FROM__subtable
_____)_column4
FROM table

Screenshots

Sorry for "_" but the 'code' in the editor was dropping formatting.

@kfordaccela kfordaccela added the bug Something isn't working label May 27, 2021
@PabloRMira
Copy link
Owner

Hi @kfordaccela , actually I'm still wondering if such a query is valid in the first place. What SQL dialect are you using?

What if subtable has more rows than table?

As a workaround I would propose you the more classical way with a join, e.g.

SELECT a.column1, 
       a.column2, 
       b.column3
FROM table as a
    LEFT JOIN subtable as b
        ON a.column1 = b.column1

@kfordaccela
Copy link
Author

I have been writing SQL for a number of years and recognize the fact that it would have been better to use a LEFT JOIN, however often in the use case that I provided the result would be a sum or an average that would have required a grouping or a partition by that the original coder wasn't in favor of for some reason so they did it as a sub-query within the list of columns to be pulled into the report.

I have been playing also with the following as a comparison, which does do the formatting as I suggested above.
https://zeroturnaround.github.io/sql-formatter/

The whole reason for carefully formatting the code for me is the fact that I'm using it to parse out the tables and columns that are used within the query so that I can create a list for further work within python. When formatted properly I'm able to find the table aliases much faster.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants