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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T16:13:01+00:00 2026-06-12T16:13:01+00:00

I have a database formed like this: descr | hours | timer ———-+——-+———— foo

  • 0

I have a database formed like this:

 descr    | hours |   timer    
----------+-------+------------
 foo      |   1.3 | 2012-07-14
 foo      |   2.5 | 2012-07-15
 foo      |  2.35 | 2012-07-16
 bar      |     1 | 2012-08-16
 baz      |     1 | 2012-08-16
 buh      |     5 | 2012-08-17

This does what I expect:

abc=# select extract('month' from timer) as Month, sum(hours) from foo group by Month     order by 1;

 month | sum  
 ------+------
 7     | 6.15
 8     |    7

This doesn’t quite do what I expect:

abc=# select to_char(timer, 'Month'), sum(hours) from foo group by 1 order by 1 asc;

 to_char  | sum  
----------+------
August    |    7
July      | 6.15

The desired result is:

month     | sum
----------+-----
July      | 6.15
August    |    7

I understand why things are happening in #3, but how do I achieve the desired result, within PostgreSQL, in the most efficient manner possible?

  • 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-12T16:13:02+00:00Added an answer on June 12, 2026 at 4:13 pm

    You can use extract expression in ORDER BY section:

    SELECT to_char(timer, 'Month'), SUM(hours)
    FROM foo
    GROUP BY to_char(timer, 'Month'), extract('Month' from timer)
    ORDER BY extract('Month' from timer) ASC;
    

    This query does desired result.

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

Sidebar

Related Questions

I have different php output in jQuery-based tabs. This output is formed from database
i have database table like this +-------+--------------+----------+ | id | ip | date |
I have time like this in the database [open_time] => 10:00:00 [close_time] => 23:00:00
Hi all! I have a database table that looks somewhat like this (stripped for
I have Database with date field. I see the time like: 1900-01-01 13:38:00.000 How
that my problem: I have database table like that: id (AI) market_id 1 6
I'm quite a beginner at database design. I have two problems that I'd like
This seems like a classic rails situation: I want to have a my index
I have a PHP page which interacts with this small MySQL database table with
I have database with multiple tables in Microsoft SQL Server with schema in tables

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.