Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Oracle - How to analyze data using DATE COLUMN?

Posted on 2016-10-25
7
Medium Priority
?
101 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 70

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 2000 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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
LVL 30

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 30

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
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 restore a database from backup after a simulated disk failure using RMAN.

704 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