Gracias por las recomendaciones, quedó en está forma:

subquery = (reporte_linea
            .join(reporte,
                condition=reporte.id == reporte_linea.reporte)
            .select(
                Max(reporte_linea.id * 1005).as_('id'),
                Max(reporte_linea.create_uid).as_('create_uid'),
                Max(reporte_linea.create_date).as_('create_date'),
                Max(reporte_linea.write_uid).as_('write_uid'),
                Max(reporte_linea.write_date).as_('write_date'),
                (reporte_linea.gp).as_('gp'),
                (reporte.distrito).as_('distrito'),
                where = where,
                group_by=(reporte_linea.gp,
                    reporte.distrito), 
                order_by=(reporte.distrito),
                )
            )
        query = (gp
            .join(subquery,'LEFT',
            condition=gp.id == subquery.gp)
            .select(
                Max(gp.id * 1005).as_('id'),
                Max(gp.create_uid).as_('create_uid'),
                Max(gp.create_date).as_('create_date'),
                Max(gp.write_uid).as_('write_uid'),
                Max(gp.write_date).as_('write_date'),
                (gp.id).as_('gp'),
                (subquery.distrito).as_('distrito'),
                where= subquery.gp == None,
                group_by=(gp.id, subquery.distrito,),
                order_by=(subquery.distrito)
                )
            )

El problema es que ahora no puedo indexar dicha tabla en el método .search()

Reporte = Pool().get('disc.reporte.lider.cero.table')

reports = Reporte.search( [('distrito','=',distrito)] )

También en los reportes no puedo utilizar, como por ejemplo: 
gp.distrito.zona (en otros reportes funciona bien, pero no después de la 
doble consulta).

Hay alguna recomendación para trabajar con este tipo de datos? 

El jueves, 1 de febrero de 2018, 6:29:02 (UTC-6), Sergi Almacellas Abellana 
escribió:
>
> El 01/02/18 a les 04:57, Josias Pérez ha escrit: 
> > Hola,  
> > 
> > Tengo el siguiente método de table_query y las siguientes tablas:  
> > 
> > class Gp: 
> >      name  
> > 
> > class Reporte 
> >      fecha_inicio 
> >      fecha_fin 
> >      distrito 
> > 
> > class ReporteLine 
> >      reporte many2one Reporte 
> >      gp        many2one Gp 
> >      cantidad  
> > 
> > Deseo encontrar los grupos que no generan reportes, es decir sean NULL o 
> > cero. Si existe un registro en la clase ReporteLine se puede sumar 
> > (aunque sea 0), pero si no existe un registro no sé como obtener el 
> > nombre del grupo (Gp) que no está registrado en ReporteLine.  
> > 
> > Tengo el siguiente table_query pero devuelve un valor de error al 
> > realizar la consulta y devuelve el siguiente error  
> > ProgrammingError: syntax error at or near ")" 
>
> Por el error parece que te falta/sobra algun parentesis. 
>
> De todos modos, si nos pones la traza completa mejor. 
> > 
> > Cualquier comentario será de mucha ayuda.  
> > 
> > def table_query(): 
> >     pool = Pool() 
> >     context = Transaction().context 
> >     Gp = pool.get('disc.gp') 
> >     gp = Gp.__table__() 
> >     Reporte = pool.get('disc.reporte') 
> >     reporte = Reporte.__table__() 
> >     ReporteLinea = pool.get('disc.reporte.linea') 
> >     reporte_linea = ReporteLinea.__table__() 
> >      
> >     where = Literal(True) 
> >     if Transaction().context.get('fecha_inicio'): 
> >         where &= reporte.fecha_inicio >= 
> > Transaction().context['fecha_inicio'] 
> >     if Transaction().context.get('fecha_fin'): 
> >         where &= reporte.fecha_fin <= Transaction().context['fecha_fin'] 
> >      
> >     where &= gp.id == None 
>
> Una sugerencia: En vez de instanciar el context varias veces es mejor 
> hacer: 
>
> context = Transaction().context 
>
> y a partir d'aqui context.get('fecha_inicio') 
>
> Queda más limpio. 
>
> > 
> >     query1 = reporte.join(reporte_linea, 
> >             condition=reporte_linea.reporte == reporte.id).select( 
> >                 Max(reporte_linea.id * 1000).as_('id'), 
> >                 Max(reporte.create_uid).as_('create_uid'), 
> >                 Max(reporte.create_date).as_('create_date'), 
> >                 Max(reporte.write_uid).as_('write_uid'), 
> >                 Max(reporte.write_date).as_('write_date'), 
> >                 Max(reporte.distrito).as_('distrito'), 
> >                 (reporte_linea.gp).as_('gp'), 
> >                 Sum(reporte_linea.cantidad).as_('total'), 
> >                 group_by=(reporte_linea.gp), 
> >                 ) 
> >      
> >     query2 = gp.join(query1,'LEFT').select( 
> >             Max(gp.id * 1001).as_('id'), 
> >             Max(gp.create_uid).as_('create_uid'), 
> >             Max(gp.create_date).as_('create_date'), 
> >             Max(gp.write_uid).as_('write_uid'), 
> >             Max(gp.write_date).as_('write_date'), 
> >             Max(query1.distrito).as_('distrito'), 
> >             (reporte_linea.gp).as_('gp'), 
> >             where = where, 
> >             group_by=(query1.gp), 
> >     ) 
>
>
> -- 
> Sergi Almacellas Abellana 
> www.koolpi.com 
> Twitter: @pokoli_srk 
>
 

Responder a