Le jeudi 19 juin 2014 16:10:19 UTC+3, Michael Bayer a écrit :
>
>
> On 6/19/14, 4:09 AM, Mike Solomon wrote: 
> > 
> > 
> > 
> > It's difficult to issue a straight SQL string for the hybrid property 
> > itself because 
>
>
> sorry, I meant, please write the query *that you really want* as a SQL 
> string.   Don't use SQLAlchemy.   It's better to work in that direction. 
>
> If you don't know what the SQL you want is, that's a different issue, 
> I'd start on that part first. 
>

Ah, OK.  The SQL below will group fractions based on tag and give the 
num(erator) and den(ominator) of the maximum for each group. In Python, I'd 
like to have a fraction class that has members tag and val where val is a 
hybrid_property combining num and den.   I'd like to be able to do a query 
like session.query(Fraction.tag, 
func.max(Fraction.val)).group_by(Fraction.tag) and get the SQL below:

CREATE TABLE fraction (
  id Int,
  tag Int,
  num Int,
  den Int,
  PRIMARY KEY (id)
);

SELECT DISTINCT fraction_a.tag, fraction_a.high,
  fraction_b.num, fraction_b.den
FROM
  (SELECT fraction.tag, max(1.0 * fraction.num / fraction.den) AS high
    FROM fraction
    GROUP BY fraction.tag)
  AS fraction_a JOIN
  (SELECT fraction.tag, fraction.num, fraction.den
    FROM fraction)
  AS fraction_b
  ON fraction_a.tag = fraction_b.tag
    AND fraction_a.high = 1.0 * fraction_b.num / fraction_b.den; 

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to