患者信息SQL v1

select
    CASE a.appointment_state WHEN -1 THEN '' ELSE '' END AS returnFlag,     -- 是否退号
    CASE a.is_appoint_resource WHEN 0 THEN '' WHEN 1 THEN '' END as isAppointResource,        -- 是否指定医生
    a.create_time as guaHaoTime,                   -- 挂号时间
    p.create_time as createDocTime,                -- '建档时间'
        -- 挂号实收金额
        round((f.preFee - f.discountFee - f.promotionBenefitFee - f.couponFee - f.itemBenefitFee     - f.memberCardBenefitFee - f.itemComInvBenefitFee) * f.discount,2) AS realFee,        
        round(t1.realFee,2) as menZenRealMoney, -- 门诊实收金额
    j.outpatient_number as blNumber,                            -- 病历号
    p.name as patientName,                          -- 患者姓名
    a.dept_name,                                 -- 科室名称
    a.appointment_doctor_name as doctorName,     -- 医生
    CASE a.subsequent_visit                                                 -- 初复诊
        WHEN 0 THEN '初诊' 
        WHEN 1 THEN '复诊' 
        WHEN 2 THEN '转诊' 
        WHEN 3 THEN '急诊' 
        WHEN 4 THEN '体检'
        WHEN 5 THEN '简易'
        WHEN 6 THEN '疫苗'  
    END AS isReVisit,                            -- 初复诊
    channel.name as sourceName,                  -- 信息(渠道)来源
    p.birthday,                                  -- 出生日期
   if(i.id_no is null, if(i.other_type is null, null,
        (select e.name from  `thc_warehouse`.`sys_type_info` e 
         JOIN `thc_warehouse`.`sys_type` f ON e.sys_type_id = f.id
         WHERE f.`code` = 'THC_WH_PERSON_CARD' and e.value = i.other_type)
        ), '身份证') as cardType,                            -- 证件类型
    if(i.id_no is null,i.other_no,i.id_no) as cardNo,        -- 证件号码


    p.address,    -- 住址
    p.household, -- 户籍
    p.household_address, -- 区(户口所在地)


    a.patient_phone,             -- 患者电话号
    a.creator,               -- 挂号员
        (select t.docname from (
        SELECT u.clinic_id AS clinicid, u.id AS docid,u1.property_value AS docname
        FROM thc_warehouse.staff_record u
        LEFT JOIN thc_warehouse.staff_record_property u1 ON u1.property_code = 'SXX000083' AND u.id = u1.staff_record_id
    ) t where t.docid = a.creator and a.dept_id = t.clinicid) AS creater2,
    a.appointment_starttime, -- 预约时间
        CONCAT(a.`appointment_date`," ",a.appointment_starttime) as startTime,
    CONCAT(a.`appointment_date`," ",a.appointment_endtime) as endTime,
    a.dept_id,        -- 部门ID
    a.orderId,    -- 订单ID
    a.order_item_id, -- 订单明细id
    a.medical_card_number,    -- 社保卡号  
    a.description,    -- 备注
    CASE a.data_source WHEN 1 THEN '网站' WHEN 2 THEN 'APP' END as dataSource  -- 数据来源
from `thc_arrange`.`bpm_appointment` a
inner join `thc_sob`.`bpm_service_order` b on a.orderId = b.id
inner join `thc_sob`.`bpm_service_order_item` c on c.service_order_id = b.id and c.id = a.order_item_id
inner join `thc_rcm`.`Cs_AccountBill` d on d.orderID = b.id and d.`isDelete` = 0 and d.orderSource = 1 and d.orderType = 3 and d.returnFlag = 0
inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId and e.itemClass = 1 and e.returnFlag is NULL
inner join `thc_rcm`.`Cs_SettlementDetail` f on f.accountBillID = d.id and f.accountBillDetailID = e.id
inner join `thc_rcm`.`Cs_Settlement` g on g.id = f.settlementID and g.settlementType=2 and g.`isDelete` = 0 and g.returnFlag = 0 -- 门诊挂号
inner join `thc_passport`.`patient` p on a.patient_id = p.id



left join 
(
        select 
          aa.id,
        round((ff.preFee - ff.discountFee - ff.promotionBenefitFee - ff.couponFee - ff.itemBenefitFee     - ff.memberCardBenefitFee - ff.itemComInvBenefitFee) * ff.discount,2) AS realFee
        from `thc_arrange`.`bpm_appointment` aa
        inner join `thc_sob`.`bpm_service_order` b on aa.orderId = b.id
        inner join `thc_sob`.`bpm_service_order_item` cc on cc.service_order_id =  b.id and cc.id = aa.`order_item_id`
        inner join `thc_rcm`.`Cs_AccountBill` dd on dd.orderID = b.id and dd.`isDelete` = 0 and dd.orderSource = 1 and dd.orderType = 3 and dd.returnFlag = 0
        inner join `thc_rcm`.`Cs_AccountBillDetail` ee on dd.id = ee.AccountBillId and ee.itemClass = 1 and ee.returnFlag is NULL
        inner join `thc_rcm`.`Cs_SettlementDetail` ff on ff.accountBillID = dd.id and ff.accountBillDetailID = ee.id
        inner join `thc_rcm`.`Cs_Settlement` gg on gg.id = ff.settlementID  and gg.`isDelete` = 0  and gg.returnFlag = 0 -- 门诊挂号
                                                                         and gg.settlementType=1 -- 非挂号消费类型
        where 1=1 and aa.del_flag = 0
) t1 on a.id = t1.id

left join `thc_c_union`.`member_channel` channel on a.channel_id = channel.id
left join `thc_passport`.`contact` i on a.patient_id = i.patient_id
left join `thc_passport`.`patient_org` j on a.patient_id = j.patient_id

猜你喜欢

转载自www.cnblogs.com/guchunchao/p/10164925.html
v1