Link to home
Start Free TrialLog in
Avatar of thomasm1948
thomasm1948Flag for United States of America

asked on

T-SQL Filter Groups

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
Avatar of ValentinoV
ValentinoV
Flag of Belgium image

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.
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.)
Avatar of thomasm1948

ASKER

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
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!
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.
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
ASKER CERTIFIED SOLUTION
Avatar of ValentinoV
ValentinoV
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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