sb.AppendFormat(" AND A.StartDate<'{0}' AND A.EndDate>'{0}' ",dateStr);
if(!string.IsNullOrEmpty(StuAccountId))
{
sb.AppendFormat(" AND NOT EXISTS(SELECT 1 FROM rb_guestsurvey WHERE UserType=2 AND ActivitySurveyId=A.ID AND UserId={0}) ",StuId);
//sb.AppendFormat(" AND NOT EXISTS(SELECT 1 FROM rb_guestsurvey WHERE UserType=2 AND ActivitySurveyId=A.ID AND UserId={0}) ", StuAccountId);
sb.AppendFormat(" AND EXISTS(SELECT 1 FROM RB_ActivitySurvey_Guest WHERE GuestType=2 AND IsFinish=0 AND SurveyId=A.ID AND GuestId={0} AND StartTime<'{1}' AND EndTime>'{1}' )",StuAccountId,dateStr);
SELECT IFNULL(SUM(b.CommissionMoeny),0) as TotalEmployee,IFNULL(SUM(c.CommissionMoeny),0) as TotalCustomer,a.FinanceIds
,IFNULL(SUM(d.CommissionMoeny),0) as TotalInternal,IFNULL(SUM(e.CommissionMoeny),0) as TotalTrans
SELECT IFNULL(b.CommissionMoeny,0) as TotalEmployee,IFNULL(c.CommissionMoeny,0) as TotalCustomer,a.FinanceIds
,IFNULL(d.CommissionMoeny,0) as TotalInternal,IFNULL(e.CommissionMoeny,0) as TotalTrans
,a.Id,a.BatchName,TotalMoney,a.CreateBy,a.CreateDate,DATE_FORMAT(a.CreateDate,'%Y-%m-%d %H:%i:%S') as CreateDateStr
FROM RB_ReturnMoney_Batch a LEFT JOIN
(SELECT SUM(CommissionMoeny) as CommissionMoeny,BatchId FROM rb_order_returncomission where OrderSourceType=1 and `Status` in(1,2) GROUP BY OrderSourceType,BatchId) as b
(SELECT SUM(CommissionMoeny) as CommissionMoeny,BatchId FROM rb_order_returncomission WHERE OrderSourceType=1 AND `Status` in(1,2) GROUP BY OrderSourceType,BatchId) as b
on a.Id=b.BatchId
LEFT JOIN
(SELECT SUM(CommissionMoeny) as CommissionMoeny,BatchId FROM rb_order_returncomission where OrderSourceType=2 and `Status` in(1,2) GROUP BY OrderSourceType,BatchId) as c
(SELECT SUM(CommissionMoeny) as CommissionMoeny,BatchId FROM rb_order_returncomission WHERE OrderSourceType=2 AND `Status` in(1,2) GROUP BY OrderSourceType,BatchId) as c
on a.Id=c.BatchId
LEFT JOIN
(SELECT SUM(CommissionMoeny) as CommissionMoeny,BatchId FROM rb_order_returncomission where OrderSourceType=3 and `Status` in(1,2) GROUP BY OrderSourceType,BatchId) as d
(SELECT SUM(CommissionMoeny) as CommissionMoeny,BatchId FROM rb_order_returncomission WHERE OrderSourceType=3 AND `Status` in(1,2) GROUP BY OrderSourceType,BatchId) as d
on a.Id=d.BatchId
LEFT JOIN
(SELECT SUM(CommissionMoeny) as CommissionMoeny,BatchId FROM rb_order_returncomission where OrderSourceType=4 and `Status` in(1,2) GROUP BY OrderSourceType,BatchId) as e
(SELECT SUM(CommissionMoeny) as CommissionMoeny,BatchId FROM rb_order_returncomission WHERE OrderSourceType=4 AND `Status` in(1,2) GROUP BY OrderSourceType,BatchId) as e