Solved

Oracle - How to analyze data using DATE COLUMN?

Posted on 2016-10-25
7
94 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
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 
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 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 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 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Running total between 2 sql tables in Sql 6 50
When are cursors useful? 8 62
SQL Query 20 25
How can I group this data in the following manner? 2 34
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
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…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

710 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