Query builder distorting "PARTITION BY" when using more than one column

Alright, so I’m trying to use MySQL window functions. This is a snippet of what I’ve written:

$query = (new Query())
         ->select('SUM(vote) OVER (PARTITION BY id, date) as test')

Looking at the query builder, I get this output:

[select] => [
    0 => 'sum(vote) OVER (PARTITION BY (id'
    'test' => 'date))'
]

The database will throw a syntax error when trying to process this query.

If I just use one column, like this:

$query = (new Query())
         ->select('SUM(vote) OVER (PARTITION BY id) as test')

The output from the query builder looks like this:

[select] => [
    'test' => 'sum(vote) OVER (PARTITION BY id)'
]

And the database handles it just fine.

How do I input the two columns such that they are parsed properly? Or have a found a bug in the QueryBuilder?

Hi @QuantumPsi,

Try the following:

 ->select(['SUM(vote) OVER (PARTITION BY id, date) as test']);

Check the following section of the guide.

https://www.yiiframework.com/doc/guide/2.0/en/db-query-builder#select

Thanks!

I didn’t try this as I discovered the expression function to escaping. Thank you though!

1 Like