Solved

analytics query - days within range - oracle sql

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…
This video shows how to recover a database from a user managed backup

757 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

20 Experts available now in Live!

Get 1:1 Help Now