question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Doesn't Differentiate Using Aliases in Group By

See original GitHub issue

The test that I used was:

sql = """SELECT
    table_schema
    , CASE WHEN table_schema = 'my_table' THEN 1 ELSE 0 END as test
    , COUNT(*)

FROM information_schema.columns
GROUP BY
    table_schema
    , test
LIMIT 10 """
print(sqlglot.transpile(sql, read='postgres', write='oracle', identify=True, pretty=True)[0])

And what it returned was:

SELECT
  "table_schema",
  CASE
    WHEN "table_schema" = 'my_table'
    THEN 1
    ELSE 0
  END AS "test",
  COUNT(*)
FROM "information_schema"."columns"
GROUP BY
  "table_schema",
  "test"
LIMIT 10

Referencing aliases in the group by is not supported in Oracle SQL, so this returns an error

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
tobymaocommented, Aug 8, 2022

@barakalon sounds good to me, should i reopen this issue?

0reactions
vldt-aemcommented, Aug 9, 2022

@tobymao @barakalon Awesome, thank you!

Read more comments on GitHub >

github_iconTop Results From Across the Web

SQL - using alias in Group By - Stack Overflow
For most relational database systems, this order explains which names (columns or aliases) are valid because they must have been introduced in a...
Read more >
Use column alias in GROUP BY - DBA Stack Exchange
Aliases can be used only if they were introduced in the preceding step. So aliases in the SELECT clause can be used in...
Read more >
Why doesn't SQL allow using ALIAS in group by? - Quora
One solution is to define aliases in a subquery, then they can be used anywhere in the main query.
Read more >
Is it possible to alias a GROUP BY expression? If not, I don't ...
One major concern with aliases for example is that you then need some kind of name resolution preference - to decide whether to...
Read more >
SQL: ALIASES - TechOnTheNet
It is acceptable to use spaces when you are aliasing a column name. However, it is not generally good practice to use spaces...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found