In one of the case statements below I enter a "Y" if it is a new patient. However, I really only want to count them one time per date (c.begin_date_of_service). I wasn't sure how to do that so I tried to do a distinct count in another case statement but it probably isn't allowed (I got an error - incorrect syntax near the keyword 'distinct').
I would either like to count the new patients in SQL or to mark only one per date (c.begin_date_of_service) with a Y or a 1 and I can count those later in Crystal.
Thanks -
I would either like to count the new patients in SQL or to mark only one per date (c.begin_date_of_service) with a Y or a 1 and I can count those later in Crystal.
Thanks -
Code:
Select distinct c.charge_id as ChgID,
per.person_id as PerID,
pe.enc_id as PEenc_id,
c.source_id as ChgEnc_id,
ep.payer_id as EncPayerID,
pe.cob1_payer_id as EncPrimaryPayer,
pe.enc_nbr as EncNbr,
c.create_Timestamp as ChgCreateDate,
c.begin_date_of_service as DOS,
c.amt as ChgAmt,
Case
When per.create_timestamp between '20051212' and '20051213' then 'Y'
Else 'N'
End as NewPtY_or_N,
Case
When EncPrimPayer.payer_name is not null then EncPrimPayer.payer_name --when there is an encounter_payer fill in the one with cob=1
When pe.cob1_payer_id is null then 'Self'
Else (
select payer_name --need this part to fill in primary payer where pmts made from secondary payer
from payer_mstr inner join encounter_payer on payer_mstr.payer_id = encounter_payer.payer_id
where encounter_payer.enc_id = ep.enc_id and cob = 1
)
End as EncounterChgPayer,
Case
When pe.cob1_payer_id is Null then 'SP'
When pe.cob1_payer_id is not Null then
(
SELECT [mstr_list_item_desc]
FROM [mstr_lists] inner join payer_mstr on mstr_lists.mstr_list_item_id = payer_mstr.financial_class
inner join encounter_payer on payer_mstr.payer_id = encounter_payer.payer_id
WHERE encounter_payer.enc_id = ep.enc_id and cob = 1
)
End As [ChargeFC],
Case
When per.create_timestamp between '20051212' and '20051213' then (distinct count(cast(per.person_id as varchar(50))) )
End as PtCount
FROM person per
left outer join patient_encounter pe on (pe.person_id = per.person_id and pe.practice_id = per.practice_id)
left outer join charges c on (c.person_id = pe.person_id and c.source_id = pe.enc_id and pe.practice_id = c.practice_id)
left outer join payer_mstr EncPrimPayer on EncPrimPayer.payer_id = pe.cob1_payer_id--(EncPrimPayer.payer_id = ep.payer_id and cob=1)
left outer join encounter_payer ep on (pe.enc_id = ep.enc_id and pe.person_id = ep.person_id and EncPrimPayer.payer_id = ep.payer_id)
left outer join mstr_lists ml on (ml.mstr_list_item_id = EncPrimPayer.financial_class)
WHERE c.begin_date_of_service between '20051212' and '20051213'