按来源统计过渡版本

select 
    channel.name as name, 
    DATE_FORMAT(a.appointment_date,${if(dateType == 1,"'%Y-%m'","'%Y-%m-%d'")}) as date,     -- 按日期还是月份展示
    IFNULL(count(1),0) as num
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
inner join `thc_rcm`.`Cs_AccountBillDetail` e on d.id = e.AccountBillId
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
left join `thc_c_union`.`member_channel` channel on a.channel_id = channel.id
where 1=1 
 and a.del_flag = 0 
 and d.isDelete = 0 and d.orderSource = 1 and d.orderType = 3 and d.returnFlag = 0
 and e.itemClass = 1 and e.returnFlag is NULL
 and g.settlementType=2 and g.`isDelete` = 0  and g.returnFlag = 0 -- 门诊挂号
 and g.payStatus = 2
${if(dateType == 0 && len(startDay) == 0,'and DATE_FORMAT(a.appointment_date,"%Y-%m-%d") >= DATE_FORMAT("0000-01-01","%Y-%m-%d") ','')} -- 按日期 默认开始时间
${if(dateType == 0 && len(startDay) > 0,'and DATE_FORMAT(a.appointment_date,"%Y-%m-%d") >= DATE_FORMAT("' + startDay +'","%Y-%m-%d")','')} -- 按日期 选择开始时间
${if(dateType == 0 && len(endDay) == 0,'and DATE_FORMAT(a.appointment_date,"%Y-%m-%d") <= DATE_FORMAT("9999-01-01","%Y-%m-%d") ','')} -- 按日期 默认结束时间
${if(dateType == 0 && len(endDay) > 0,'and DATE_FORMAT(a.appointment_date,"%Y-%m-%d") <= DATE_FORMAT("' + endDay +'","%Y-%m-%d")','')} -- 按日期 选择结束时间
${if(dateType == 1 && len(startMonth) == 0,'and DATE_FORMAT(a.appointment_date,"%Y-%m") >= "0000-01"','')} -- 按月份 默认开始时间
${if(dateType == 1 && len(startMonth) > 0,'and DATE_FORMAT(a.appointment_date,"%Y-%m") >= "'+startMonth+'"','')} -- 按日期 选择开始时间
${if(dateType == 1 && len(endMonth) == 0,'and DATE_FORMAT(a.appointment_date,"%Y-%m") <= "9999-01"','')} -- 按月份 默认结束月份
${if(dateType == 1 && len(endMonth) > 0,'and DATE_FORMAT(a.appointment_date,"%Y-%m") <= "'+endMonth+'"','')} -- 按月份 选择结束月份
${if(len(subVisit) == 0,"","and a.subsequent_visit = '" + subVisit + "'")}
-- and channel.name is not null
group by  date_format(a.appointment_date, ${if(dateType == 1,"'%Y-%m'","'%Y-%m-%d'")}), channel.name     -- 按日期还是月份分组查询
order by a.appointment_date DESC, data_source asc

猜你喜欢

转载自www.cnblogs.com/guchunchao/p/10241417.html