按来源统计过渡版本

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
select * from (

select  t.*  from
    (
        select
        CONCAT(DATE_FORMAT(a.appointment_date,'%Y-%m'),"") as monthDate,
        CONCAT(DATE_FORMAT(a.appointment_date,'%Y-%m-%d'),"") as dateDate,     -- 按日期还是月份展示
        channel.name as name
        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 -- 门诊挂号
        left join `thc_sob`.`bpm_serv_provider` h on a.dept_id = h.id
        left join `thc_c_union`.`member_channel` channel on a.channel_id = channel.id
          where  a.del_flag = 0 and g.payStatus = 2
    ) t
    where 1=1
    and t.dateDate >= '2019-01-08'
    and t.dateDate <= '2019-01-08'

) t0 
where t

2019-01-09上午版本,SQL对,但帆软未出结果版

select  
    -- t.date,
    -- t.dateMonth,
    ${if(dateType == 1,'dateMonth as date','date')},
    name,
    count(name)
from
    (
        select
        CONCAT(DATE_FORMAT(a.appointment_date,'%Y-%m'),"") as dateMonth,     -- 按日期还是月份展示
        a.appointment_date as date, -- 保留做条件比较
        IFNULL(channel.name,"") as name
        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 -- 门诊挂号
        left join `thc_sob`.`bpm_serv_provider` h on a.dept_id = h.id
        left join `thc_c_union`.`member_channel` channel on a.channel_id = channel.id
        where  a.del_flag = 0 and g.payStatus = 2
        -- and a.appointment_date >= '2019-01-08'
        -- and a.appointment_date <= '2019-01-08'
        ${if(dateType == 0 && len(startDay) == 0,'and a.appointment_date >= "0000-01-01" ','')} -- 按日期 默认开始时间
         ${if(dateType == 0 && len(startDay) > 0,'and a.appointment_date >= "' + startDay +'"','')} -- 按日期 选择开始时间
         ${if(dateType == 0 && len(endDay) == 0,'and a.appointment_date <= "9999-01-01"','')} -- 按日期 默认结束时间
         ${if(dateType == 0 && len(endDay) > 0,'and a.appointment_date <= "' + endDay +'"','')} -- 按日期 选择结束时间
         ${if(dateType == 1 && len(startMonth) == 0,'and a.appointment_date >= "0000-01-01"','')} -- 按月份 默认开始时间
         ${if(dateType == 1 && len(startMonth) > 0,'and a.appointment_date >= "'+startMonth+'-01"','')} -- 按日期 选择开始时间
         ${if(dateType == 1 && len(endMonth) == 0,'and a.appointment_date <= "9999-01-31"','')} -- 按月份 默认结束月份
         ${if(dateType == 1 && len(endMonth) > 0,'and a.appointment_date <= "'+endMonth+'-31"','')} -- 按月份 选择结束月份
         ${if(len(subVisit) == 0,"","and a.subsequent_visit = '" + subVisit + "'")}
              
    ) t
    where 1=1
    group by name, ${if(dateType == 1,'dateMonth','date')}
    order by t.date desc,t.name

时间单引号版本

select  
    -- t.date,
    -- t.dateMonth,
    ${if(dateType == 1,'dateMonth as date','date')},
    name,
    count(name) as num
from
    (
        select
        CONCAT(DATE_FORMAT(a.appointment_date,'%Y-%m'),"") as dateMonth,     -- 按日期还是月份展示
        a.appointment_date as date, -- 保留做条件比较
        IFNULL(channel.name,"") as name
        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 -- 门诊挂号
        left join `thc_sob`.`bpm_serv_provider` h on a.dept_id = h.id
        left join `thc_c_union`.`member_channel` channel on a.channel_id = channel.id
        where  a.del_flag = 0 and g.payStatus = 2
        -- and a.appointment_date >= '2019-01-08'
        -- and a.appointment_date <= '2019-01-08'
        ${if(dateType == 0 && len(startDay) == 0,"and a.appointment_date >= '0000-01-01' ","")} -- 按日期 默认开始时间
         ${if(dateType == 0 && len(startDay) > 0,"and a.appointment_date >= '" + startDay +"'","")} -- 按日期 选择开始时间
         ${if(dateType == 0 && len(endDay) == 0,"and a.appointment_date <= '9999-01-01'","")} -- 按日期 默认结束时间
         ${if(dateType == 0 && len(endDay) > 0,"and a.appointment_date <= '" + endDay +"'","")} -- 按日期 选择结束时间
         ${if(dateType == 1 && len(startMonth) == 0,"and a.appointment_date >= '0000-01-01'","")} -- 按月份 默认开始时间
         ${if(dateType == 1 && len(startMonth) > 0,"and a.appointment_date >= '" + startMonth + "'-01'","")} -- 按日期 选择开始时间
         ${if(dateType == 1 && len(endMonth) == 0,"and a.appointment_date <= '9999-01-31'","")} -- 按月份 默认结束月份
         ${if(dateType == 1 && len(endMonth) > 0,"and a.appointment_date <= '" + endMonth + "'-31'","")} -- 按月份 选择结束月份
         ${if(len(subVisit) == 0,"","and a.subsequent_visit = '" + subVisit + "'")}
              
    ) t
    where 1=1
    group by name, ${if(dateType == 1,'dateMonth','date')}
    order by t.date desc,t.name
原文地址:https://www.cnblogs.com/guchunchao/p/10241417.html