Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3243484
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T18:26:20+00:00 2026-05-17T18:26:20+00:00

I want to generate this query in sqlalchemy. The table ‘demande’ exists in the

  • 0

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.

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;

I’ve tried this :

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

But it complains with an InvalidRequesError: Could not find a FROM clause to join from. I guess this is caused by the subquery.

If i try to “invert” the query, it works but it does a ‘LEFT OUTER JOIN’ :

q = session.query(
        func.count(Demande.id),
        stmt.c.timestep).
    outerjoin((stmt, grouped==stmt.c.timestep)).
    group_by(stmt.c.timestep)

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T18:26:21+00:00Added an answer on May 17, 2026 at 6:26 pm

    The following example should give you a clue (assuming I correctly guessed that Demande is declarative model):

    joined = stmt.outerjoin(Demande.__table__, Demande.grouped==stmt.c.timestep)
    q = session.query(stmt.c.timestep, func.count(Demande.id)).\
            select_from(joined).\
            group_by(stmt.c.timestep)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to display a table like this Generated from three database tables depends
I want to generate a query which generate ratio girls over boys. For this
I want to generate this SQL statement in LINQ: select * from Foo where
I have this problem: I want to generate a new source code file from
I want to generate an url like this: /photo?tags=tag1,tag2,tag3 routes file: GET /photo controllers.Photos.list(tags:Array[String]
Assume I want to generate an element similar to this in ASP.NET MVC 2:
I want to generate unique random, N-valued key. This key can contain numbers and
Basically, I want to generate an item, in this case, a ring. I have
I asked this question before: Generate XML from a class I want to do
I want to write method which generates key for cache object. This is my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.