0

Here's my first query to shows the number of customers added per year-month

select count(name) AS CUSTOMER, extract(year from create_date) as yr, extract(month from create_date) as mon from x group by extract(year from create_date), extract(month from create_date) order by yr desc, mon desc; 
CUSTOMER YR MON
3 2019 07
4 2015 02
100 2014 09
3 2014 04

I tried the query

SELECT MAX(count(*)) FROM x GROUP BY create_date; 

in the results I have;

MAX(COUNT(*))
100

need to see the year and month in the result.

How to do this?

1
  • Your question has several format problems, but, ignoring that, have you tried parsing to number the year and month of the date? to_number(to_char(create_date, 'YYYYMM')). Commented Sep 17, 2021 at 8:21

3 Answers 3

1

The way I understood the question, you'd use rank analytic function in a subquery (or a CTE) and fetch rows whose count is either minimum or maximum. Something like this:

with temp as (select to_char(create_date, 'yyyymm') yyyy_mm, count(*) cnt, -- rank() over (order by count(*) asc) rnk_min, rank() over (order by count(*) desc) rnk_max from x group by to_char(create_date, 'yyyymm') ) select yyyy_mm, cnt from temp where rnk_min = 1 or rnk_max = 1; 
Sign up to request clarification or add additional context in comments.

2 Comments

"cnt" : invalid indentifier
Not any more; forgot to set alias (line #3). Fixed; sorry.
0

You can use two levels of aggregation and put the results all in one row using keep (which implements a "first" aggregation function):

select max(num_customers) as max_num_customers, max(yyyymm) keep (dense_rank first order by num_customers desc) as max_yyyymm, min(num_customers) as max_num_customers, max(yyyymm) keep (dense_rank first order by num_customers asc) as in_yyyymm, from (select to_char(create_date, 'YYYY-MM') as yyyymm, count(*) AS num_customers from x group by to_char(create_date, 'YYYY-MM' ) ym 

Comments

0

From Oracle 12, you can use FETCH FIRST ROW ONLY to get the row with the highest number of customers (and, in the case of ties, the latest date):

SELECT count(name) AS CUSTOMER, extract(year from create_date) as yr, extract(month from create_date) as mon FROM x GROUP BY extract(year from create_date), extract(month from create_date) ORDER BY customer DESC, yr DESC, mon DESC FETCH FIRST ROW ONLY; 

If you want to include ties for the highest number of customers then:

SELECT count(name) AS CUSTOMER, extract(year from create_date) as yr, extract(month from create_date) as mon FROM x GROUP BY extract(year from create_date), extract(month from create_date) ORDER BY customer DESC FETCH FIRST ROW WITH TIES; 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.