How to use "DISTINCT ON (field)" in Doctrine 2?

Viewed 8027

I know how to use "DISTINCT" in Doctrine 2, but I really need to use "DISTINCT ON (field)" and I don't know how to do this with the QueryBuilder.

My SQL query looks like:

SELECT DISTINCT ON (currency) currency, amount FROM payments ORDER BY currency

And this query works perfect, but I can't use it with the QueryBuilder. Maybe I could write this query on some other way?

3 Answers

INDEX BY can be used in DQL, allowing first result rows indexed by the defined string/int field to be overwritten by following ones with the same index:

SELECT 
    p.currency, 
    p.amount 
FROM Namespace\To\Payments p INDEX BY p.currency 
ORDER BY p.currency ASC

DQL - EBNF - INDEX BY

Related