梦溪(提问者)select c.ranknumber,
(case when left(CONVERT(VARCHAR,a.createtime,108),2)>=09 and left(CONVERT(VARCHAR,a.createtime,108),2)<17 then \'午餐\'
when left(CONVERT(VARCHAR,a.createtime,108),2)>=17 and left(CONVERT(VARCHAR,a.createtime,108),2)<22 then \'晚餐\'
when left(CONVERT(VARCHAR,a.createtime,108),2)>=22 or left(CONVERT(VARCHAR,a.createtime,108),2)<09 then \'夜餐\' end ) shiduan ,
sum(goodsnumber*goodspriceoriginal) zong
from WaiterOrder a,WaiterOrderDetail b,(
SELECT orderid,RankNumber from JK_PayOrder
where CustomerID in (
select CustomerID from JK_Customer where Phone!=\'\' and WXOpenID like \'oj%\'
) and ordertype=\'1\' and orderid!=\'999\' --and CreateTime >\'2020-05-01\'
${IF(SDATES == \'All\' || len(SDATES)==0,\"\",\" AND CreateTime >=\'\" +SDATES+ \"\'\")}
${IF(EDATES == \'All\' || len(EDATES)==0,\"\",\" AND CreateTime <=\'\" +EDATES+ \"\'\")}
UNION
select WaiterOrderid,RankNumber from JK_MemberCardRecord
where recordtype=\'0\' and WaiterOrderid>\'999\' --and CreateTime >\'2020-05-01\'
${IF(SDATES == \'All\' || len(SDATES)==0,\"\",\" AND CreateTime >=\'\" +SDATES+ \"\'\")}
${IF(EDATES == \'All\' || len(EDATES)==0,\"\",\" AND CreateTime <=\'\" +EDATES+ \"\'\")}
and CustomerID in (
select CustomerID from JK_Customer where Phone!=\'\' and WXOpenID like \'oj%\'
)) c
where a.WaiterOrderid=c.orderid and b.WaiterOrderid=c.orderid
GROUP BY c.ranknumber,(case when left(CONVERT(VARCHAR,a.createtime,108),2)>=09 and left(CONVERT(VARCHAR,a.createtime,108),2)<17 then \'午餐\'
when left(CONVERT(VARCHAR,a.createtime,108),2)>=17 and left(CONVERT(VARCHAR,a.createtime,108),2)<22 then \'晚餐\'
when left(CONVERT(VARCHAR,a.createtime,108),2)>=22 or left(CONVERT(VARCHAR,a.createtime,108),2)<09 then \'夜餐\' end )