from sql import Table, Window
from sql.aggregate import Sum
from sql.conditionals import Coalesce
table = Table('table')
columns = [
Coalesce(
table.name1,
table.name2,
).as_('name'),
Sum(
table.debit - table.credit,
window=Window(
[],
order_by=(
# The next line is not ok:
table.name.asc,
)
)
).as_('sum'),
]
select = table.select(*columns)
tuple(select)
It results in:
('SELECT COALESCE("a"."name1", "a"."name2") AS "name", SUM(("a"."debit" - "a"."credit")) OVER "b" AS "sum" FROM "table" AS "a" WINDOW "b" AS ( ORDER BY "a"."name" ASC)', ())
The COALESCE column is correctly aliased as "name".
The result should look like:
('SELECT COALESCE("a"."name1", "a"."name2") AS "name", SUM(("a"."debit" - "a"."credit")) OVER "b" AS "sum" FROM "table" AS "a" WINDOW "b" AS ( ORDER BY
"name" ASC)', ())
TIA and regards
Udo
On 2016-03-01 11:19, 'udono' via python-sql wrote:
> But how can I use the "name" column in the WINDOW ORDER BY expression,
> without using a Table() instance (table.name) which results in the wrong
> table prefix "a"?
You can not, you must duplicate the expression.
On 2016-03-01 23:11, 'udono' via python-sql wrote:
> Am Mittwoch, 2. März 2016 07:44:17 UTC+1 schrieb udono:
> > Am Mittwoch, 2. März 2016 00:40:02 UTC+1 schrieb Cédric Krier:
> >> On 2016-03-01 11:19, 'udono' via python-sql wrote:
> >> > But how can I use the "name" column in the WINDOW ORDER BY expression,
> >> > without using a Table() instance (table.name) which results in the
> >> wrong
> >> > table prefix "a"?
> >> You can not, you must duplicate the expression.
> > What does it mean, "duplicate the expression"?
> Ok, I found it:
> > order_by=(
> > table.name.as_('name').asc,
> > )
This is wrong even if by change it works.
You must use:
order_by=(Coalesce(table.name1, table.name2).asc)