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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:18:52+00:00 2026-05-14T04:18:52+00:00

I am trying to query a table in PostgreSQL 8.4.2 server for to get

  • 0

I am trying to query a table in PostgreSQL 8.4.2 server for to get open-high-low-close data. The table and my first query are below.

Question:
Is there a way to get the same results without using subqueries as in the example query below? Perhaps using FIRST_VALUE() or LAST_VALUE() window methods?

-- FIRST ATTEMPT AT OHLC
SELECT
  contract_id
, TO_CHAR(ts, 'YYMMDDHH24MI')
, (SELECT price FROM fill minF WHERE minF.fill_id = MIN(f.fill_id)) AS open
, MAX(f.price) AS high
, MIN(f.price) AS low
, (SELECT price FROM fill maxF WHERE maxF.fill_id = MAX(f.fill_id)) AS close
FROM fill f
GROUP BY 1,2
ORDER BY 1,2;

-- SIMPLIFIED DDL
CREATE TABLE fill
(
    contract_id SEQUENCE PRIMARY KEY
,   ts          TIMESTAMP
,   price       NUMERIC(10,4)
);
  • 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-14T04:18:52+00:00Added an answer on May 14, 2026 at 4:18 am

    I would like to get sub-day resolution. This appears to work well.

    SELECT
      contract_id
    , the_minute
    , open
    , high
    , low
    , close
    FROM 
    (
      SELECT
        contract_id
      , TO_CHAR(ts, 'YYMMDDHH24MI') AS the_minute
      , MIN(price) OVER w            AS low
      , MAX(price) OVER w            AS high
      , LAST_VALUE(price) OVER w     AS open   -- Note the window is in reverse (first value comes last)
      , FIRST_VALUE(price) OVER w    AS close  -- Note the window is in reverse (last value comes first)
      , RANK() OVER w                AS the_rank
      FROM fill
      WINDOW w AS (PARTITION BY contract_id, TO_CHAR(ts, 'YYMMDDHH24MI') ORDER BY fill_id DESC)
    ) AS inr
    WHERE the_rank = 1
    ORDER BY 1, 2;
    

    Thank you, Scott. You answer helped me get to the following solution.

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

Sidebar

Related Questions

I'm trying to query a table with a column which is xml data with
I am trying to write the query for existing table data. TABLE_A and TABLE_B
I am trying to insert data into a PostgreSQL database table using Python. I
I am trying to query data from my app, through postgresql DB. What I
I'm trying to create a function to insert data into a table. The query
I'm trying to convert a PostgreSQL into SQL Server. But this query doesn't work.
I'm trying to modify a table inside my PostgreSQL database, but it says there
I'm trying to query a table, fetch all records, and save the result as
I'm trying to query a mysql table which places its date in the following
I'm trying to query a set of buldings listed on a table, these buildings

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.