Re: [sqlalchemy] Selecting across columns with func.min()

2013-08-20 Thread Michael Bayer
you have to consider that more than one row can have that same close, so it's a 
set of rows corresponding to that one close.  so to remain in terms of sets you 
need to use a join (or a subquery):

select prices.date from prices, (select min(price.close) as close from prices 
join company on  where ticker='AAPL' and date>=) as 
min_close
on prices.close = min_close.close

or if you can use LIMIT 1 then lele's idea works more easily



On Aug 19, 2013, at 8:09 PM, csdr...@gmail.com wrote:

> Hi all,
> 
> I've searched through the documentation and google on this and haven't been 
> able to find an answer. I have the following class:
> 
> class Price(Base):
> __tablename__ = "prices"
> id = Column(Integer, primary_key = True)
> company_id = Column(Integer, ForeignKey('companies.id'))
> date = Column(DateTime, nullable=False)
> close = Column(Float)
> 
> I'm trying to query Price for the minimum price during a certain period of 
> time. The query for that is:
> 
> session.query(func.min(Price.close)).join(Company).filter(and_(Company.ticker=="AAPL",
>  Price.date>=)).one()
> 
> But, how does one query the Price.date that corresponds to 
> func.min(Price.close)? I had thought I could do session.query(Price.date, 
> func.min(Price.close)... but that instead returned the first date in the 
> column alongside the min price. 
> 
> I also thought that I could do:
> 
> session.query(Price.date).join(Company).filter(Company.ticker=="AAPL").having(func.min(Price.adj_close)==low[0]).all()
> 
> But that returns the empty set for some reason. (Just as well--there has to 
> be an easier way to do this.)
> 
> Chris
> 
> -- 
> 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/groups/opt_out.



signature.asc
Description: Message signed with OpenPGP using GPGMail


[sqlalchemy] Selecting across columns with func.min()

2013-08-19 Thread csdrane
Hi all,

I've searched through the documentation and google on this and haven't been 
able to find an answer. I have the following class:

class Price(Base):
__tablename__ = "prices"
id = Column(Integer, primary_key = True)
company_id = Column(Integer, ForeignKey('companies.id'))
date = Column(DateTime, nullable=False)
close = Column(Float)

I'm trying to query Price for the minimum price during a certain period of 
time. The query for that is:

session.query(func.min(Price.close)).join(Company).filter(and_(Company.ticker=="AAPL",
 
Price.date>=)).one()

But, how does one query the Price.date that corresponds to 
func.min(Price.close)? I had thought I could do session.query(Price.date, 
func.min(Price.close)... but that instead returned the first date in the 
column alongside the min price. 

I also thought that I could do:

session.query(Price.date).join(Company).filter(Company.ticker=="AAPL").having(func.min(Price.adj_close)==low[0]).all()

But that returns the empty set for some reason. (Just as well--there has to 
be an easier way to do this.)

Chris

-- 
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/groups/opt_out.