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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T07:19:49+00:00 2026-05-15T07:19:49+00:00

A tough SQL question (I’m using postgres by the way). I need the first

  • 0

A tough SQL question (I’m using postgres by the way).

I need the first row inserted every day for the past X days. one of my columns is a timestamp, which i hold the time inserted, and another column is the row id.

If it’s not possible to get the first row inserted every day, i at least need a unique one; a single row for every day for the past x days.

Any suggestions?

Thanks

okie

  • 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-15T07:19:50+00:00Added an answer on May 15, 2026 at 7:19 am

    You may want to try something like the following (tested in MySQL, but I guess it should be easy to port to Postgres):

    SELECT      l.id, l.timestamp, l.value
    FROM        log l
    INNER JOIN  (
                 SELECT    MIN(timestamp) first_timestamp
                 FROM      log
                 GROUP BY  DATE(timestamp)
                ) sub_l ON (sub_l.first_timestamp = l.timestamp)
    WHERE       l.timestamp > DATE_ADD(NOW(), INTERVAL -30 DAY);
    

    Note that this assumes that your timestamps are unique.

    Test Case (in MySQL):

    CREATE TABLE log (id int, timestamp datetime, value int);
    
    INSERT INTO log VALUES (1, '2010-06-01 02:00:00', 100);
    INSERT INTO log VALUES (2, '2010-06-01 03:00:00', 200);
    INSERT INTO log VALUES (3, '2010-06-01 04:00:00', 300);
    INSERT INTO log VALUES (4, '2010-06-02 02:00:00', 400);
    INSERT INTO log VALUES (5, '2010-06-02 03:00:00', 500);
    INSERT INTO log VALUES (6, '2010-06-03 02:00:00', 600);
    INSERT INTO log VALUES (7, '2010-06-04 02:00:00', 700);
    INSERT INTO log VALUES (8, '2010-06-04 03:00:00', 800);
    INSERT INTO log VALUES (9, '2010-06-05 05:00:00', 900);
    INSERT INTO log VALUES (10, '2010-06-05 03:00:00', 1000);
    

    Result:

    +------+---------------------+-------+
    | id   | timestamp           | value |
    +------+---------------------+-------+
    |    1 | 2010-06-01 02:00:00 |   100 |
    |    4 | 2010-06-02 02:00:00 |   400 |
    |    6 | 2010-06-03 02:00:00 |   600 |
    |    7 | 2010-06-04 02:00:00 |   700 |
    |   10 | 2010-06-05 03:00:00 |  1000 |
    +------+---------------------+-------+
    5 rows in set (0.00 sec)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a tough SQL question for you guys. I am working with the
I know this is similar to this question , but I'm using SQL Server
This is my very first question here, so don't be too tough with me.
I am finding tough to align the label and textboxes using Div's. If I
Simple question here. Context: A Transact-SQL table with an int primary key, and a
Update from comment: I need to extend linq-to-sql classes by own parameters and dont
This question is about a general technique in SQL, that I can't quite work
I have a question about why some SQL (running on SQL Server 2005) is
I need to perform batch export of several APEX applications into .sql script. I
This is a tough question to word...so bear with me. I have two tables

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.