<?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 Window function using last/last_value with PARTITION BY/ORDER BY has unexpected results in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/window-function-using-last-last-value-with-partition-by-order-by/m-p/34811#M25528</link>
    <description>&lt;P&gt;Hi, I'm wondering if this is the expected behavior when using last or last_value in a window function? I've written a query like this:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;select
    col1,
    col2,
    last_value(col2) over (partition by col1 order by col2) as column2_last
from values
    (1, 10), (1, 11), (1, 12),
    (2, 20), (2, 21), (2, 22);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;In Snowflake I get the following results. The behavior feels right, since we're partitioning by col1 and ordering by col2 (&lt;A href="https://docs.snowflake.com/en/sql-reference/functions/last_value.html" alt="https://docs.snowflake.com/en/sql-reference/functions/last_value.html" target="_blank"&gt;https://docs.snowflake.com/en/sql-reference/functions/last_value.html&lt;/A&gt;).&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper" image-alt="Screen Shot 2021-11-18 at 12.48.25 PM"&gt;&lt;img src="https://community.databricks.com/t5/image/serverpage/image-id/2310iEB96F93811ECC5C0/image-size/large?v=v2&amp;amp;px=999" role="button" title="Screen Shot 2021-11-18 at 12.48.25 PM" alt="Screen Shot 2021-11-18 at 12.48.25 PM" /&gt;&lt;/span&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In Databricks, I get: &lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper" image-alt="Screen Shot 2021-11-18 at 12.48.32 PM"&gt;&lt;img src="https://community.databricks.com/t5/image/serverpage/image-id/2314i2AA355BBF9CFE639/image-size/large?v=v2&amp;amp;px=999" role="button" title="Screen Shot 2021-11-18 at 12.48.32 PM" alt="Screen Shot 2021-11-18 at 12.48.32 PM" /&gt;&lt;/span&gt;It seems like the ORDER BY doesn't get applied... Perhaps I am writing the query incorrectly?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Any insight into this behavior would be very helpful. Also, I'm curious why last and last_value are synonyms? &lt;A href="https://docs.databricks.com/sql/language-manual/functions/last.html" alt="https://docs.databricks.com/sql/language-manual/functions/last.html" target="_blank"&gt;https://docs.databricks.com/sql/language-manual/functions/last.html&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;</description>
    <pubDate>Thu, 18 Nov 2021 18:46:17 GMT</pubDate>
    <dc:creator>alexisjohnson</dc:creator>
    <dc:date>2021-11-18T18:46:17Z</dc:date>
    <item>
      <title>Window function using last/last_value with PARTITION BY/ORDER BY has unexpected results</title>
      <link>https://community.databricks.com/t5/data-engineering/window-function-using-last-last-value-with-partition-by-order-by/m-p/34811#M25528</link>
      <description>&lt;P&gt;Hi, I'm wondering if this is the expected behavior when using last or last_value in a window function? I've written a query like this:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;select
    col1,
    col2,
    last_value(col2) over (partition by col1 order by col2) as column2_last
from values
    (1, 10), (1, 11), (1, 12),
    (2, 20), (2, 21), (2, 22);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;In Snowflake I get the following results. The behavior feels right, since we're partitioning by col1 and ordering by col2 (&lt;A href="https://docs.snowflake.com/en/sql-reference/functions/last_value.html" alt="https://docs.snowflake.com/en/sql-reference/functions/last_value.html" target="_blank"&gt;https://docs.snowflake.com/en/sql-reference/functions/last_value.html&lt;/A&gt;).&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper" image-alt="Screen Shot 2021-11-18 at 12.48.25 PM"&gt;&lt;img src="https://community.databricks.com/t5/image/serverpage/image-id/2310iEB96F93811ECC5C0/image-size/large?v=v2&amp;amp;px=999" role="button" title="Screen Shot 2021-11-18 at 12.48.25 PM" alt="Screen Shot 2021-11-18 at 12.48.25 PM" /&gt;&lt;/span&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In Databricks, I get: &lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper" image-alt="Screen Shot 2021-11-18 at 12.48.32 PM"&gt;&lt;img src="https://community.databricks.com/t5/image/serverpage/image-id/2314i2AA355BBF9CFE639/image-size/large?v=v2&amp;amp;px=999" role="button" title="Screen Shot 2021-11-18 at 12.48.32 PM" alt="Screen Shot 2021-11-18 at 12.48.32 PM" /&gt;&lt;/span&gt;It seems like the ORDER BY doesn't get applied... Perhaps I am writing the query incorrectly?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Any insight into this behavior would be very helpful. Also, I'm curious why last and last_value are synonyms? &lt;A href="https://docs.databricks.com/sql/language-manual/functions/last.html" alt="https://docs.databricks.com/sql/language-manual/functions/last.html" target="_blank"&gt;https://docs.databricks.com/sql/language-manual/functions/last.html&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Thu, 18 Nov 2021 18:46:17 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/window-function-using-last-last-value-with-partition-by-order-by/m-p/34811#M25528</guid>
      <dc:creator>alexisjohnson</dc:creator>
      <dc:date>2021-11-18T18:46:17Z</dc:date>
    </item>
    <item>
      <title>Re: Window function using last/last_value with PARTITION BY/ORDER BY has unexpected results</title>
      <link>https://community.databricks.com/t5/data-engineering/window-function-using-last-last-value-with-partition-by-order-by/m-p/34813#M25530</link>
      <description>&lt;P&gt;yes indeed it looks like a bug with LAST_VALUE,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Min gives correct results:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper" image-alt="image.png"&gt;&lt;img src="https://community.databricks.com/t5/image/serverpage/image-id/2302i55CCAAC351678B02/image-size/large?v=v2&amp;amp;px=999" role="button" title="image.png" alt="image.png" /&gt;&lt;/span&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Max hmm only when desc is specified (shouldn't affect as max is max but only with descending sorting it works as should be):&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper" image-alt="image.png"&gt;&lt;img src="https://community.databricks.com/t5/image/serverpage/image-id/2299i2D77776F8BA9DD5A/image-size/large?v=v2&amp;amp;px=999" role="button" title="image.png" alt="image.png" /&gt;&lt;/span&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;for last_value is exactly like on your example (so nothing helps - I tried many ways).&lt;/P&gt;&lt;P&gt;@Kaniz Fatma​&amp;nbsp;maybe someone from inside databricks could look at it&lt;/P&gt;</description>
      <pubDate>Thu, 18 Nov 2021 20:44:55 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/window-function-using-last-last-value-with-partition-by-order-by/m-p/34813#M25530</guid>
      <dc:creator>Hubert-Dudek</dc:creator>
      <dc:date>2021-11-18T20:44:55Z</dc:date>
    </item>
    <item>
      <title>Re: Window function using last/last_value with PARTITION BY/ORDER BY has unexpected results</title>
      <link>https://community.databricks.com/t5/data-engineering/window-function-using-last-last-value-with-partition-by-order-by/m-p/34814#M25531</link>
      <description>&lt;P&gt;Agreed. Interesting find that `max` behaves funny when `desc` isn't specified. &lt;/P&gt;&lt;P&gt;I'd also note that `first_value` yields the correct result.&lt;/P&gt;&lt;P&gt;@Kaniz Fatma​&amp;nbsp;Is there a process for filing bugs in Databricks? It'd be great to get this fixed. &lt;/P&gt;</description>
      <pubDate>Thu, 18 Nov 2021 20:47:44 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/window-function-using-last-last-value-with-partition-by-order-by/m-p/34814#M25531</guid>
      <dc:creator>alexisjohnson</dc:creator>
      <dc:date>2021-11-18T20:47:44Z</dc:date>
    </item>
    <item>
      <title>Re: Window function using last/last_value with PARTITION BY/ORDER BY has unexpected results</title>
      <link>https://community.databricks.com/t5/data-engineering/window-function-using-last-last-value-with-partition-by-order-by/m-p/34815#M25532</link>
      <description>&lt;P&gt;Thank you for your help, @Hubert Dudek​&amp;nbsp;!&lt;/P&gt;</description>
      <pubDate>Thu, 18 Nov 2021 20:48:28 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/window-function-using-last-last-value-with-partition-by-order-by/m-p/34815#M25532</guid>
      <dc:creator>alexisjohnson</dc:creator>
      <dc:date>2021-11-18T20:48:28Z</dc:date>
    </item>
    <item>
      <title>Re: Window function using last/last_value with PARTITION BY/ORDER BY has unexpected results</title>
      <link>https://community.databricks.com/t5/data-engineering/window-function-using-last-last-value-with-partition-by-order-by/m-p/34817#M25534</link>
      <description>&lt;P&gt;@Alexis Johnson​&amp;nbsp;- Here is where you can open a support ticket:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;A href="https://help.databricks.com/s/contact-us" alt="https://help.databricks.com/s/contact-us" target="_blank"&gt;Contact Us&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 19 Nov 2021 16:38:49 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/window-function-using-last-last-value-with-partition-by-order-by/m-p/34817#M25534</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2021-11-19T16:38:49Z</dc:date>
    </item>
    <item>
      <title>Re: Window function using last/last_value with PARTITION BY/ORDER BY has unexpected results</title>
      <link>https://community.databricks.com/t5/data-engineering/window-function-using-last-last-value-with-partition-by-order-by/m-p/37526#M26382</link>
      <description>&lt;P&gt;For those stumbling across this; it seems LAST_VALUE emulates the same functionality as it does in SQL Server which does not, in most people's minds, have a proper row/range frame for the window. You can adjust it with the below syntax.&lt;/P&gt;&lt;P&gt;I understand last_value emulating what is in my mind a mistake from sql_server but I don't understand why last() (an alias for last_value) has to use the same window row range.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Carv_0-1689191879502.png" style="width: 400px;"&gt;&lt;img src="https://community.databricks.com/t5/image/serverpage/image-id/2844iC27A7C179128C54B/image-size/medium/is-moderation-mode/true?v=v2&amp;amp;px=400" role="button" title="Carv_0-1689191879502.png" alt="Carv_0-1689191879502.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;SELECT
    col1
  , col2
  , LAST_VALUE(col2) OVER (PARTITION BY col1 ORDER BY col2 ASC) as column2_last
  , LAST_VALUE(col2) OVER (PARTITION BY col1 ORDER BY col2 ASC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) as column2_last_unbound
  , FIRST_VALUE(col2) OVER (PARTITION BY col1 ORDER BY col2 ASC) as column2_first
  , FIRST_VALUE(col2) OVER (PARTITION BY col1 ORDER BY col2 DESC) as column2_first_reversed
FROM values
  (1,10), (1,12), (1,11),
  (2,20), (2,21), (2,22);&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 12 Jul 2023 20:00:31 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/window-function-using-last-last-value-with-partition-by-order-by/m-p/37526#M26382</guid>
      <dc:creator>Carv</dc:creator>
      <dc:date>2023-07-12T20:00:31Z</dc:date>
    </item>
    <item>
      <title>Re: Window function using last/last_value with PARTITION BY/ORDER BY has unexpected results</title>
      <link>https://community.databricks.com/t5/data-engineering/window-function-using-last-last-value-with-partition-by-order-by/m-p/170482#M56312</link>
      <description>&lt;P&gt;The behavior of LAST_VALUE with PARTITION BY and ORDER BY can be confusing when the window frame affects which row is considered the last value. I’ve found that checking the window frame definition carefully helps make these results easier to understand. For office &lt;A href="https://bthtint.com/commercial-window-tinting-san-antonio-texas/" target="_self"&gt;window film in San Antonio&lt;/A&gt;, similar attention to detail can help when choosing solutions for large glass spaces.&lt;/P&gt;</description>
      <pubDate>Sat, 03 Oct 2026 10:25:29 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/window-function-using-last-last-value-with-partition-by-order-by/m-p/170482#M56312</guid>
      <dc:creator>asbivu</dc:creator>
      <dc:date>2026-10-03T10:25:29Z</dc:date>
    </item>
  </channel>
</rss>

