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

  • Home
  • SEARCH
  • 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 8469197
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T16:09:22+00:00 2026-06-10T16:09:22+00:00

I have two tables, event and event_exception. Table event has a (boolean) column regular

  • 0

I have two tables, “event” and “event_exception”. Table “event” has a (boolean) column “regular” that indicates whether the event is to occur regularly every year or not. Table “event_exception” has a column “event_id”, a column “year” and a (boolean) column “occurs”.

The data is to be interpreted this way:

  • Some events occur regularly every year, but sometimes they exceptionally don’t, which is being encoded by an “event” row with “event.regular == True” and an “event_exception” row with “event_exception.occurs == False” for every year where the event exceptionally is omitted.
  • Some events don’t occur regularly, but sometimes they exceptionally do. This is being encoded by an “event” row with “event.regular == False” and an “event_exception” row with “event_exception.occurs == True” for every year where the event exceptionally occurs.

How can I write a query that matches all events that will occur this year?

My guess was something like

session.query(Event, EventException).filter(Event.id==EventException.event_id)
.filter(EventException.year==current_year).
filter(or_(
    and_(Event.regular==1, EventException.occurs==0, having(count(EventException)==0)),
    and_(Event.regular==0, EventException==1, having(count(EventException)>0)
))

, but I’m not sure if the having clause can be used within an and_.

  • 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-06-10T16:09:24+00:00Added an answer on June 10, 2026 at 4:09 pm

    You can’t use HAVING without GROUP BY. Anyway, neither of them is necessary in this case, what is needed is EXISTS. Assuming you already have a SQLAlchemy relationship defined for Event.exceptions, the following expression should work:

    session.query(Event).filter(or_(
        and_(
            Event.regular == True,
            ~Event.exceptions.any(and_(
                EventException.year == current_year,
                EventException.occurs == False,
            )),
        ),
        and_(
            Event.regular == False,
            Event.exceptions.any(and_(
                EventException.year == current_year,
                EventException.occurs == True,
            )),
        ),
    ))
    

    and generate SQL like the following:

    SELECT event.*
    FROM event
    WHERE
        (
            event.regular = true
            AND NOT EXISTS (
                SELECT 1
                FROM event_exception
                WHERE
                    event.id = event_exception.event_id
                    AND event_exception.year == :year
                    AND event_exception.occurs = false
            )
        )
        OR
        (
            event.regular = false
            AND EXISTS (
                SELECT 1
                FROM event_exception
                WHERE
                    event.id = event_exception.event_id
                    AND event_exception.year == :year
                    AND event_exception.occurs = true
            )
        )
    

    EDIT: the first condition should use NOT EXISTS instead

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables that have a foreign key constraint between them Table event
I have a table that has a composite key that consists of two int
I have two tables. One of events and one of articles. Each event has
I have two tables that share the same column ( C ) but SQL
USING MySQL. I have two tables. Table A, and Table B. Table A has
I have two tables and I want to have change event on one table.
I have got two tables: Event +----------+---------+--------------+ | event_id | name | date |
I have two tables, events and photos, which relate together via the 'Event_ID' column.
I have a table with a column event_time . How can I select two
I have a simple query that relies on two full-text indexed tables, but it

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.