Solved

analytics query - days within range - oracle sql

Posted on 2014-09-05
2
257 Views
Last Modified: 2014-09-08
I need count the number of previous episodes within 28 days of the current episode.

the attached has some sample data from the episodes table (fig 1). fig 2 is the desired output with the calculated field (prev_episodes)

I’d like to use an analytics function rather than an aggregate function.

thanks
ee-output2.xlsx
0
Comment
Question by:tonMachine100
2 Comments
 
LVL 73

Accepted Solution

by:
sdstuber earned 500 total points
ID: 40306307
SELECT e.*,
       COUNT(
           *
       )
       OVER(
           PARTITION BY person_id
           ORDER BY dte
           RANGE INTERVAL '28' DAY PRECEDING
       ) - 1
           prev_episodes
  FROM episodes e

Open in new window


instead of subtracting one to exclude the current row
you could use BETWEEN on the range

SELECT e.*,
       COUNT(
           *
       )
       OVER(
           PARTITION BY person_id
           ORDER BY dte
           RANGE BETWEEN INTERVAL '28' DAY PRECEDING AND INTERVAL '1' DAY PRECEDING
       )
           prev_episodes
  FROM episodes e

Open in new window

0
 

Author Closing Comment

by:tonMachine100
ID: 40309939
great thanks
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  …
This post first appeared at Oracleinaction  (http://oracleinaction.com/undo-and-redo-in-oracle/)by Anju Garg (Myself). I  will demonstrate that undo for DML’s is stored both in undo tablespace and online redo logs. Then, we will analyze the reaso…
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.
This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now