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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:59:50+00:00 2026-06-15T08:59:50+00:00

I have a table widget_events that records event_what events occurring to widget widget_id on

  • 0

I have a table widget_events that records event_what events occurring to
widget widget_id on date event_when. It’s possible for the same event to
occur multiple times to the same widget on the same day. For this reason,
column event_id is used as primary key to distinguish such rows. Here is
the table declaration:

CREATE TABLE widget_events
(
event_id    int4 UNIQUE NOT NULL,
event_when  date NOT NULL,
event_what  text NOT NULL,
widget_id   int4 REFERENCES widgets (widget_id) NOT NULL,
PRIMARY KEY (event_id)
);

The client application processes events in batches, where each batch consists
of all events for one widget on one date. However, the application has no
previous knowledge of which widgets and dates are stored in widget_events.

One possible solution is to start by selecting one random row from
widget_events (using SQL’s LIMIT), and then do another query for all
rows with the same widget_id and widget_when. After this batch is
processed, those rows can be deleted from widget_events, and we go back
to the first step. The algorithm stops when the first step reports that
there is no more random row to return.

My question is whether there is a faster, more elegant way to do this.
Is it possible in SQL (in particular the SQL understood by PostgreSQL)
to return each distinct batch in a single query?

  • 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-15T08:59:52+00:00Added an answer on June 15, 2026 at 8:59 am

    To select distinct batches:

    select  distinct event_when
    ,       event_what
    from    widget_events
    

    Or you could pick up a single batch in one query, like:

    select  batch.*
    from    widget_events batch
    join    (
            select  event_when
            ,       event_what
            from    widget_events
            limit   1
            ) filter
    on      filter.event_when = batch.event_when
            and filter.event_what = batch.event_what
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a HTML widget/table that is pulling info from a private server that
Say I have the following table: TABLE: widget - widget_id (not null, unique, auto-increment)
I have a table widget in reporting services where I group rows on a
I have table with around 70 000 rows. There is 6000 rows that i
I have table with 300 000 records (MyISAM). I get record from this table
I have table of data that is sorted as follows: Item | Sample |
I have an HTML form: <div id=dialog class=event-dialog title=Create Event> <div id=dialog-inner> <table> <tr><td
I have a bunch of QComboBox es in a table. So that I know
I have a table that keeps track of the physical mass of sprockets in
I have a StyledText widget with embedded Table controls. However, once I add 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.