El 20/11/15 a les 09:37, Hendrik Brandes ha escrit:
> Hello,
>
> I am using python-sql within tryton und want to create a query, which
> selects data across several tables using joins.
>
> My problem is, that in tryton the ID-field is necessary and I cannot
> create a proper-ID field, which corresponds to the group by clause, e.g.
> which will not generate double entries.
>
> I thought about using the RowNumber-function to create a individual ID
> for each line. But RowNumber is a WindowsFunction and I cannot see, how
> to use this.
>
> My Select-Statements sounds like this:
>
> |
> line.join(product,'LEFT',condition=line.product ==
product.id
> ).join(product_template,'LEFT',condition=product.template==
product_template.id
> ).join(category,'LEFT',condition=product_template.category ==
category.id
> ).join(sale,'LEFT',condition=line.sale ==
sale.id
> ).join(prop,'LEFT',condition=
product_template.id
> ==ToNumber(Substring(prop.res,18),'999999')
> ).select(
> #Max( product_template.category * 1000000 ).as_("id"),
> Max(line.create_uid).as_('create_uid'),
> Max(line.create_date).as_('create_date'),
> Max(line.write_uid).as_('write_uid'),
> Max(line.write_date).as_('write_date'),
> year_column,
> month_column,
> product_template.category,
> Sum(line.quantity).as_('quantity'),
> Sum(line.unit_price *line.quantity).as_('untaxed_amount'),
> Sum(line.unit_price *line.quantity -line.quantity
> *ToNumber(prop.value,'9999.999')).as_('rendite'),
> where=(prop.field==778),
>
> group_by=(year_column,month_column,product_template.category))
> |
>
>
>
> At the point Max( product_template.category ), I want to have the
> RowNumber Function.
>
You can create your own functions with:
from sql.functions import Function
class RowNumber(Function):
__slots__ ()
_function = 'ROW_NUMBER'
and then call it normaly:
table.select(RowNumber())
will produce:
SELECT ROW_NUMBER() FROM table;
Hope it helps!
> Thank you and best regards,
> Hendrik Brandes
--
Sergi Almacellas Abellana
www.koolpi.com
Twitter: @pokoli_srk