[sqlalchemy] Newest records per category

2010-10-11 Thread Sebastian Elsner

Hello,

I have one table called 'Assets' with a 'category' (String) and 
'created' (DateTime) column. Now I would like to find the records 
created since a given datetime for each category:



This is what I thought would work (with a self-join):

session.query(Asset).join(Asset, and_(Asset.category == Asset.category, 
Asset.created  specifiedDateTime)


But it does error with 'Cant find any foreign key relationships...'

How can I fix this? Or do you have a better idea how to accomplish the task?

Thank you,

Sebastian

--
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalch...@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.



Re: [sqlalchemy] Newest records per category

2010-10-11 Thread Mark Erbaugh

On Oct 11, 2010, at 7:50 AM, Sebastian Elsner wrote:

  have one table called 'Assets' with a 'category' (String) and 'created' 
 (DateTime) column. Now I would like to find the records created since a given 
 datetime for each category:
 
 
 This is what I thought would work (with a self-join):
 
 session.query(Asset).join(Asset, and_(Asset.category == Asset.category, 
 Asset.created  specifiedDateTime)
 
 But it does error with 'Cant find any foreign key relationships...'
 
 How can I fix this? Or do you have a better idea how to accomplish the task?


You probably don't need the self join?  You can filter on multiple conditions.

session.query(Asset).filter(Asset.category == 
spefiiedCateogory).filter(Asset.created  specifiedDateTime)

or the equivalent using the and_ function

session.query(Asset).filter(and_(Asset.category == specifiedCategory, 
Asset.created  specifiedDateTime))

Mark

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalch...@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.



Re: [sqlalchemy] Newest records per category

2010-10-11 Thread Sebastian Elsner
You are actually right. There was a logical mistake in my request, so I 
have to rephrase it:


Having a table Assets with columns category (String) and created 
(DateTime), I would like to get the newest n records for each category.


On 10/11/2010 04:30 PM, Mark Erbaugh wrote:


On Oct 11, 2010, at 7:50 AM, Sebastian Elsner wrote:


have one table called 'Assets' with a 'category' (String) and
'created' (DateTime) column. Now I would like to find the records
created since a given datetime for each category:


This is what I thought would work (with a self-join):

session.query(Asset).join(Asset, and_(Asset.category ==
Asset.category, Asset.created  specifiedDateTime)

But it does error with 'Cant find any foreign key relationships...'

How can I fix this? Or do you have a better idea how to accomplish the
task?



You probably don't need the self join? You can filter on multiple
conditions.

session.query(Asset).filter(Asset.category ==
spefiiedCateogory).filter(Asset.created  specifiedDateTime)

or the equivalent using the and_ function

session.query(Asset).filter(and_(Asset.category == specifiedCategory,
Asset.created  specifiedDateTime))

Mark

--
You received this message because you are subscribed to the Google
Groups sqlalchemy group.
To post to this group, send email to sqlalch...@googlegroups.com.
To unsubscribe from this group, send email to
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/sqlalchemy?hl=en.


--
Sebastian Elsner - Pipeline TD - r i s e | fx

t: +49 30 201 803 00 sebast...@risefx.com
c: +49 175 336 5739 7548 www.risefx.com

r i s e | fx GmbH
Schlesische Strasse 28, Aufgang B 10997 Berlin
Richard-Byrd-Strasse 12, 50829 Cologne
Geschaeftsfuehrer: Sven Pannicke, Robert Pinnow
Handelsregister Berlin HRB 106667 B

--
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalch...@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.