Solved

analytics query - days within range - oracle sql

Posted on 2014-09-05
2
272 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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that use…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
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

705 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