Solved

analytics query - days within range - oracle sql

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

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…
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 setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…

803 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