Click here to Skip to main content
15,891,943 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm trying to display the list of employee details (id, code, name, timein, timeOut, NumberOfWorkedDays) from 2 tables "Employee" and "Attendance": I try this query but it gives a wrong message "the subquery returned more than 1 value" (the number of worked days should be in a specific month)

SQL
SELECT  MST_EMPLOYEE.[ID] as ID
      ,[EMPCODE]

      ,[EMPFNAME]+' '+[EMPMNAME]+' '+[EMPLNAME] AS EMPNAME
     ,(SELECT DESIGNATION FROM MST_DESIGNATION WHERE ID=
     (SELECT TOP(1) DESIGNATIONID FROM EMP_PROFESSIONALDETAILS WHERE EMPID=MST_EMPLOYEE.ID ORDER BY HIREDATE DESC)) AS DESIGNATION
    , min(Times_IN) as TimeIn , max(Times_Out) as TimeOut,
    (select count (distinct _Date) from Emp_Attendance  where  month (_date)=6 group by EmpID)


  FROM [MST_EMPLOYEE] inner join Emp_Attendance on Emp_Attendance.EmpID=MST_Employee.ID
 where month(Times_IN) = 6

 group by MST_Employee.ID, MST_Employee.EmpCode, EmpFName, EmpMName, EmpLName








Any solution please ?
Posted
Updated 11-Jun-15 0:35am
v4
Comments
Herman<T>.Instance 11-Jun-15 6:36am    
the GROUP BY EmpID is the problem. You get a full list returned. so...
WHERE MONTH(_date)= 6 AND EmpID = MST_EMPLOYEE.[ID]
Should give release
Member 10286520 11-Jun-15 6:44am    
Thank you very much :) it works successfully :)

1 solution

Why not using another join instead of Sub Query

SQL
SELECT  MST_EMPLOYEE.[ID] as ID
      ,[EMPCODE],[EMPFNAME]+' '+[EMPMNAME]+' '+[EMPLNAME] AS EMPNAME
     ,DESIGNATION
    , min(Times_IN) as TimeIn , max(Times_Out) as TimeOut,
    (select count (distinct _Date) from Emp_Attendance  where  month (_date)=6 group by EmpID)
  FROM [MST_EMPLOYEE] inner join Emp_Attendance on Emp_Attendance.EmpID=MST_Employee.ID
INNER JOIN EMP_PROFESSIONALDETAILS ON MST_EMPLOYEE.ID=EMP_PROFESSIONALDETAILS.EMPID
INNER JOIN MST_DESIGNATION ON MST_DESIGNATION.ID=EMP_PROFESSIONALDETAILS.DESIGNATIONID
 where month(Times_IN) = 6
 group by MST_Employee.ID, MST_Employee.EmpCode, EmpFName, EmpMName, EmpLName,DESIGNATION
 
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