Share via

Max function

Bullard, Cory E 1 Reputation point
2022-03-10T23:26:23.403+00:00

I'm new to Databricks SQL. I'm trying to use the Max function but it's not working like it typically does in SQL Server. I would normally do something like this:

select ID
, max(QTY) as QTY

from table

group by ID

but Azure is making me put QTY in the group by and even then it's not working

Azure SQL Database
0 comments No comments

2 answers

Sort by: Most helpful
  1. Alberto Morillo 35,501 Reputation points MVP Volunteer Moderator
    2022-03-10T23:55:17.017+00:00

    If you are using PySpark with Databricks, then you will find here examples of how to use not only Max aggregate funcions but other functions to.

    Put all values in an array, explode it and then max that. For example:

    %sql
    WITH cte AS
    (
    SELECT *, EXPLODE( ARRAY( date1, date2 ) ) xmax 
    FROM tmp
    )
    SELECT MAX( xmax )
    FROM cte
    
    1 person found this answer helpful.
    0 comments No comments

  2. Bullard, Cory E 1 Reputation point
    2022-03-11T14:16:07.187+00:00

    Appreciate you. I've never heard of Max Array! I'll definitely look into it! And, I'm not sure what PySpark is. I just know it looks like SQL. lol. I have years of SQL Server experience and the syntax for this code is different. I'm just used to the Max example that I gave above.

    So, I don't need to use a Group By?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.