1

这是我的脚本:

select c.rendering_id as prov_number, c.begin_date_of_service as date_of_service, 
    c.practice_id as group_number, v.enc_nbr as invoice, p.person_nbr as patient, 
    v.enc_nbr as invoice_number, c.charge_id as transaction_number, 
    t.med_rec_nbr as primary_mrn, p.last_name, p.first_name, 
    z.payer_id as orig_fsc_number, z.payer_id as curr_fsc_number, 
    c.location_id as location_number, c.closing_date as posting_date, 
    c.quantity as service_units, c.amt as charge_amount, 
    c.cpt4_code_id as procedure_code, r.description as procedure_name, 
    x.tran_code_id as pay_code_number, ISNULL([modifier_1],'') as modifier_code_1, 
    ISNULL([modifier_2],'') as modifier_code_2, ISNULL([modifier_3],'') as modifier_code_3, 
    ISNULL ([icd9cm_code_id],'') as dx_code_1, ISNULL ([icd9cm_code_id_2],'') as dx_code_2, 
    ISNULL ([icd9cm_code_id_3],'') as dx_code_3, ISNULL ([icd9cm_code_id_4],'') as dx_code_4

from charges c, person p, patient t, patient_encounter v, encounter_payer z, cpt4_code_mstr r, transactions x

where c.person_id = p.person_id
  and c.person_id = t.person_id
  and c.person_id = v.person_id
  and c.person_id = z.person_id
  and c.cpt4_code_id = r.cpt4_code_id
  and c.person_id = x.person_id
  and c.practice_id = '0001'
  and c.closing_date >= GetDate() - 7

我应该得到大约 14k 行,但有了这个我得到了几十万。我觉得这里应该有一个内部连接来纠正它,但我已经阅读了一堆帖子,似乎可以让它工作。它是迄今为止我在 SQL 中所做的最大的拉动。

任何帮助都会有很大帮助。

4

3 回答 3

1

现在您一次注释一个内部联接并执行下面的查询,看看这些联接中的哪一个导致了一对多的关系……当计数给您说大约 14 K 时,这意味着注释的表导致了一对多的关系。

否则最好的方法是在这些表上找到基于唯一键、主键和 FK 的关系。

select 
count(c.person_id)
from charges c 
inner join person p on c.person_id = p.person_id 
inner join patient t on c.person_id = t.person_id 
inner join patient_encounter v on c.person_id = v.person_id 
inner join encounter_payer z on c.person_id = z.person_id 
inner join cpt4_code_mstr r on c.cpt4_code_id = r.cpt4_code_id 
inner join transactions x on c.person_id = x.person_id 

where c.practice_id = '0001' 
and c.closing_date >= GetDate() - 7  

你可以试试

 select count(*) from <tablename> group by person_id having count(*) > 1

并对所有表重复上述查询,这将使您了解费用表和其他表之间的关系。Offcourse 对 cpt4_code_mstr 表使用 cpt4_code_id,但从名称上看,该表似乎是主表,因此它对于费用表中的每个 cpt4-code_id 值都有一个信号值。

我希望它会有所帮助

于 2012-07-26T20:05:51.903 回答
1

在不了解数据结构和外键关系的情况下,这个答案只是有根据的推测。但是,在回答之前,您需要学习正确的 JOIN 语法。您的查询应如下所示:

 from charges c join
      person p
      on . . . .

也就是说,您的问题可能是您同时沿多个维度加入。虽然不清楚,但我猜测一个人可能会遇到多个患者,比如 A、B 和 C。一个人也可能有多个指控,比如 10、11 和 12。

在这种情况下,您的查询将产生九行,每个组合一个。

换句话说,您需要确定:

  1. 验证表之间的连接键。一个名为 transactions 的表是否真的使用 person_id 加入了遭遇和成本?
  2. 找出你从哪里得到交叉产品,然后分成两个子查询,然后适当地连接在一起。

我建议您从前两个表开始,看看您是否获得了预期的行数:

select *
from charges c join
     person p
     on c.person_id = p.person_id
where c.practice_id = '0001' and
     c.closing_date >= GetDate() - 7

然后一次建立一个表的查询以获得您想要的结果。

最后一点,当使用表别名时,我发现使用唤起表的别名更清楚。“C”代表收费非常好。考虑像“pe”之类的东西作为患者的遭遇,等等。

于 2012-07-26T19:48:24.640 回答
1

应该是这样,或者你可以使用左连接

select c.rendering_id as prov_number, c.begin_date_of_service as date_of_service, 
c.practice_id as group_number, v.enc_nbr as invoice, p.person_nbr as patient, 
v.enc_nbr as invoice_number, c.charge_id as transaction_number, 
t.med_rec_nbr as primary_mrn, p.last_name, p.first_name, 
z.payer_id as orig_fsc_number, z.payer_id as curr_fsc_number, 
c.location_id as location_number, c.closing_date as posting_date, 
c.quantity as service_units, c.amt as charge_amount, 
c.cpt4_code_id as procedure_code, r.description as procedure_name, 
x.tran_code_id as pay_code_number, ISNULL([modifier_1],'') as modifier_code_1, 
ISNULL([modifier_2],'') as modifier_code_2, ISNULL([modifier_3],'') as modifier_code_3, 
ISNULL ([icd9cm_code_id],'') as dx_code_1, ISNULL ([icd9cm_code_id_2],'') as dx_code_2, 
ISNULL ([icd9cm_code_id_3],'') as dx_code_3, ISNULL ([icd9cm_code_id_4],'') as dx_code_4

from charges c
inner join person p on c.person_id = p.person_id
inner join patient t on c.person_id = t.person_id
inner join patient_encounter v on c.person_id = v.person_id
inner join encounter_payer z on c.person_id = z.person_id
inner join cpt4_code_mstr r on c.cpt4_code_id = r.cpt4_code_id
inner join transactions x on c.person_id = x.person_id

where c.practice_id = '0001'
and c.closing_date >= GetDate() - 7
于 2012-07-26T19:48:50.827 回答