Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I'm trying to display data from 2 tables: the first Table is "Emp_OverTime" is like :

EmployeeID       DateOvertime    DurationOfOverTime     AchievedDuration
27                22-06-2015        2                     1
27                23-06-2015        2                     2




The second table is "Emp_Attendance" like this:

EmployeeID          ModeAttendance      Date              Time  
27                   OverTimeIn         22-06-2015         17:00:00
27                   OverTimeOut        22-06-2015         18:00:00
27                   OverTimeIn         23-06-2015         19:00:00
27                   OverTimeOut        23-06-2015         20:00:00


what I need is obtaining for each date of attendance and for each employee the TimeIn , the timeOut for each overtime, the durationOfOverTime, AchievedDuration. Like Below:

EmpID Date       Duration  AchievedDuration   TimeOfOverTimeIn TimeOfOverTimeOut
27    22-06-2015    2        1                17:00:00         18:00:00
27    23-06-2015    2        2                19:00:00         20:00:00       



I try this query:

SQL
select employeeid, overtimedate, duration, achievedduration
, (select Emp_AttendanceByDevice.wHour from Emp_AttendanceByDevice
where MONTH(Date)=6 and EmployeeID=27 and InOutMode='OverTimeIn') as OverTimeIn
, (select Emp_AttendanceByDevice.wHour from Emp_AttendanceByDevice
where MONTH(Date)=6 and EmployeeID=27 and InOutMode='OverTimeOut')  as OverTimeOut
 from Emp_OverTimeScheduler
 inner join Emp_AttendanceByDevice on Emp_AttendanceByDevice.EmployeeID=Emp_OverTimeScheduler.employeeid
  where employeeid =27 and MONTH(overtimedate)=6
  group by  employeeid, overtimedate, duration, achievedduration



But it gives me an error message ; the subquery returns more than 1 value
Any Idea please ?
Posted
Updated 24-Jun-15 6:37am
v5

Try this:

SQL
--SET DATEFORMAT dmy;

DECLARE @a TABLE (EmployeeID INT, DateOvertime DATE, DurationOfOverTime INT, AchievedDuration INT)
INSERT INTO @a (EmployeeID, DateOvertime, DurationOfOverTime, AchievedDuration)
VALUES(27, '22-06-2015', 2, 1), (27, '23-06-2015', 2, 2)

DECLARE @b TABLE(EmployeeID INT, ModeAttendance VARCHAR(50), [Date] DATE, [Time] VARCHAR(50))
INSERT INTO @b (EmployeeID, ModeAttendance, [Date], [Time])
VALUES(27, 'OverTimeIn', '22-06-2015', '17:00:00'),
(27, 'OverTimeOut', '22-06-2015', '18:00:00'),
(27, 'OverTimeIn', '23-06-2015', '19:00:00'),
(27, 'OverTimeOut', '23-06-2015', '20:00:00')

SELECT t1.EmployeeID, t1.DateOvertime, t1.DurationOfOverTime, t1.AchievedDuration, t2.TimeOverTimeIn, t2.TimeOverTimeOut
FROM @a AS t1 INNER JOIN (
    SELECT EmployeeID, [Date], OverTimeIn  AS [TimeOverTimeIn],  OverTimeOut AS [TimeOverTimeOut]
    FROM (
        SELECT *
        FROM @b
    ) AS DT
    PIVOT(MAX([Time]) FOR ModeAttendance IN([OverTimeIn], [OverTimeOut])) AS DT
    ) AS t2 ON t1.EmployeeID = t2.EmployeeID AND t1.DateOvertime = t2.Date


Result:
27	2015-06-22	2	1	17:00:00	18:00:00
27	2015-06-23	2	2	19:00:00	20:00:00
 
Share this answer
 
Run your sub-queries and determine "if" it's true - or more correctly, decide what you need to do to the sub-queries WHERE clause to return only one value and that value the one that you want.

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900