Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8378327
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:54:21+00:00 2026-06-09T15:54:21+00:00

I have a table employee having columns id (primary key), *employee_name* and another table

  • 0

I have a table employee having columns id (primary key), *employee_name* and another table called employee_works with columns *employee_id* (foreign key referencing employee.id), *start_date* (datetime), *finish_date* (datetime).

Here are some datas for employee table:

 **id**   **employee_name**
 1      employee A
 2      employee B
 3      employee C
 4      employee D
 5      employee E
 6      employee F
 7      employee G

employee_works table:

1  2010-01-01 00:00:00   NULL
2  2010-01-01 00:00:00   2010-01-10 10:00:00"
2  2010-01-13 00:00:00   2010-01-15 10:00:00"
2  2010-01-31 00:00:00   NULL
4  2010-02-18 00:00:00   2011-01-31 00:00:00"
6  2010-02-18 00:00:00   NULL

NULL value means the employee still works.
I need to get a single query showing the list of persons in employee, if they worked with us, who still works in our company, who left and if possible, for how long they worked with us.
Example:

id     employee_name       status
1      Employee A       Still with us
3      Employee C       Never worked
4      Employee D       Left

My attempt:

SELECT emp.id,emp.name,
CASE
WHEN occ.finish_date is NULL and occ.start_date is NOT NULL THEN 'Still working'
WHEN occ.finish_date is NULL and occ.start_date is NULL THEN 'Never Worked'
WHEN occ.finish_date is NOT  NULL and occ.start_date is NOT NULL THEN 'Left'
END
AS status

FROM employee AS emp
LEFT JOIN employee_works AS occ ON emp.id=occ.employee_id 
GROUP BY emp.id, occ.finish_date

I also want to get the total no of days the employees have worked in another column?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T15:54:23+00:00Added an answer on June 9, 2026 at 3:54 pm

    The problem is that you have a group by but no aggregations for the definition of status. Mysql does not give you a syntax error. Instead, it gives you a random status:

    Try something like this instead:

    select id, name,
           (CASE WHEN statusint = 3
                 THEN 'Still working'
                 WHEN statusint = 1 or statusint is null
                 THEN 'Never Worked'
                 WHEN statusint = 2
                 THEN 'Left'
            END) AS status,
           days_worked
    from (SELECT emp.id, emp.name,
                 max(CASE WHEN occ.departure_date is NULL and occ.start_date is NOT NULL
                          THEN 3
                          WHEN occ.departure_date is NULL and occ.start_date is NULL
                          THEN 1
                          WHEN occ.departure_date is NOT NULL and occ.start_date is NOT NULL
                          THEN 2
                     END) AS statusint,
                 sum(datediff(coalesce(departure_date, curdate()), occ.start_date
                    ) as days_worked
          FROM employee emp LEFT JOIN
               employee_works occ
               ON emp.id=occ.employee_id
          GROUP BY emp.id, emp.name
         ) eg
    

    This “feature” of mysql is called hidden columns. Folks who write mysql (and many who use it) think this is a great feature. Many people who use other databases just scratch their heads and wonder why any database would act so strangely.

    By the way, you should check if someone who is employeed multiple times gets assigned a new id. If so, your query might need more advanced name matching methods.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have one table CSBCA1_5_FPCIC_2012_EES207201222743 , having two columns employee_id and employee_name I have
I have main table called 'Employee' and another slave table called 'EmployeeTypes' that has
I have a table called employee_salary , having two columns(emp_id, emp_salary) in it. I
I have a table: Employee (employeeID) EmployeeRank (rankID, employeeID) Now I have another table
I have a table called Employee with the following fields: EmpID Salary Name I
I am having trouble with a SQL join question. I have a table EMPLOYEE
I have two columns in my Table called as Workers - Technicians. In Workers
I have a table of Employees which consists of two columns : Employee and
I have a table with 2 columns, say Empname, DepartmentName where one employee may
Suppose I have table Person table Employee, which inherits Person. I want to get

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.