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 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

StackOverflow Point

StackOverflow Point Navigation

  • Web Stories
  • Badges
  • Tags
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Web Stories
  • Badges
  • Tags
Home/ Questions/Q 1316
Alex Hales
  • 0
Alex HalesTeacher
Asked: May 30, 20222022-05-30T12:35:16+00:00 2022-05-30T12:35:16+00:00

group by – Postgresql count last 12 month

  • 0

[ad_1]

I am calculating the last 12-months count after joining multiple tables, my expected output is working is OK but it is not what I want?. I want to add another column with the name “Current Month”, so the basic idea is if I see a report for the month May, then it will start from Last year’s May till This year’s April and May as Current Month, total 13 columns counts. My intuition says window query will help me out on this, but I am now sure how I can do that.

select
c.name,
  SUM(case when RTRIM(TO_CHAR(mor.sent_at , 'Month')) = 'January'  THEN 1  END) as "January",
  SUM(case when RTRIM(TO_CHAR(mor.sent_at , 'Month')) = 'February' THEN 1  END) as "February",
  SUM(case when RTRIM(TO_CHAR(mor.sent_at , 'Month')) = 'March' THEN 1  END) as "March",
  SUM(case when RTRIM(TO_CHAR(mor.sent_at , 'Month')) = 'April' THEN 1  END) as "April",
  SUM(case when RTRIM(TO_CHAR(mor.sent_at , 'Month')) = 'May' THEN 1  END) as "May",
  SUM(case when RTRIM(TO_CHAR(mor.sent_at , 'Month')) = 'June' THEN 1  END) as "June",
  SUM(case when RTRIM(TO_CHAR(mor.sent_at , 'Month')) = 'July' THEN 1  END) as "July",
  SUM(case when RTRIM(TO_CHAR(mor.sent_at , 'Month')) = 'August' THEN 1  END) as "August",
  SUM(case when RTRIM(TO_CHAR(mor.sent_at , 'Month')) = 'September' THEN 1  END) as "September",
  SUM(case when RTRIM(TO_CHAR(mor.sent_at , 'Month')) = 'October' THEN 1  END) as "October",
  SUM(case when RTRIM(TO_CHAR(mor.sent_at , 'Month')) = 'November' THEN 1  END) as "November",
  SUM(case when RTRIM(TO_CHAR(mor.sent_at , 'Month')) = 'December' THEN 1  END) as "December"
from analytics_outbox mo 
inner join analytics_outbox_recipient mor on mor.analytics_outbox_id = mo.id
inner join customer c on c.id = mo.customer_id
group by c.name

Current Output:

name                              |january|february|march |april  |may    |june|july|august|september|october|november|december|
----------------------------------+-------+--------+------+-------+-------+----+----+------+---------+-------+--------+--------+
ABC                               |       |        |     1|      2|       |    |    |      |         |       |        |        |
DEF                               |     11|      24|    34|     32|     19|    |    |      |         |       |        |        |
GEH                               |      9|       3|     7|     18|     22|    |    |      |         |       |        |        |
IJK                               |       |        |      |      1|       |    |    |      |         |       |        |        |

[ad_2]

  • 0 0 Answers
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report
Leave an answer

Leave an answer
Cancel reply

Browse

Sidebar

Ask A Question

Related Questions

  • xcode - Can you build dynamic libraries for iOS and ...

    • 0 Answers
  • bash - How to check if a process id (PID) ...

    • 4800 Answers
  • database - Oracle: Changing VARCHAR2 column to CLOB

    • 1063 Answers
  • What's the difference between HEAD, working tree and index, in ...

    • 1010 Answers
  • Amazon EC2 Free tier - how many instances can I ...

    • 0 Answers

Stats

  • Questions : 43k

Subscribe

Login

Forgot Password?

Footer

Follow

© 2022 Stackoverflow Point. All Rights Reserved.

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.