Solved

analytics query - days within range - oracle sql

Posted on 2014-09-05
2
267 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 74

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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

Cursors in Oracle: A cursor is used to process individual rows returned by database system for a query. In oracle every SQL statement executed by the oracle server has a private area. This area contains information about the SQL statement and the…
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 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 recover a database from a user managed backup

733 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