Solved

Oracle - How to analyze data using DATE COLUMN?

Posted on 2016-10-25
7
100 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
[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
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 35

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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 29

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 35

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 29

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 35

Expert Comment

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

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

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…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
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.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

635 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