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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T09:33:17+00:00 2026-06-09T09:33:17+00:00

I would like to know how to run a SQL query that returns events

  • 0

I would like to know how to run a SQL query that returns events that have a time that is in a valid segment.

For example, I have the two tables,

Event

event_id event_time
0        134
1        434
2        4300

Segment

segment_type segment_start_time segment_end_time
data1         100                130
data1         200                500
data1         700                8000
veto1         400                440
data2         150                450
data2         4000               5000
veto2         175                200

I want a query that would return

event_id event_time
1        434
2        4300

I was thinking something like

SELECT * 
FROM event
WHERE 
    event.event_time > (SELECT segment.segment_start_time FROM segment)
    AND 
    event.event_time < (SELECT segment.segment_end_time FROM segment)

But this just returns nothing. What am I doing wrong?

The next step I want to do is to find events times that are;

between a ‘data’ segment start time and end time AND
NOT between a ‘veto’ segment start time and end time

And finally, I actually have two type of data segments and veto segments. I want to return events that have times;

between a ‘data1’ segment start time and end time AND

between a ‘data2’ segment start time and end time AND

NOT between a ‘veto1’ segment start time and end time AND

NOT between a ‘veto2’ segment start time and end time

So from my example table above, the output I want would be

event_id event_time
2        4300
  • 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-09T09:33:18+00:00Added an answer on June 9, 2026 at 9:33 am

    I’m going to assume that you will normally have more entries in the event table than in the segment table.

    This allow the pseduo-code to be “for every segment, check which events are valid”. This allows range-seeks on the event table.

    SELECT
      event.event_id, event.event_time
    FROM
      segment
    INNER JOIN
      event
        ON  event.event_time >= segment.segment_start_time
        AND event.event_time <  segment.segment_end_time
    GROUP BY
      event.event_id, event.event_time
    

    The GROUP BY is only needed if different entries in segment can overlap.

    The alternative approach is with the pseudo-code “for each event, check if any segments overlap it”.

    Doing so, however, will not allow a range seek (see my comment on @MarkByers answer).

    EDIT Following edit to question…

    As far as I understand the changes, an event should only be returned if…

    • The event time is within at least (one data1 segment AND also one data2 segment)
    • The event time is not within (any veto1 segments OR any veto2 segments)

    SELECT
      event.event_id, event.event_time
    FROM
      segment
    INNER JOIN
      event
        ON  event.event_time >= segment.segment_start_time
        AND event.event_time <  segment.segment_end_time
    GROUP BY
      event.event_id, event.event_time
    HAVING
          0 = SUM(CASE WHEN segment_type = 'veto1' THEN 1 ELSE 0 END)
      AND 0 = SUM(CASE WHEN segment_type = 'veto2' THEN 1 ELSE 0 END)
      AND 1 = SUM(CASE WHEN segment_type = 'data1' THEN 1 ELSE 0 END)
      AND 1 = SUM(CASE WHEN segment_type = 'data2' THEN 1 ELSE 0 END)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a SQL query that returns a number of results (unfortunately, the number
I have a query in Delphi using DBExpress TSQLQuery that looks like so ActiveSQL.sql.add('SELECT
I would like to know how to run a file like this: Search-Mailbox -Identity
I would like to know how you would run a stored procedure from a
I have a script that generates an SQL query as text e.g. ... return
I am using SQL Server 2008 and would really like to know how the
i would like know some reference. I know i can googling it. but prefer
Would like to know what a programmer should know to become a good at
Would like to know the c# code to actually retrieve the IP type: Static
Would like to know how to integarate cruise control with maven? Cruise Control comes

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.