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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T22:53:25+00:00 2026-05-15T22:53:25+00:00

i have some SQL code that is inserting values from another (non sql-based) system.

  • 0

i have some SQL code that is inserting values from another (non sql-based) system. one of the values i get is a timestamp.

i can get multiple inserts that have the same timestamp (albeit different values for other fields).

my problem is that i am trying to get the first insert happening every day (based upon timestamp) since a particular day (i.e. give me the first insert of each day since January 28, 2007…)

my code to get the first timestamp of every day is as follows:

SELECT MIN(my_timestamp) AS first_timestamp
FROM my_schema.my_table
WHERE my_col1 = 'WHATEVER'
AND my_timestamp > timestamp '2010-Jul-27 07:45:24' - INTERVAL '365 DAY'
GROUP BY DATE (my_timestamp);

This delivers me the list of times available. But when I join against these times, I can get several rows, as there are lots of rows that mach these times. So for 365 days, I may get 5,000 rows (I could be inserting 100 rows at 00:00:00 every day).

Assuming, in the example above, my_table has columns my_col1 and my_col2, how can I get exactly 365 rows that contain my_col1 & my_col2? it doesn’t matter which row i get back if there are multiple rows for a date; any row will suffice.

it’s an odd question. the overall problem is: given a timestamp, how can one get 1-row-per-timestamp even if there are multiple rows that have said timestamp (assuming there is no other priority)?

thanks for the help in advance.

EDIT:
So, let’s say for example, this table has the following columns: my_col1, my_col2, and my_timestamp.
Here are example values (in order of my_col1 – my_col2 – my_timestamp):

  • ‘my_val1’ – 10 – ‘2010-07-01 01:01:01’
  • ‘my_val2’ – 11 – ‘2010-07-01 01:01:01’
  • ‘my_val3’ – 12 – ‘2010-07-01 01:01:01’
  • ‘my_val4’ – 13 – ‘2010-07-01 01:01:02’
  • ‘my_val5’ – 14 – ‘2010-07-02 01:01:01’
  • ‘my_val6’ – 15 – ‘2010-07-02 01:01:01’
  • ‘my_val7’ – 16 – ‘2010-07-03 01:01:01’

in the end, i would want only 3 rows, 1 with a timestamp with ‘2010-07-01 01:01:01’, one with ‘2010-07-02 01:01:01’, and one with ‘2010-07-03 01:01:01’. the third one is easy, since there is only 1 row with that last timestamp. but the first two are the tricky ones. the sql i posted above will ignore the row with ‘my_val4’.

i need a query that will return me all of the columns, not just the dates.

how would i get sql to give me either the first or last of the values that would match that timestamp (it doesn’t matter either way. i just need to get 1-per first-day’s timestamp matching)?

  • 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-15T22:53:26+00:00Added an answer on May 15, 2026 at 10:53 pm
    select distinct on (date(my_timestamp)) *
    from my_table
    order by date(my_timestamp), my_timestamp
    

    This selects all columns, exactly one row per date(my_timestamp). The single row per day is the first row for the group, as determined by order by (so that’s the row with minimal my_timestamp).

    Of course you can add whatever joins, wheres etc. you need. But this is the stub you’re looking for.

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

Sidebar

Related Questions

If have a piece of code that gets some data from a sql database
I have some code that uses the SQL Server 2005 SMO objects to backup
I have some C# code that dynamically generates an SQL query and executes it
We have some code that archives data from a Microsoft Access database into a
I have some PL/SQL code that I think might have a memory leak. Everytime
We have some code that we want to run a BULK INSERT sql command,
I have some code that currently builds up an In statement in SQL. I
I have some e-commerce code that I use often that uses Linq To SQL
I have some existing code that retrieves data from a database using ADO.NET that
I have some existing code that is querying a SQL database repeatedly with different

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.