Solved

Oracle - How to analyze data using DATE COLUMN?

Posted on 2016-10-25
7
58 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:ScottPletcher
Comment Utility
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
Comment Utility
Yep, I could use   ...extract(year from ServiceBeginDate)

Ok thanks for the input.
0
 
LVL 34

Accepted Solution

by:
johnsone earned 500 total points
Comment Utility
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 16

Expert Comment

by:Pawan Kumar Khowal
Comment Utility
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
Comment Utility
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 16

Expert Comment

by:Pawan Kumar Khowal
Comment Utility
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
Comment Utility
Not what they asked for and you aren't showing how they would accomplish that.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how the fundamental information of how to create a table.

762 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

9 Experts available now in Live!

Get 1:1 Help Now