Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to generate this query in sqlalchemy?
    primarykey
    data
    text
    <p>I want to generate this query in sqlalchemy. The table 'demande' exists in the database. There is a subquery that generates the timesteps with the generate_series function.</p> <pre><code>SELECT timesteps.timestep AS timestep, d.count AS count FROM (SELECT DATE_TRUNC('hour',date_demande) AS timestep, COUNT(id) AS count FROM demande GROUP BY timestep ) AS d RIGHT OUTER JOIN (SELECT timestep FROM generate_series('2010-01-01 00:00:00'::timestamp, '2010-01-01 23:59:59'::timestamp, '1 hour'::interval) AS timestep ) AS timesteps ON d.timestep = timesteps.timestep ORDER BY timestep; </code></pre> <p>I've tried this :</p> <pre><code>stmt = session.query( func. generate_series( datetime.datetime(2010,1,1,0,0,0), datetime.datetime(2010,1,1,23,59,59), cast('1 hour',Interval())). label('timestep') ).subquery() print stmt q = session.query( stmt.c.timestep, func.count(Demande.id)). outerjoin((Demande, grouped==stmt.c.timestep)). group_by(stmt.c.timestep) print q </code></pre> <p>But it complains with an InvalidRequesError: Could not find a FROM clause to join from. I guess this is caused by the subquery.</p> <p>If i try to "invert" the query, it works but it does a 'LEFT OUTER JOIN' :</p> <pre><code>q = session.query( func.count(Demande.id), stmt.c.timestep). outerjoin((stmt, grouped==stmt.c.timestep)). group_by(stmt.c.timestep) </code></pre> <p>As there is no RIGHT OUTER JOIN in sqlalchemy, I just want to find a way to take the subquery as the first table and the 'demande' table as the second one. This way I will be able to use LEFT OUTER JOIN</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload