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

Ask A Question

Stats

  • Questions 508k
  • Answers 508k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Int is based on the underlying system's int implementation, and… May 16, 2026 at 4:20 pm
  • Editorial Team
    Editorial Team added an answer Since the correct answer has already been given, I'll argue… May 16, 2026 at 4:19 pm
  • Editorial Team
    Editorial Team added an answer You may want to use the GROUP_CONCAT() function: SELECT GROUP_CONCAT(name)… May 16, 2026 at 4:19 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

This one's tough to explain, so I'll try to show what I'm after using
I am having an insanely tough time getting Rails to connect to SQL Server
Simple question here. Context: A Transact-SQL table with an int primary key, and a
I need to export the data from 36 SQL tables containing 24GB of data
fgrep -ircl --include=*.{sql} [--] * doesn't seem to be doing the trick. Please help...
I'm jumping into ASP.Net MVC and wanted to know how tough it's been for
i have a large sql server db, and i want to get the schema
I'm having a tough time integrating moodle with sqlserver through FREEDTS even after reading
Is there a way to allow hook_cron to run with administrator privileges -- e.g
I've written this SQL query in MS Access: SELECT * FROM Students WHERE name

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.