Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

176
Views
How use a few columns using group by in PostgresSQL?

How i can use COALESCE(price)? Because i got error "non aggregated", how i can use all columns in select with group by? COALESCE(price) - mean first value in group, but im need first and last value in group too, but how i can do it?

select MAX(price) as high, MIN(price) as low, COALESCE(price) as open, date_trunc('second', created_at) as secondVal
from "eur_usd_ticks"
where "created_at" > '2020-07-19 23:15:49' and "created_at" <= '2020-07-19 23:49:09'
group by secondVal order by "created_at" desc

Structure: price|created_at

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

coalesce is of no use. try this:

SELECT MAX(price) as high, MIN(price) as low, 
  (ARRAY_AGG(price ORDER BY created_at ASC))[1] AS open_price,
  (ARRAY_AGG(price ORDER BY created_at DESC))[1] as close_price, 
  date_trunc('second', created_at) as secondVal
from "eur_usd_ticks"
where "created_at" > '2020-07-19 23:15:49' and "created_at" <= '2020-07-19 23:49:09'
group by secondVal order by secondVal desc
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!