WITH cte1
AS (SELECT
tbl.EmpID,
CASE
WHEN MIN(tbl.LeaveTypeId) = 'a' THEN CONVERT(bit, 1)
ELSE CONVERT(bit, 0)
END AS 'Absent',
CASE
WHEN (MIN(tbl.LeaveTypeId) IS NOT NULL OR
MIN(tbl.LeaveTypeId) <> '') THEN MIN(tbl.LeaveDesc)
ELSE NULL
END AS 'Leave',
CASE
WHEN (MIN(tbl.outime) IS NULL) AND
(MIN(tbl.LeaveTypeId) IS NULL) THEN CONVERT(bit, 1)
ELSE CONVERT(bit, 0)
END AS 'InOut',
CASE
WHEN CONVERT(char(5), MIN(tbl.InTime), 108) <> '00:00' THEN MIN(tbl.InTime)
ELSE NULL
END AS 'S1IN',
MIN(tbl.outime) AS 'S1OUT',
CASE
WHEN MIN(tbl.InTime) = MAX(tbl.InTime) THEN NULL
ELSE MAX(tbl.InTime)
END AS 'S2IN',
CASE
WHEN MIN(tbl.outime) = MAX(tbl.outime) THEN NULL
ELSE MAX(tbl.outime)
END AS 'S2OUT',
CASE
WHEN MIN(tbl.InTime) > DATEADD(dd, 0, DATEDIFF(dd, 0, MIN(tbl.P_Date))) + ' ' + CONVERT(varchar(10), CONVERT(time, DATEADD(n, MIN(sd.S1SL), MIN(sd.S1Start)))) THEN DATEDIFF(n, DATEADD(dd, 0, DATEDIFF(dd, 0, MIN(tbl.P_Date))) + ' ' + CONVERT(varchar(10), CONVERT(time, DATEADD(n, MIN(sd.S1SL), MIN(sd.S1Start)))), MIN(InTime))
ELSE 0
END AS 'LateBy',
CASE
WHEN MAX(tbl.OuTime) < DATEADD(dd, 0, DATEDIFF(dd, 0, MIN(tbl.P_Date))) + ' ' + CONVERT(varchar(10), CONVERT(time, DATEADD(n, -MIN(sd.S2EL), MIN(sd.S2End)))) THEN DATEDIFF(n, MAX(OuTime), DATEADD(dd, 0, DATEDIFF(dd, 0, MAX(tbl.P_Date))) + ' ' + CONVERT(varchar(10), CONVERT(time, DATEADD(n, -MIN(sd.S2EL), MIN(sd.S2End)))))
ELSE 0
END AS 'EarlyBy',
MIN(emp.EmpFullName) AS 'EmpFullName',
MIN(tbl.P_Date) AS 'P_Date',
MIN(emp.nation_id) AS 'nation_id',
MIN(emp.dept_id) AS 'dept_id',
MIN(emp.desig_id) AS 'designation_id',
MIN(emp.tradecd) AS 'tradecd',
MIN(emp.comp_id) AS 'comp_id',
MIN(emp.section_id) AS 'section_id',
MIN(emp.location_id) AS 'location_id',
MIN(emp.emplcatgry) AS 'emplcatgry',
(SELECT
HODName
FROM Department
WHERE Dept_id = MIN(emp.dept_id))
AS 'deptHOD',
(SELECT
HODEmail
FROM Department
WHERE Dept_id = MIN(emp.dept_id))
AS 'deptEmail',
(SELECT
ManagerName
FROM Location
WHERE Location_id = MIN(emp.location_id))
AS 'LocManager',
(SELECT
Email
FROM Location
WHERE Location_id = MIN(emp.location_id))
AS 'LocEmail',
(SELECT
LocationName
FROM Location
WHERE Location_id = MIN(emp.location_id))
AS 'Location',
(SELECT
LocType
FROM Location
WHERE Location_id = MIN(emp.location_id))
AS 'LocType',
(SELECT
MinWorkingHour
FROM Location
WHERE Location_id = MIN(emp.location_id))
AS 'Minworkhr'
FROM tblattendance tbl
INNER JOIN employees emp
ON emp.empid = tbl.empid
INNER JOIN Company cmp
ON cmp.comp_id = emp.comp_id
INNER JOIN ShiftDetails sd
ON emp.ShiftCode = ISNULL((SELECT
ShiftCode
FROM Shift_Calender
WHERE nFingerPrintId = emp.empid
AND C_Date = tbl.p_date)
, sd.ShiftCode)
AND sd.DayOfWeek = DATENAME(dw, tbl.p_date)
WHERE CONVERT(varchar(10), tbl.p_date, 112) BETWEEN '20151101' AND '20151101'
AND emp.status <> 'terminated'
GROUP BY tbl.p_date,
tbl.EmpID)
SELECT
*,
CASE
WHEN S1IN IS NOT NULL AND
S1OUT IS NOT NULL AND
S2IN IS NOT NULL AND
S2OUT IS NOT NULL THEN DATEDIFF(n, S1IN, S1OUT) + DATEDIFF(n, S2IN, S2OUT)
WHEN S1IN IS NOT NULL AND
S1OUT IS NOT NULL AND
S2IN IS NULL AND
S2OUT IS NULL THEN DATEDIFF(n, S1IN, S1OUT)
ELSE 0
END AS 'TotalHrs'
FROM cte1
WHERE (Absent = 1
OR InOut = 1
OR LateBy > 0
OR EarlyBy > 0)
AND LocType = 'Retail Store'
AND Location =
CASE 'Lite N Lamps'
WHEN '0' THEN Location
ELSE 'Lite N Lamps'
END
这个查询我想再添加一个像
这样的条件AND LocType =&#39;零售店&#39;和TotalHrs = 0 ,但显示错误
无效的列名称&#39; TotalHrs&#39;。
我如何解决这个问题。如果有任何帮助非常明显,请提前谢谢
答案 0 :(得分:0)
您可以使用以下多个CTE:
WITH cte1
AS (SELECT
tbl.EmpID,
CASE
WHEN MIN(tbl.LeaveTypeId) = 'a' THEN CONVERT(bit, 1)
ELSE CONVERT(bit, 0)
END AS 'Absent',
CASE
WHEN (MIN(tbl.LeaveTypeId) IS NOT NULL OR
MIN(tbl.LeaveTypeId) <> '') THEN MIN(tbl.LeaveDesc)
ELSE NULL
END AS 'Leave',
CASE
WHEN (MIN(tbl.outime) IS NULL) AND
(MIN(tbl.LeaveTypeId) IS NULL) THEN CONVERT(bit, 1)
ELSE CONVERT(bit, 0)
END AS 'InOut',
CASE
WHEN CONVERT(char(5), MIN(tbl.InTime), 108) <> '00:00' THEN MIN(tbl.InTime)
ELSE NULL
END AS 'S1IN',
MIN(tbl.outime) AS 'S1OUT',
CASE
WHEN MIN(tbl.InTime) = MAX(tbl.InTime) THEN NULL
ELSE MAX(tbl.InTime)
END AS 'S2IN',
CASE
WHEN MIN(tbl.outime) = MAX(tbl.outime) THEN NULL
ELSE MAX(tbl.outime)
END AS 'S2OUT',
CASE
WHEN MIN(tbl.InTime) > DATEADD(dd, 0, DATEDIFF(dd, 0, MIN(tbl.P_Date))) + ' ' + CONVERT(varchar(10), CONVERT(time, DATEADD(n, MIN(sd.S1SL), MIN(sd.S1Start)))) THEN DATEDIFF(n, DATEADD(dd, 0, DATEDIFF(dd, 0, MIN(tbl.P_Date))) + ' ' + CONVERT(varchar(10), CONVERT(time, DATEADD(n, MIN(sd.S1SL), MIN(sd.S1Start)))), MIN(InTime))
ELSE 0
END AS 'LateBy',
CASE
WHEN MAX(tbl.OuTime) < DATEADD(dd, 0, DATEDIFF(dd, 0, MIN(tbl.P_Date))) + ' ' + CONVERT(varchar(10), CONVERT(time, DATEADD(n, -MIN(sd.S2EL), MIN(sd.S2End)))) THEN DATEDIFF(n, MAX(OuTime), DATEADD(dd, 0, DATEDIFF(dd, 0, MAX(tbl.P_Date))) + ' ' + CONVERT(varchar(10), CONVERT(time, DATEADD(n, -MIN(sd.S2EL), MIN(sd.S2End)))))
ELSE 0
END AS 'EarlyBy',
MIN(emp.EmpFullName) AS 'EmpFullName',
MIN(tbl.P_Date) AS 'P_Date',
MIN(emp.nation_id) AS 'nation_id',
MIN(emp.dept_id) AS 'dept_id',
MIN(emp.desig_id) AS 'designation_id',
MIN(emp.tradecd) AS 'tradecd',
MIN(emp.comp_id) AS 'comp_id',
MIN(emp.section_id) AS 'section_id',
MIN(emp.location_id) AS 'location_id',
MIN(emp.emplcatgry) AS 'emplcatgry',
(SELECT
HODName
FROM Department
WHERE Dept_id = MIN(emp.dept_id))
AS 'deptHOD',
(SELECT
HODEmail
FROM Department
WHERE Dept_id = MIN(emp.dept_id))
AS 'deptEmail',
(SELECT
ManagerName
FROM Location
WHERE Location_id = MIN(emp.location_id))
AS 'LocManager',
(SELECT
Email
FROM Location
WHERE Location_id = MIN(emp.location_id))
AS 'LocEmail',
(SELECT
LocationName
FROM Location
WHERE Location_id = MIN(emp.location_id))
AS 'Location',
(SELECT
LocType
FROM Location
WHERE Location_id = MIN(emp.location_id))
AS 'LocType',
(SELECT
MinWorkingHour
FROM Location
WHERE Location_id = MIN(emp.location_id))
AS 'Minworkhr'
FROM tblattendance tbl
INNER JOIN employees emp
ON emp.empid = tbl.empid
INNER JOIN Company cmp
ON cmp.comp_id = emp.comp_id
INNER JOIN ShiftDetails sd
ON emp.ShiftCode = ISNULL((SELECT
ShiftCode
FROM Shift_Calender
WHERE nFingerPrintId = emp.empid
AND C_Date = tbl.p_date)
, sd.ShiftCode)
AND sd.DayOfWeek = DATENAME(dw, tbl.p_date)
WHERE CONVERT(varchar(10), tbl.p_date, 112) BETWEEN '20151101' AND '20151101'
AND emp.status <> 'terminated'
GROUP BY tbl.p_date,
tbl.EmpID)
, cte2 AS
(
SELECT
*,
CASE
WHEN S1IN IS NOT NULL AND
S1OUT IS NOT NULL AND
S2IN IS NOT NULL AND
S2OUT IS NOT NULL THEN DATEDIFF(n, S1IN, S1OUT) + DATEDIFF(n, S2IN, S2OUT)
WHEN S1IN IS NOT NULL AND
S1OUT IS NOT NULL AND
S2IN IS NULL AND
S2OUT IS NULL THEN DATEDIFF(n, S1IN, S1OUT)
ELSE 0
END AS 'TotalHrs'
FROM cte1
WHERE (Absent = 1
OR InOut = 1
OR LateBy > 0
OR EarlyBy > 0)
AND LocType = 'Retail Store'
AND Location =
CASE 'Lite N Lamps'
WHEN '0' THEN Location
ELSE 'Lite N Lamps'
END
)
SELECT *
FROM cte2
WHERE TotalHrs=0 --your alias condition or you can move all where clause here