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

  • Home
  • SEARCH
  • 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 8952891
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:58:28+00:00 2026-06-15T13:58:28+00:00

I’m having an issue with a query, I’m using postgresql 9.2. I have created

  • 0

I’m having an issue with a query, I’m using postgresql 9.2.

I have created a view which returns a key value, a timestamp, and an interval that look like this:

select * from foo.a;  -- My view and the results

 k  |         ts1            |  tm
 1  | 2011-12-08 06:46:00+00 | 01:00:00
 1  | 2011-12-08 09:14:00+00 | 01:00:00
 2  | 2011-12-08 02:00:00+00 | 02:00:00
 2  | 2011-12-08 05:00:00+00 | 02:00:00
 4  | 2011-12-08 10:06:00+00 | 02:00:00
 4  | 2011-12-08 13:31:00+00 | 02:00:00
 1  | 2011-12-11 03:00:00+00 | 01:00:00
 1  | 2011-12-11 04:49:00+00 | 02:00:00
 4  | 2011-12-11 10:06:00+00 | 02:00:00
 4  | 2011-12-11 13:31:00+00 | 02:00:00

I have about 8,000 rows in my results. Now, I want to sum the interval’s and group by key, so I do this:

select k,
       sum(tm) as tm
  from foo.a
group by k;

As expected my results are:

k   |  tm
1   | 5:00
2   | 4:00
4   | 8:00

I want to take it a step further and do this (the error – ERROR: subquery uses ungrouped column ‘ts1’ from outer query).

select k,
       sum(tm) as tm,
       (select sum(tm) where ts1 > '2011-12-09')
        -- (select sum(tm) where ts1 > '2012-01-01')
        -- I need to the above on 5 different dates. 
  from foo.a
  group by k,

Desired results are:

k    |   tm   |  tm1
1    |  5:00  |  3:00
2    |  4:00  |  
4    |  8:00  |  4:00

If I add ts1 into my group clause, it doesn’t return desired results, naturally – because I don’t want to group by ts1, only k. I can use ts1 in an aggregate function and the query will run with undesirable results.

This works:

select foo.a.k,
       bar.tm1,
       sum(tm)
  from foo.a
  left join
      (select k,
              sum(tm) as tm1
         from foo.a
        where foo.a.ts1 > '2011-12-09'
        group by k) as bar
  on foo.a.k = bar.k
  group by foo.a.k,
           bar.tm1;

But querying the same view for the same information seems wrong and unnecessary. Especially, since I have to do this 5 times.

My question: Is there a work-around for performing a correlated-query on an ungrouped column? If not, what is a better approach to achieving my desired results?

  • 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-06-15T13:58:30+00:00Added an answer on June 15, 2026 at 1:58 pm

    Is this what you want?

    select k,
           sum(tm) as tm,
           sum(case when ts1 > '2012-01-01' then tm else 0 end) as tm_20120101
    from foo.a
    group by k
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
We're building an app, our first using Rails 3, and we're having to build
I have an autohotkey script which looks up a word in a bilingual dictionary
I have an array which has BIG numbers and small numbers in it. I
I have a text area in my form which accepts all possible characters from
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
I have thousands of HTML files to process using Groovy/Java and I need to
I have a view passing on information from a database: def serve_article(request, id): served_article
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example

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.