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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:22:59+00:00 2026-05-28T05:22:59+00:00

I am using DB2 in this case, but I’m figuring this has a generic

  • 0

I am using DB2 in this case, but I’m figuring this has a generic SQL answer. I have simplified the data as much as I can. I am counting actions on things called “Claims”. Each claim has a unique claim number. Each action is timestamped in the format “hhmm”. Actually, I’m not counting actions, I’m counting action sessions–Most of the time, a person performs one action on one claim, and that’s one action session. But sometimes a person performs multiple actions on one claim, separated by a few seconds or a few minutes: that also would be one action session. But if somebody performed an action on a claim at 10am, and then performed an action on that same claim at 1pm, those would be two action sessions. For my purposes, the time window for what makes something one action session vs. two action sessions is 3 hours, but that’s arbitrary, of course. And there is no worry of the window spanning across midnight. Also, I have read-only access to this data, and I have to do this in one statement. Thanks.

So Here’s some data (Table: ACTIONS):

CLAIM_NO ACTTIME
AA       1424
BB       1134
CC       1221
DD       1425
DD       1512
EE       1619
FF       0928
FF       1518
GG       1348
HH       1332
II       1350

I would like to turn that into

CLAIM_NO ACTTIME
AA       1424
BB       1134
CC       1221
DD       1425
EE       1619
FF       0928
FF       1518
GG       1348
HH       1332
II       1350

(Note that the second DD record is gone, but the second FF record is still there).

I have accomplished this by joining the table to itself, on CLAIM_NO being equal and ACTTIME being between 3 hours earlier and 1 minute earlier. This allows me to get the rows that don’t belong, and then I use EXCEPT to eliminate them.

with excepto as (
 select a.claim_no, b.acttime
 from actions a 
 join actions b
 on a.claim_no=b.claim_no 
  and a.acttime between (b.acttime-300) and (b.acttime-1)
)
select * from actions except select * from excepto

But I’d like to do this with one join, so there is no “except” necessary. This is in hopes that performance will be better: my real data has more columns being used by the except and more rows, of course. And that except statement seems to be slowing the query down a whole lot. I’m using a whole lot of temporary tables via the “with” statement, and they seem to be much slower than the sum of their parts.

  • 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-28T05:23:00+00:00Added an answer on May 28, 2026 at 5:23 am

    I feel a little silly for forgetting about this…

    You don’t need the except – there’s a join available called exception that does exactly what you want (and I’ve used it heavily):

    SELECT a.claim_no, a.acttime
    FROM actions as a
    EXCEPTION JOIN actions as b
    ON b.claim_no = a.claim_no
    AND b.acttime >= a.acttime - 300
    AND b.acttime < a.acttime
    

    Gains you a result set of:

    claim_No     acttime
    ============================
    AA           1,424 
    BB           1,134 
    CC           1,221 
    DD           1,425 
    EE           1,619 
    FF             928 
    FF           1,518 
    GG           1,348 
    HH           1,332 
    II           1,350 
    

    (unfortunately, this isn’t going to cut it for situations where you have somebody making changes at least once every 3 hours – it’s only going to show up the first one. I believe you need something like a 6-way self-join minimum to detect the proper entries, and it’s convoluted somewhat, too; you may have better luck dealing with this application side)

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

Sidebar

Related Questions

This question also has the answer , but it mentions DB2 specifically. How do
I'm running an SQL query against a mainframe DB2 database using the OdbcDataReader class
Hi im using sqlalchemy on a db2 table with 500k rows. using plain sql
Hibernate criteria, using DB2 dialect, generates the following SQL with composite keys in the
We're using DB2 9.1 on Linux and SQL Server 2005, and the IBM db2
I am currently using DB2 . I do not know much about load statement.
I have the same issue using hibernate against DB2 and MySQL. Here is a
I started using the system with a pre-made file called DB2.SQL. I am using
I am using IBM DB2 with Toad. I have a stored procedure that I
I am using DB2 DBMS. Scenario 1: myTable has a composite key (key1, key2)

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.