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

The Archive Base Latest Questions

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

Is there a function that calculates the total count of the complete month like

  • 0

Is there a function that calculates the total count of the complete month like below? I am not sure if postgres. I am looking for the grand total value.

2012-08=# select date_trunc('day', time), count(distinct column) from table_name group by 1 order by 1;

     date_trunc      | count 
---------------------+-------
 2012-08-01 00:00:00 |    22
 2012-08-02 00:00:00 |    34
 2012-08-03 00:00:00 |    25
 2012-08-04 00:00:00 |    30
 2012-08-05 00:00:00 |    27
 2012-08-06 00:00:00 |    31
 2012-08-07 00:00:00 |    23
 2012-08-08 00:00:00 |    28
 2012-08-09 00:00:00 |    28
 2012-08-10 00:00:00 |    28
 2012-08-11 00:00:00 |    24
 2012-08-12 00:00:00 |    36
 2012-08-13 00:00:00 |    28
 2012-08-14 00:00:00 |    23
 2012-08-15 00:00:00 |    23
 2012-08-16 00:00:00 |    30
 2012-08-17 00:00:00 |    20
 2012-08-18 00:00:00 |    30
 2012-08-19 00:00:00 |    20
 2012-08-20 00:00:00 |    24
 2012-08-21 00:00:00 |    20
 2012-08-22 00:00:00 |    17
 2012-08-23 00:00:00 |    23
 2012-08-24 00:00:00 |    25
 2012-08-25 00:00:00 |    35
 2012-08-26 00:00:00 |    18
 2012-08-27 00:00:00 |    16
 2012-08-28 00:00:00 |    11
 2012-08-29 00:00:00 |    22
 2012-08-30 00:00:00 |    26
 2012-08-31 00:00:00 |    17
(31 rows)
--------------------------------
      Total          |    12345
  • 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-13T13:59:02+00:00Added an answer on June 13, 2026 at 1:59 pm

    As best I can guess from your question and comments you want sub-totals of the distinct counts by month. You can’t do this with group by date_trunc('month',time) because that’ll do a count(distinct column) that’s distinct across all days.

    For this you need a subquery or CTE:

    WITH day_counts(day,day_col_count) AS (
      select date_trunc('day', time), count(distinct column)
      from table_name group by 1
    )
    SELECT 'Day', day, day_col_count
    FROM day_counts
    UNION ALL
    SELECT 'Month', date_trunc('month', day), sum(day_col_count)
    FROM day_counts
    GROUP BY 2
    ORDER BY 2;
    

    My earlier guess before comments was: Group by month?

    select date_trunc('month', time), count(distinct column)
    from table_name
    group by date_trunc('month', time)
    order by time
    

    Or are you trying to include running totals or subtotal lines? For running totals you need to use sum as a window function. Subtotals are just a pain, as SQL doesn’t really lend its self to them; you need to UNION two queries then wrap them in an outer ORDER BY.

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

Sidebar

Related Questions

is there any function in C++ that calculates a fingerprint or hash of a
TTR has some excellent TA indicators. Is there a package or function that calculates
Is there a function that tells what class you are extending? Like function_exists? I
Is there a function that can round a float in Fsharp? Something like round(3.21156,3)
I have created a function (see below) that calculates a 7.5% sales tax. Now
Is there any function that does the inverse operation of array_diff() ? I mean,
Is there a function that can truncate or round a Double? At one point
Is there a function that takes 1,2,3 and returns 3,1,2 respectively and vice-verse. eg:
Is there some function that returns the parent directory of a file in Perl?
Is there any function that do the opposite job of QString::split() . I mean

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.