Solved

T-SQL Filter Groups

Posted on 2013-01-04
8
460 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
  • 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
 
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
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!

 

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

It is helpful to note: This is a cosmetic update and is not required, but should help your reports look better for your boss.  This issue has manifested itself in SSRS version 3.0 is where I have seen this behavior in.  And this behavior is only see…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

747 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