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 7694953
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T21:26:08+00:00 2026-05-31T21:26:08+00:00

I have an Events table with a not-null date field. Based on a pivot

  • 0

I have an Events table with a not-null date field. Based on a pivot date and an offset, the query must return the next 2 events.

Here’s the query I’m trying to build:

SELECT e.* FROM Events as e
JOIN (
    -- latest event before pivot date, with offset
    SELECT * FROM Events where date < :dateRef
        ORDER BY date DESC, id DESC LIMIT :offset,1
) as eLatest
-- depending on :dateRef and :offset, eLatest can be empty!
-- If eLatest is empty, the join should produce all rows from Events table.
-- "eLatest is null" gives an error... How can I do it?
ON e.date > eFirst.date  || (e.date = eFirst.date && e.id >= eFirst.id)
ORDER BY e.date, e.id LIMIT 2

With the following events sequence when sorted by date ASC (no equal dates to keep it simple) :
e0 – e1 – e2 – … – e10

here are some desired results:

  • :dateRef = e2.date, :offset = 0 : [e1, e2]
  • :dateRef = e2.date, :offset = 1 : [e0, e1]
  • :dateRef = e2.date, :offset > 1 : [e0, e1] <— Does not work, returns no row
  • :dateRef <= e0.date, :offset = ? : [e0, e1] <— Does not work, returns no row
  • :dateRef = e5.date, :offset = 2 : [e2, e3]
  • :dateRef > e10.date, :offset = 0 : [e10]
  • :dateRef > e10.date, :offset = 2 : [e8, e9]

The problem I have is that when the subquery eLatest is empty, the join never matches. I can’t figure out how to drop the join condition when eLatest is empty.

The final query must be doable in JPQL or JPA criteria (so no UNIONS! I already have a query that works with a UNION and am trying to get rid of it…)

  • 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-31T21:26:10+00:00Added an answer on May 31, 2026 at 9:26 pm

    Would a RIGHT JOIN solution do?

    SELECT e.* FROM Events as e
    JOIN (
        -- latest event before pivot date, with offset
        SELECT COALESCE(ev.date, b.date), COALESCE(ev.id, b.id)
        FROM (
            SELECT * FROM Events where date < :dateRef
            ORDER BY date DESC, id DESC LIMIT :offset,1
        ) ev
        RIGHT JOIN (
            SELECT CAST('1900-01-01' AS date) AS date, 0 AS id
        ) AS b ON 1=1
    ) as eFirst
    ON e.date > eFirst.date  || (e.date = eFirst.date && e.id >= eFirst.id)
    ORDER BY e.date, e.id LIMIT 2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following table EVENT_LOG : EVENT_ID: pk, int, not null TYPEID: fk,
I have the following tables: CREATE TABLE `attendance_event_attendance` ( `id` int(11) NOT NULL AUTO_INCREMENT,
Suppose I have the following table: CREATE TABLE EXAMPLETABLE ( ID NUMBER(10,0) NOT NULL,
I have a table called Events that I want to insert information to. Here
I have an Events table whose goal is to store actions done by web
I have an events table with a 'featureEvent' boolean column. I need to display
I have 2 tables events and events_visibility events table and the column id -
I have a table of events with a recorded start and end time as
I have a table of events, I need to find all tail events of
I have a database table of events. Each event has a category and a

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.