<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Query with distinct sort and alias produces error column not found in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/query-with-distinct-sort-and-alias-produces-error-column-not/m-p/15893#M10162</link>
    <description>&lt;P&gt;The code from above is worked in both cases.&lt;/P&gt;</description>
    <pubDate>Thu, 07 Oct 2021 20:31:45 GMT</pubDate>
    <dc:creator>Kotofosonline</dc:creator>
    <dc:date>2021-10-07T20:31:45Z</dc:date>
    <item>
      <title>Query with distinct sort and alias produces error column not found</title>
      <link>https://community.databricks.com/t5/data-engineering/query-with-distinct-sort-and-alias-produces-error-column-not/m-p/15890#M10159</link>
      <description>&lt;P&gt;I’m trying to use sql query on azure-databricks with distinct sort and aliases&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;SELECT DISTINCT album.ArtistId AS my_alias 
    FROM album ORDER BY album.ArtistId&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;The problem is that if I add an alias then I can not use not aliased name in the order by clause.&lt;/P&gt;&lt;P&gt;&lt;I&gt;ORDER BY album.ArtistId&lt;/I&gt; part produces an error. &lt;I&gt;ORDER BY my_alias works&lt;/I&gt;.&lt;/P&gt;&lt;P&gt;If I remove distinct it also works.&lt;/P&gt;&lt;P&gt;Seems like after Project step original column name is lost. That behavior is unexpected for SQL compared to other SQL dialects. And I can not find any documentation about it.&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;Error in SQL statement: AnalysisException: cannot resolve '`album.ArtistId`' given input columns: [my_alias]; line 2 pos 22;
    'Sort ['album.ArtistId ASC NULLS FIRST], true
    +- Distinct
       +- Project [ArtistId#2506 AS my_alias#2500]
          +- SubqueryAlias spark_catalog.chinook.album
             +- Relation[AlbumId#2504,Title#2505,ArtistId#2506] parquet&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Is there any way to make this query run as is, without modification maybe by changing some execution flags?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The query is generated by sqlachemy and works on other databases fine, I prefer to not edit it for databricks.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The schema used for testing is Chinook database.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The where clause works fine&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;SELECT DISTINCT album.ArtistId AS my_alias 
    FROM album WHERE album.ArtistId = 1&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 08 Sep 2021 11:47:43 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/query-with-distinct-sort-and-alias-produces-error-column-not/m-p/15890#M10159</guid>
      <dc:creator>Kotofosonline</dc:creator>
      <dc:date>2021-09-08T11:47:43Z</dc:date>
    </item>
    <item>
      <title>Re: Query with distinct sort and alias produces error column not found</title>
      <link>https://community.databricks.com/t5/data-engineering/query-with-distinct-sort-and-alias-produces-error-column-not/m-p/15892#M10161</link>
      <description>&lt;P&gt;Hi ,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;can you please try with the following.&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;%sql SELECT DISTINCT album.ArtistId AS my_alias 
    FROM album ORDER BY 1 desc&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;or &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;%sql SELECT DISTINCT album.ArtistId AS my_alias 
    FROM album ORDER BY my_alias desc&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 27 Sep 2021 17:14:38 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/query-with-distinct-sort-and-alias-produces-error-column-not/m-p/15892#M10161</guid>
      <dc:creator>User16752246494</dc:creator>
      <dc:date>2021-09-27T17:14:38Z</dc:date>
    </item>
    <item>
      <title>Re: Query with distinct sort and alias produces error column not found</title>
      <link>https://community.databricks.com/t5/data-engineering/query-with-distinct-sort-and-alias-produces-error-column-not/m-p/15893#M10162</link>
      <description>&lt;P&gt;The code from above is worked in both cases.&lt;/P&gt;</description>
      <pubDate>Thu, 07 Oct 2021 20:31:45 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/query-with-distinct-sort-and-alias-produces-error-column-not/m-p/15893#M10162</guid>
      <dc:creator>Kotofosonline</dc:creator>
      <dc:date>2021-10-07T20:31:45Z</dc:date>
    </item>
  </channel>
</rss>

