select distinct on (album_id) *
from tracks
order by album_id, milliseconds desc;A standard* method would be:
SELECT *
FROM tracks
QUALIFY row_number() over (partition by album_id order by milliseconds desc) = 1;
But the QUALIFY clause is so new that it doesn't work on most RDBMSs. If you're on MS SQL Server, you're still using: SELECT *
FROM (
SELECT *
,row_number() over (partition by album_id order by milliseconds desc) rn
FROM tracks
) x
WHERE x.rn = 1;
That said, I still don't think PRQL is particularly amazing. I can't tell if it's merely syntactic sugar for SQL, or if it's actually meant to control query execution. If it's the former, it's likely to frustrate developers because it's actually just another layer of abstraction. If it's the latter, then it requires the developer to not only understand the data model well enough to be able to write SQL queries, they need to be able to understand the RDBMS impementation details well enough to be able to write queries that best take advantage of the current database's indexes, statistics, and configuration. Even something as simple as sorting before filtering or projecting can be a significant performance issue. Nevermind the fact that relational algebra done in the wrong order can be non-deterministic or not equivalent transformations, so even if the query processor is smart enough to do rewrites whatever the developer enters might be logically different unintentionally.Ultimately I think it's a tool that lets the developer thinking about the problem in the way they prefer, rather than thinking about the problem in the way that best suits the problem at hand. Like insisting on writing documentation in LaTeX instead of Word or Markdown.
*: I believed this was in SQL 2023, but double checking it looks like it did not have make the final standard. I would be surprised if it didn't make it in the future, however.
Since when is QUALIFY part of the SQL standard? So far I have only seen it as a proprietary feature in Terradata.
And even then, it'll be another 6 years before your application vendor finally upgrades to it.
And even then, it'll be another 6 years before the database feature is allowed to be enabled.
And even then, your reporting software won't support it.
It might be that the question was "the longest track across all albums" which indeed would require a LIMIT