Solved

Oracle - How to analyze data using DATE COLUMN?

Posted on 2016-10-25
7
84 Views
Last Modified: 2016-10-26
Experts,

My data looks like this:

ProviderID,ClaimID,MemberID,ProcedureCode,ServiceBeginDate
1000,20000,3000,12345,2016-01-01
1000,20001,3000,12345,2016-01-07
1000,20005,3000,12345,2016-06-01
1000,20002,3001,23456,2016-02-01
1000,20003,3002,23466,2016-02-02
1001,20011,3003,24565,2016-03-01
....
....
....

What is the best way to write a SQL script to query this table and return all ProviderIDs and MemberIDs that have billed PROCEDURE CODE "12345"... 3 times or more per YEAR?

From the above data provider "1000" and member "12345" should be returned based on the "ServiceBeginDate".
0
Comment
Question by:MIKE
7 Comments
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 41859297
This is for SQL Server.  For Oracle, you might need to change the "YEAR()" function to something else.

SELECT ProviderID,MemberID,YEAR(ServiceBeginDate) AS ServiceBeginYear
FROM table_name
WHERE ProcedureCode = '12345'
GROUP BY ProcedureCode,MemberID,YEAR(ServiceBeginDate)
HAVING COUNT(*) >= 3
0
 
LVL 17

Author Comment

by:MIKE
ID: 41859305
Yep, I could use   ...extract(year from ServiceBeginDate)

Ok thanks for the input.
0
 
LVL 34

Accepted Solution

by:
johnsone earned 500 total points
ID: 41859307
For Oracle it would be something like this:
SELECT providerid, 
       memberid, 
       Trunc(servicebegindate, 'yyyy') year, 
       Count(1) 
FROM   mytab 
WHERE  procedurecode = 12345 
GROUP  BY providerid, 
          memberid, 
          Trunc(servicebegindate, 'yyyy') 
HAVING Count(1) >= 3; 

Open in new window

There are a few ways in Oracle to get to something common for the year, this is just one.  Should be slightly faster as it wouldn't require a datatype conversion.
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 28

Expert Comment

by:Pawan Kumar
ID: 41859801
Try..FOR SQL SERVER..


--

SELECT * FROM 
(
	SELECT providerid, 
		   memberid, 
		   YEAR(servicebegindate) servicebegindate, 
		   COUNT(*) OVER(PARTITION BY ProcedureCode,MemberID,YEAR(ServiceBeginDate)) Counts 
	FROM   YourTable 
	WHERE  procedurecode = 12345 
)b 
WHERE Counts >= 3


--

Open in new window

0
 
LVL 34

Expert Comment

by:johnsone
ID: 41860079
Why the windowed aggregate?  There isn't a need for it and there is a lot more overhead to that than a simple group by.
0
 
LVL 28

Expert Comment

by:Pawan Kumar
ID: 41860093
Hey johnsone,
I have given this option if they need other columns like ClaimID in the select list which are not part of the Group By clause so they can use this option of windowed aggregate.

Regards,
Pawan
0
 
LVL 34

Expert Comment

by:johnsone
ID: 41860103
Not what they asked for and you aren't showing how they would accomplish that.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
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 videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

839 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