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 9054077
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T13:35:28+00:00 2026-06-16T13:35:28+00:00

I have two tables, like this: master ——— empcode INT PRIMARY KEY name VARCHAR

  • 0

I have two tables, like this:

master
---------
empcode    INT PRIMARY KEY
name       VARCHAR
dept       VARCHAR 

emp_tx
----------
empcode    INT        references MASTER(empcode)
s_date     DATETIME

The emp_tx table records the employee “in” and “out” transactions. The column s_date stores the time (as a DATETIME value) when the “in” or “out” event occurred. The transactions are recorded from the office region (through Finger Print Biometric System.)

Example data from emp_tX table:

    empcode   s_datetime
    -------   ------------------    
    1110      2012-12-12 09:31:42  (employee in  time to the office)
    1110      2012-12-12 13:34:17  (employee out time for lunch)
    1110      2012-12-12 14:00:17  (employee in  time after lunch)
    1110      2012-12-12 18:00:12  (employee out time after working hours)
    1112
    etc.

Note:
If an employee is absent from the office on a given day, then no row will be inserted into the emp_tx transaction table for that date. An absence of an employee on a given date will be indicated by a row “missing” for that employee and that date.

Can anyone help me to get a SQL Query that returns the dates that employees were absent, to produce an Employee Absent Report?

The input to the query will be two DATE values, a “from” date and a “to” date, which specifies a range of dates. The query should return all occurrences of “absence” (or, non-occurrences rather, non, when no row is found in the EMP_TX table for an empcode on any date between the “from” and “to” dates.

Expected output:

If we input ‘2012-12-12’ as the “from” date, and ‘2012-12-20’ as the “to” date, the query should return rows something like this:

Empcode  EmpName  Department  AbsentDate  TotalNoofAbsent days
-------  -------  ----------  ----------- --------------------
1110     ABC      Accounts    2012-12-12
1110     ABC      Accounts    2012-12-14                     2   
1112     xyz      Software    2012-12-19
1112     xyz      Software    2012-12-17                     2

I’ve tried this query, and I am sure it is not returning the rows I want:

select tx.date from Emp_TX as tx where Date(S_Date) not between '2012-12-23' and '2012-12-30'

Thanks.

  • 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-16T13:35:29+00:00Added an answer on June 16, 2026 at 1:35 pm

    If an “absence” is defined as the non-appearance of a row in the emp_tx table for a particular empcode for a particular date (date=midnight to midnight 24 hour period), and …

    If its acceptable to not show an “absence” for a date when there are NO transactions in the emp_tx table for that date (i.e. exclude a date when ALL empcode are absent on that date), then …

    You can get the first four columns of the specified result set with a query like this: (untested)

    SELECT m.empcode     AS `EmpCode` 
         , m.name        AS `EmpName`
         , m.dept        AS `Department`
         , d.dt          AS `AbsentDate`
      FROM ( SELECT DATE(t.s_date) AS dt
               FROM emp_tx t
              WHERE t.s_date >= '2012-12-12' 
                AND t.s_date < DATE_ADD( '2012-12-20' ,INTERVAL 1 DAY)
              GROUP BY DATE(t.s_date)
              ORDER BY DATE(t.s_date)
           ) d
     CROSS
      JOIN master m
      LEFT
      JOIN emp_tx p
        ON p.s_date >= d.dt
       AND p.s_date <  d.dt + INTERVAL 1 DAY
       AND p.empcode = m.empcode
     WHERE p.empcode IS NULL
     ORDER
        BY m.empcode
         , d.dt
    

    Getting that fifth column TotalNoofAbsent returned in the same resultset is possible, but it’s going to make that query really messy. This detail might be more efficiently handled on the client side, when processing the returned resultset.


    How the query works

    The inline view aliased as d gets us a set of “date” values that we are checking. Using the emp_tx table as a source of these “date” values is a convenient way to do this. Not the DATE() function is returning just the “date” portion of the DATETIME argument; we’re using a GROUP BY to get a distinct list of dates (i.e. no duplicate values). (What we’re after, with this inline view query, is a distinct set of DATE values between the two values passed in as arguments. There are other, more involved, ways of generating a list of DATE values.)

    As long as every “date” value that you will consider as an “absence” appears somewhere in the table (that is, at least one empcode had one transaction on each date that is of interest), and as long a the number of rows in the emp_tx table isn’t excessive, then the inline view query will work reasonably well.

    (NOTE: The query in the inline view can be run separately, to verify that the results are correct and as we expect.)

    The next step is to do take the results from the inline view and perform a CROSS JOIN operation (to generate a Cartesian product) to match EVERY empcode with EVERY date returned from the inline view. The result of this operation represents every possible occurrence of “attendance”.

    The final step in the query is to perform an “anti-join” operation, using a LEFT JOIN and a WHERE IS NULL predicate. The LEFT JOIN (outer join) returns every possible attendance occurrence (from the left side), INCLUDING those that don’t have a matching row (attendance record) from the emp_tx table.

    The “trick” is to include a predicate (in the WHERE clause) that discards all of the rows where a matching attendance record was found, so that what we are left with is all combinations of empcode and date (possible attendance occurrences) where there was NO MATCHING attendance transaction.

    (NOTE: I’ve purposefully left the references to the s_date (DATETIME) column “bare” in the predicates, and used range predicates. This will allow MySQL to make effective use of an appropriate index that includes that column.)

    If we were to wrap the column references in the predicates inside a function e.g. DATE(p.s_date), then MySQL won’t be able to make effective use of an index on the s_date column.


    As one of the comments (on your question) points out, we’re not making any distinction between transactions that mark an employee either as “coming in” or “going out”. We are ONLY looking for the existence of a transaction for that empcode in a given 24-hour “midnight to midnight” period.


    There are other approaches to getting the same result set, but the “anti-join” pattern usually turns out to give the best performance with large sets.

    For best performance, you’ll likely want covering indexes:

    ... ON master (empcode, name, dept)
    
    ... ON emp_tx (s_date, empcode)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables which looks something like this Table Queue int ID; string
I have two tables like of this structure: content (content_id, content_type, user_id, time, comment_count)
I have two MySQL tables something like this: tool_owners: tool_owners_id | user_id | ...
Let's say I have two tables that look like this: TH TH TH TH
I have two tables associated by FK. Table student is mapped like this: @Entity
I have two tables Department and Employee . Department table looks like this: ID
I have two table like this table_CN (_id, name, phone, favorite, title) table_EN (_id,
I have two tables like, CREATE TABLE [dbo].[entry] ( [id] [int] NULL, [service] [int]
I have a two tables as follows: product category(t1): id name product master(t2): id
I have two very large tables, Table1 and Table2. They look like this: Table1

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.