Home » SQL & PL/SQL » SQL & PL/SQL » Best way to get count of unique months with activity (Oracle 11.2)
Best way to get count of unique months with activity [message #635582] Wed, 01 April 2015 12:57 Go to next message
statey603
Messages: 1
Registered: April 2015
Location: NH
Junior Member
Hello,

I am working on a report that runs using a date range specified by the user. One piece of data that I must return is the number of unique months that have activity reported. the nature of the activity is such that there might be no activity in a given month or there might be multiple occurrences. I only need to provide a count of the unique months that had activity.

For example, the report span might be 1/1/2014 - 12/31/2014. There are records showing activity for the following dates:
01/15/2014, 1/20/2014,1/25/2014,
03/10/2014,
04/25/2014, 4/30/2014,
06/15/2014,
08/10/2014,
10/20/2014, 10/25/2014,
11/05/2014, 11/15/2014, 11/25/2014
In this case, the result should be 7 unique months.

I have tried a few different SQL statements [below] to get the result I need, but they are slow [all taking about the same amount of time]. I am hoping that someone might be able to suggest a different, faster way to get the result that I need.
Note: In my examples, the date range is fixed. The finished SQL will use begin date and end date parameters.

 -- METHOD A [GROUP BY]
 SELECT COUNT(*) AS Months_Rptd
 FROM
  (     
    SELECT
      TO_CHAR(TRUNC(claim_end_dt,'MON'), 'MM/YYYY') AS MonthYear,
      COUNT(claim_end_dt)                           AS ClaimCount
    FROM claims_hist_tbl
    WHERE trunc(claim_end_dt) BETWEEN to_date('07012014','MMDDYYYY') 
                              AND to_date('06302015','MMDDYYYY') 
    GROUP BY TRUNC(claim_end_dt,'MON')
    HAVING COUNT(claim_end_dt) > 0
  );

  -- METHOD B [DISTINCT]
  SELECT COUNT(*) FROM
  (
    SELECT DISTINCT(TO_CHAR(TRUNC(claim_end_dt,'MON'), 'MM/YYYY')) AS MonthYear
    FROM claims_hist_tbl
    WHERE trunc(claim_end_dt) BETWEEN to_date('07012014','MMDDYYYY') 
                              AND to_date('06302015','MMDDYYYY') 
  );

  -- METHOD C
  SELECT 
  COUNT(DISTINCT TO_CHAR(TRUNC(claim_end_dt,'MON'), 'MM/YYYY'))
  FROM claims_hist_tbl
  WHERE trunc(claim_end_dt) BETWEEN to_date('07012014','MMDDYYYY') 
                            AND to_date('06302015','MMDDYYYY') 

[Updated on: Wed, 01 April 2015 13:21]

Report message to a moderator

Re: Best way to get count of unique months with activity [message #635584 is a reply to message #635582] Wed, 01 April 2015 13:46 Go to previous messageGo to next message
Michel Cadot
Messages: 68776
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

Quote:
but they are slow [all taking about the same amount of time].


In order to be able to give you some help you must provide the information described in http://www.orafaq.com/forum/mv/msg/84315/433888/102589/#msg_433888.

Re: Best way to get count of unique months with activity [message #635585 is a reply to message #635584] Wed, 01 April 2015 13:49 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
>WHERE trunc(claim_end_dt)

above prevents any index on CLAIM_END_DT from being used
Re: Best way to get count of unique months with activity [message #635587 is a reply to message #635582] Wed, 01 April 2015 15:23 Go to previous message
joy_division
Messages: 4963
Registered: February 2005
Location: East Coast USA
Senior Member
In addition to using the trunc which eliminates the index, there is no need for the trunc at all. Just do the ending range < 07/01/2015.

Also no need for TO_CHAR is you are only using it for the inner query which you will not display.

No need for the HAVING either.
Previous Topic: how to get unmatched record from emp and dept tables
Next Topic: Child tables, grand child tables of a table
Goto Forum:
  


Current Time: Wed Aug 26 05:03:14 CDT 2026