Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Finding consecutive months

64a51ebf-6635-47dc-9c7a-f6e5d1b5a34cJan 11 2016 — edited Jan 11 2016

The following answer was posted by Frank Kulash back in Oct of 2010 in response number 774064. His solution correctly brings back the correct personal ID and drug name. My question however is as follows. Can this be tweaked to bring back also the first fill date?

WITH  got_month_num  AS

(

SELECT DISTINCT  name
,       drug_name
,       MONTHS_BETWEEN ( TRUNC (SYSDATE,   'MONTH')
                      , TRUNC (fill_date, 'MONTH')
                     ) AS month_num
FROM      table_x
-- WHERE      ... -- Any filtering goes here

)

, got_group_num AS

(

SELECT name
, drug_name
, ROW_NUMBER () OVER ( PARTITION BY  name
                           ,                drug_name
                     ORDER BY    month_num
                   ) - month_num AS group_num
FROMgot_month_num

)

SELECT DISTINCT  name, drug_name  
FROM         got_group_num
GROUP BY    name, drug_name,  group_num
HAVING       COUNT (*)   >= 3
ORDER BY  name, drug_name

;

This post has been answered by Frank Kulash on Jan 11 2016
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 8 2016
Added on Jan 11 2016
6 comments
1,543 views