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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:37:20+00:00 2026-05-27T23:37:20+00:00

This may already have been asked, but StackOverflow is massive and trying to google

  • 0

This may already have been asked, but StackOverflow is massive and trying to google for something specific enough to actually help is a nightmare!

I’ve ended up with a fairly large SQL query, and was wondering if SO could maybe point out easier methods for doing it that I might have missed.

I have a table called usage with the following structure:

host    | character varying(32)       | 
usage   | integer                     | 
logtime | timestamp without time zone | default now()

I want to get the usage value for both the MAX and MIN logtimes. After working through some of my old textbooks (been a while since I really used SQL properly), I’ve ended up with this JOIN:

SELECT *
FROM (SELECT u.host,u.usage AS min_val,r2.min
  FROM usage u
  JOIN (SELECT host,min(logtime) FROM usage GROUP BY host) r2
  ON u.host = r2.host AND u.logtime = r2.min) min_table
NATURAL JOIN (SELECT u.host,u.usage AS max_val,r1.max
  FROM usage u
  JOIN (SELECT host,max(logtime) FROM usage GROUP BY host) r1
  ON u.host = r1.host AND u.logtime = r1.max) max_table
;

This seems like a messy way to do it, as I’m basically running the same query twice, once with MAX and once with MIN. I can get both logtime columns in one query by doing SELECT usage,MAX(logtime),MIN(logtime) FROM ..., but I couldn’t work out how to then show the usage values that correspond to the two different records.

Any ideas?

  • 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-27T23:37:20+00:00Added an answer on May 27, 2026 at 11:37 pm

    With PostgreSQL 9.1 you have window functions at your disposal (8.4+):

    SELECT DISTINCT
           u.host
          ,first_value(usage) OVER w AS first_usage
          ,last_value(usage) OVER w AS last_usage
    FROM   usage u
    WINDOW w AS (PARTITION BY host ORDER BY logtime, usage
                 ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)
    

    I sort the partition by logtime and by usage in addition to break any ties and arrive at a stable result. Read about window functions in the manual.

    For some more explanation and links you might want to refer to recent related answers here or here.

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

Sidebar

Related Questions

This may have been already asked but I can't seem to find this specific
This question may have been asked already - but unfortunately, I could not find
This may have been asked already, but I can't find it, so here goes.
this may have been asked already but I cant really find it (most likely
This may have already been answered in another post, but I just am not
Perhaps something similar has already been asked, and sure, it's a nitpick... I have
This may be a n00b topic but, anyways, I have been having a rather
This may have already been answered and I just can't find it so feel
My question has already been asked here , but I am trying to understand
This may seem totally unreasonable to ask, but I have been designing a multi-panel,

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.