加入许多表获得许多值的问题

时间:2012-07-26 19:26:54

标签: sql join

这是我的剧本:

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中做过的最大努力。

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

3 个答案:

答案 0 :(得分:1)

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

 from charges c join
      person p
      on . . . .

那就是说,你的问题可能是你在同一时间加入多个维度。虽然没有明确说明,但我猜测一个人可能有多次患者遭遇,例如A,B和C.一个人也可能有多次指控,比如10,11和12。

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

换句话说,您需要确定:

  1. 验证表之间的连接键。是一个名为事务的表是否真的使用person_id?
  2. 加入了遭遇和成本
  3. 找出你在哪里获得跨产品,并分成两个子查询,然后适当地连接在一起。
  4. 我建议你从前两个表开始,看看你是否得到了预期的行数:

    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”非常好。对于patient_encounters,请考虑类似“pe”的内容,等等。

答案 1 :(得分: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

答案 2 :(得分:1)

现在你一次评论一个内连接并执行下面的查询,看看这些连接中的哪一个导致了一对多的关系...当计数给你说大约14 K时,这意味着评论表导致1到多关系。

否则最好的方法是在这些表上找到基于唯一键,主键和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

并对所有表重复上面的查询,这将让您了解费用表与其他表之间的关系类型。当然使用cpt4_code_id表示cpt4_code_mstr表,但按名称看起来这个表是主表,所以它对于费用表中的每个cpt4-code_id值都有一个符号值。

我希望它会有所帮助