** 它在第一组上按 backet 显示错误 **
select attnDate,workLoc,swipeType, fromTime,toTime,timeInterval, sum (ECount1+ECount2) as EmpCount ,
EmpCode from( SELECT attn_dt AS attnDate, att.work_loc as workLoc , vEmpDtl.emp_Code as EmpCode ,
case when convert(time,in_Ts) between '7:00' and '10:30' then 'IN' when convert(time,in_Ts2)
between '7:00' and '10:30' then 'IN' else ' ' end as swipeType , count(in_Ts) as ECount1 , count(in_Ts2) as ECount2 ,
td.from_time as fromTime, td.to_time as toTime, td.time_interval as timeInterval, in_Ts as inTime, out_ts as OutTime
from emp_attn att cross Join time_details td Inner Join v_emp_dtls vEmpDtl on att.emp_code = vEmpDtl.emp_code
join emp_mst mst WITH (NOLOCK) on mst.emp_code = vEmpDtl.emp_code join
( select distinct empRep.emp_code from emp_reporting_structure empRep where empRep.report_to = '18501')
o on o.emp_code = vEmpDtl.emp_code
WHERE attn_dt BETWEEN '2023-02-01' AND '2023-02-28'
and vEmpDtl.emp_type = 'COMMON' and vEmpDtl.emp_loc_type = 'KSACORP'
and vEmpDtl.work_loc_id = '300000001672100' and vEmpDtl.nationality = 'Sri Lanka' and vEmpDtl.payroll_code = 'KSA Retail Payroll' and vEmpDtl.country_code = '6486'
and vEmpDtl.region = 'RIY'
and (convert(time,in_Ts) between convert(time,td.from_time) and convert(time,td.to_time) or
convert(time,in_Ts2) between convert(time,td.from_time) and convert(time,td.to_time)) and (convert(time,in_Ts)
between '7:00' and '10:30' or convert(time,in_Ts2) between '7:00' and '10:30')
and vEmpDtl.emp_code NOT IN (SELECT distinct emp_code from exclude_emp where exclusion_type ='NO_ATTENDANCE') AND mst.contract is null AND vEmpDtl.employment_status = 'All'
--- group BY att.attn_dt desc ,att.emp_code
) AnyNameYouLikeButYouHaveToGiveOne
group by attn_dt , att.work_loc ,in_Ts ,out_ts ,in_Ts2, out_ts2 ,from_time ,to_time , time_interval , vEmpDtl.emp_Code )
aaa group by attnDate,timeInterval,workLoc ,swipeType,fromTime,toTime , ECount1,ECount2 , EmpCode
请建议更改上述查询中的代码
查询在第一个 GROUP BY 子句中有错误。在 swipeType 之后和 FROM 之前有额外的逗号。请删除这些逗号,查询应该可以正常运行。
SELECT attnDate, workLoc, swipeType, fromTime, toTime, timeInterval, SUM(ECount1+ECount2) AS EmpCount, EmpCode
来自( 选择 attn_dt 作为 attnDate,att.work_loc 作为 workLoc,vEmpDtl.emp_Code 作为 EmpCode, CONVERT(time, in_Ts) BETWEEN '7:00' AND '10:30' THEN 'IN' 时的情况 WHEN CONVERT(time, in_Ts2) BETWEEN '7:00' AND '10:30' 然后 'IN' 别的 ' ' 结束为 swipeType, 计数(in_Ts)作为 ECount1, 计数(in_Ts2)作为 ECount2, td.from_time AS fromTime, td.to_time AS toTime, td.time_interval AS 时间间隔, in_Ts AS inTime, out_ts AS 超时时间 来自 emp_attn att 交叉连接 time_details td 内部连接 v_emp_dtls vEmpDtl ON att.emp_code = vEmpDtl.emp_code JOIN emp_mst mst WITH (NOLOCK) ON mst.emp_code = vEmpDtl.emp_code 加入 ( 选择不同的 empRep.emp_code FROM emp_reporting_structure empRep WHERE empRep.report_to = '18501' ) o ON o.emp_code = vEmpDtl.emp_code “2023-02-01”和“2023-02-28”之间的 attn_dt AND vEmpDtl.emp_type = 'COMMON' AND vEmpDtl.emp_loc_type = 'KSACORP' AND vEmpDtl.work_loc_id = '300000001672100' AND vEmpDtl.nationality = '斯里兰卡' AND vEmpDtl.payroll_code = 'KSA 零售薪资' AND vEmpDtl.country_code = '6486' AND vEmpDtl.region = 'RIY' 和 ( 转换(时间,in_Ts)在转换(时间,td.from_time)和转换(时间,td.to_time)之间 或在转换(时间,td.from_time)和转换(时间,td.to_time)之间转换(时间,in_Ts2) ) 和 ( 在“7:00”和“10:30”之间转换(时间,in_Ts) 或在“7:00”和“10:30”之间转换(时间,in_Ts2) ) AND vEmpDtl.emp_code 不在 ( 选择不同的 emp_code 来自 exclude_emp WHERE exclusion_type ='NO_ATTENDANCE' ) AND mst.contract 为空 AND vEmpDtl.employment_status = '全部' GROUP BY attn_dt, att.work_loc, in_Ts, out_ts, in_Ts2, out_ts2, td.from_time, td.to_time, td.time_interval, vEmpDtl.emp_Code ) 啊啊 GROUP BY attnDate、timeInterval、workLoc、swipeType、fromTime、toTime、EmpCode、ECount1、ECount2