Solved

T-SQL Filter Groups

Posted on 2013-01-04
8
473 Views
Last Modified: 2013-01-09
I am stuck on a query where we need to track in-process dental procedures.  Below is an SQL script that is able to pull all of the dental procedures for the following processes:
-      Root Canal (RCT)
-      Bridges
-      Crowns
-      Dentures
-      Space Maintainers
The issue that I have is that my script also pulls the final completed procedures instead of just the incomplete procedures.  We have created local codes for each process in a procedure (service_item_id without a D in front of the number).  When an in-process procedure is completed then a D-Code (ADA Code) is then completed for that procedure.  
The idea is that we can track an in-process procedure and see what has been completed (local codes) and how much the patient has paid throughout the process.

Attached is test data dump from what my query selects.  I dump everything into a temp table so that we can manipulate the data.  From what I can see is that the teeth and quadrants can be considered unique.    Quadrants can be used for dentures and the teeth for the rest.  But bridges do span across multiple teeth but a D-Code will be associated with all of them when they are finally completed.  That is what makes it hard to filter through.
The sql code will has case statement to show what codes associate to what procedure

Thank you for all of your help in advance.  Here is the code:

select convert(varchar,pe.enc_timestamp, 101)[Encounter_Date], l.location_name, p.last_name, p.first_name, p.person_nbr,
pe.enc_nbr [encounter_number],'Dr. ' + pr.last_name [provider], c.service_item_id, sm.description, c.tooth,c.surface,
 Case when c.quadrant = '01' then 'UM'
 when c.quadrant = '02' then 'LM'
 end [Quandrant],
Case when c.service_item_id in ('9992','3302','3400','D3330','D3310','D3320','D3331') then 'Root Canal'
when c.service_item_id in ('2705','2702','2701','2071','2070','D2750','D2751','D2752','D2790','27500','D2931','D2930','D2740','D2962') Then 'Crown'
when c.service_item_id in ('2707','2708','6690','D6240','D6241','D6242','D6250','D6751','D6752','D2750','D2790') then 'Bridge'
when c.service_item_id in ('5001','5000','5003','5004','5007','5011','5014','5015','5016','D5110','D5120','D5213','D5214','D5211','D5212',
                           'D5750','D5751','D5225','D5226') then 'Prosthetics'
when c.service_item_id in ('1062','D1510','D1515') then 'Space Maintainer'
else 'Unknown'
end [Procedure_Type], c.amt [amount], isnull(td.paid_amt, 0) [amount_paid] --, c.*
into #DenTmp1
from charges c
left outer join trans_detail td on c.charge_id = td.charge_id
inner join patient_encounter pe on c.source_id = pe.enc_id
inner join location_mstr l on pe.location_id = l.location_id
inner join provider_mstr pr on pe.rendering_provider_id = pr.provider_id  
inner join person p on pe.person_id = p.person_id
inner join (select distinct service_item_id, description from service_item_mstr where time_to_perform is not null) sm on c.cpt4_code_id = sm.service_item_id
where p.last_name in ('test6','test24','test21','test23','test22') and
pe.create_timestamp  >= '12/1/2012' and
                pe.create_timestamp < DATEADD(day, 1, '12/26/2013')
and c.service_item_id in ('9992','3302','3400','D3330','D3310','D3320','D3331',
                           '2705','2702','2701','2071','2070','D2750','D2751','D2752','D2790','27500','D2931','D2930','D2740','D2962',
                           '2707','2708','6690','D6240','D6241','D6242','D6250','D6751','D6752','D2750','D2790',                          
                           '5001','5000','5003','5004','5007','5011','5014','5015','5016','D5110','D5120','D5213','D5214','D5211','D5212',
                           'D5750','D5751','D5225','D5226',                          
                           '1062','D1510','D1515')
 order by p.last_name, pe.create_timestamp
 
Select * from #DenTmp1

drop table #DenTmp1
Dental-Charges.xls
0
Comment
Question by:thomasm1948
[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
  • 4
  • 3
8 Comments
 
LVL 37

Expert Comment

by:ValentinoV
ID: 38746751
I'm not sure if I understand what you're asking?  From what I understand, you only want records with service_item_id that doesn't start with a D.  But your query is actually selecting them because those codes are hardcoded in the WHERE clause:

and c.service_item_id in ('9992','3302','3400','D3330','D3310','D3320','D3331',   '2705','2702','2701','2071','2070','D2750','D2751','D2752','D2790','27500','D2931','D2930','D2740','D2962',
'2707','2708','6690','D6240','D6241','D6242','D6250','D6751','D6752','D2750','D2790',
'5001','5000','5003','5004','5007','5011','5014','5015','5016','D5110','D5120','D5213','D5214','D5211','D5212',
'D5750','D5751','D5225','D5226', '1062','D1510','D1515')

Open in new window

If you don't want to get those Dxxxx codes, just remove them from that list.

To avoid hardcoding codes, you could do something like this:

and c.service_item_id not like 'D%'

Open in new window

Doing that would give you only codes that don't start with a D.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 38747746
Just as an aside:  It looks like you are in urgent need of some sort of normalization.  You need to create a couple of tables that include service items and service items types ("Root Canal", "'Crown", "Bridge", etc.)
0
 

Author Comment

by:thomasm1948
ID: 38747840
If I do not like 'D%' then I will still get the final completed procedures.  What I need to do is remove all of the completed items that are related to the one with a D-code.  For Example, the following codes show an in-process procedure for Dentures.  The patient will come in several times before the dentures (For Quadrant UM) is finaly completed:

5011      Denture Bite Registration
5015      Denture Wax In
5015      Denture Wax In
D5110      Complete Denture, Maxillary
5003      Denture Complete Final Impressions
5011      Denture Bite Registration
5000      Denture Complete Initial Impressions

In the above example all of the procedures should not be on the report.  The report should show only the procedures that are still in process and show what stage they have completed (local codes).  For Example, for a root canal still in process the report should show only:

3400      RCT Start
9992      PREPAYMENT
3302      Endo Continued Care
0
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
LVL 37

Expert Comment

by:ValentinoV
ID: 38748465
Does your data contain that relationship somehow?  I mean, how can you know what other items are related to the "D"-item?  Once you've figured that out, we can try to solve it.  Perhaps through that encounter_number field?  Once an "encounter" contains a "D"-record, none of the records related to the encounter should appear?  Or is that not how it works?
If you'd had a table that contains all procedures and related actions, I think that should help as well.

As acperkins already mentioned, some remodeling should make your queries a lot easier!
0
 

Author Comment

by:thomasm1948
ID: 38757571
Is it possible to use group filters in reporting services?  I managed to create the report and made my groups but I am having a hard time to filter out the groups.
0
 

Author Comment

by:thomasm1948
ID: 38757604
So what I am now trying to do is filter out a group that has a D-Code in SSRS.  Does anyone know how to do it
0
 
LVL 37

Accepted Solution

by:
ValentinoV earned 500 total points
ID: 38757897
For performance reasons it's better to filter on the server side (i.e. in the query).  But even when filtering in the report itself you'll be facing the same issue: you need something to filter on.  That means you need to figure out how those records are linked together.  That's something we can't help you with, you need to tell us: this field here is what the records have in common and can be used in a filter.  And only then we can try to produce a query that achieves the goal.

Does your data contain a field that indicates the procedure occurrence?  For instance, each record belonging to patient A coming in for an action that's part of procedure 1 would get the same identifier.  If patient A would come in for actions belonging to another procedure, procedure 2, those records would get another identifier.

I'll try to make it clear using your examples above:

1   5011      Denture Bite Registration
1   5015      Denture Wax In
1   5015      Denture Wax In
1   D5110     Complete Denture, Maxillary
1   5003      Denture Complete Final Impressions
1   5011      Denture Bite Registration
1   5000      Denture Complete Initial Impressions
2   3400      RCT Start
2   9992      PREPAYMENT
2   3302      Endo Continued Care

I've added a unique identifier to the data to indicate the records that belong to the same procedure.  Once you have that, you can use a statement like the following:

select <your fields> from your_data
where procedure_identifier NOT IN (select procedure_identifier from your_data where service_item_id like 'D%')

Open in new window

PS: above statement is just one of several possibilities
0
 

Author Closing Comment

by:thomasm1948
ID: 38759273
That made since.  Thank you so much.  The following is my code.  I decided to create a identifier as the quadrant

select convert(varchar,pe.create_timestamp, 101)[Encounter_Date], replace(l.location_name,'CDCR ','')[location_name], p.last_name, p.first_name, p.person_nbr,
pe.enc_nbr [encounter_number],'Dr. ' + pr.last_name [provider], c.service_item_id, sm.description, c.tooth,c.surface,
 Case when c.quadrant = '01' then 'UM'
 when c.quadrant = '02' then 'LM'
 when c.quadrant is null and c.service_item_id in ('9992','3302','3400','D3330','D3310','D3320','D3331') then 'RCT' + c.tooth
 when c.quadrant is null and c.service_item_id in ('2705','2702','2701','2071','2070','D2750','D2751','D2752','D2790','27500','D2931','D2930','D2740','D2962')then 'CRN' + c.tooth
 when c.quadrant is null and c.service_item_id in ('2707','2708','6690','D6240','D6241','D6242','D6250','D6751','D6752','D2750','D2790') then 'BR'
 when c.quadrant is null and c.service_item_id in ('1062','D1510','D1515') then 'SP' + c.tooth
 end [Quadrant],
Case when c.service_item_id in ('9992','3302','3400','D3330','D3310','D3320','D3331') then 'Root Canal'
when c.service_item_id in ('2705','2702','2701','2071','2070','D2750','D2751','D2752','D2790','27500','D2931','D2930','D2740','D2962') Then 'Crown'
when c.service_item_id in ('2707','2708','6690','D6240','D6241','D6242','D6250','D6751','D6752','D2750','D2790') then 'Bridge'
when c.service_item_id in ('5001','5000','5003','5004','5007','5011','5014','5015','5016','D5110','D5120','D5213','D5214','D5211','D5212',
                           'D5750','D5751','D5225','D5226') then 'Prosthetics'
when c.service_item_id in ('1062','D1510','D1515') then 'Space Maintainer'
else 'Unknown'
end [Procedure_Type], c.amt [amount], isnull(td.paid_amt, 0) [amount_paid] --, c.*
into #DenTmp1
from charges c
left outer join trans_detail td on c.charge_id = td.charge_id
inner join patient_encounter pe on c.source_id = pe.enc_id
inner join location_mstr l on pe.location_id = l.location_id
inner join provider_mstr pr on pe.rendering_provider_id = pr.provider_id  
inner join person p on pe.person_id = p.person_id
inner join (select distinct service_item_id, description from service_item_mstr where time_to_perform is not null) sm on c.cpt4_code_id = sm.service_item_id
where p.last_name in ('test7','test6','test24','test21','test23','test22') and
pe.create_timestamp  >= '12/1/2012' and

 
                pe.create_timestamp < DATEADD(day, 1, '12/26/2013')
and c.service_item_id in ('9992','3302','3400','D3330','D3310','D3320','D3331',
                           '2705','2702','2701','2071','2070','D2750','D2751','D2752','D2790','27500','D2931','D2930','D2740','D2962',
                           '2707','2708','6690','D6240','D6241','D6242','D6250','D6751','D6752','D2750','D2790',                          
                           '5001','5000','5003','5004','5007','5011','5014','5015','5016','D5110','D5120','D5213','D5214','D5211','D5212',
                           'D5750','D5751','D5225','D5226',                          
                           '1062','D1510','D1515')
 order by p.last_name, pe.create_timestamp
 
Select * from #DenTmp1
where Quadrant NOT IN (select Quadrant from #DenTmp1 where service_item_id like 'D%')


drop table #DenTmp1
0

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SSMS Imprt data from Excel 7 37
Database Owner 3 43
SQL Server Serialization error 8 43
get count of orders by customer Sql Server table. 3 46
A recent question popped up and the discussion heated up regarding updating a COMMENTS (TXT) field in a table using SSRS. http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/MS-SQL_Reporting/Q_27475269.html?cid=1572#a37227028 (htt…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

737 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