Results 1 to 12 of 12

Thread: [RESOLVED] How to get all employees, who have NOT had attendance entries?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2006
    Location
    Anchorage, Alaska
    Posts
    545

    Resolved [RESOLVED] How to get all employees, who have NOT had attendance entries?

    I'm trying to figure out how to get all employee names, that don't have a record in tblAttendance, for a specific period?

    I can't think of a way to say, where data not found, or where entry not found?

    I'm guessing i'll have to do a Union to get all employee names. Then maybe do a count, or something. Combine the two and if the employee name has 0 for the count, they had perfect attendace?

    Can I get a few pointers in the correct direction, I'd like to play and see if I can figure it out. I'll post once I give up on trying to see if someone has an answer.

    Please don't just post the solution yet. =)
    Please RATE posts, click the RATE button to the left under the Users Name.

    Once your thread has been answered, Please use the Thread Tools and select RESOLVED so everyone knows your question has been answered.


    "As I look past the light, I see the world I wished tonight, never the less, sleep has come, and death shall soon follow..." © 1998 Jeremy J Swartwood

  2. #2
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: How to get all employees, who have NOT had attendance entries?

    You won't need to use a Union. Check out the IN operator of the SQL language for whichever database you are using.

  3. #3
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: How to get all employees, who have NOT had attendance entries?

    Search for "EXISTS"

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2006
    Location
    Anchorage, Alaska
    Posts
    545

    Re: How to get all employees, who have NOT had attendance entries?

    I think I figured it out. Please reply with if I have made any errors, or if there is a better way to do this, to shorten up the queries saved, and the run time.

    Also, I need to exclude 2 items from the count.
    If Exception="Approved Unscheduled" or Exception="Deleted".
    I'm not sure how to add that, maybe in the count function?

    I did a Union query, followed by a sum query.

    PA stands for Perfect Attendance

    Q3_PA
    Code:
    SELECT tblAgents.AgentName, 0 AS Occurances
    FROM tblAgents
    WHERE tblagents.active=true;
    UNION ALL SELECT tblAgents.AgentName, Count(tblAttendance.AgentName) AS Occurances
    FROM tblAgents INNER JOIN tblAttendance ON tblAgents.AgentName = tblAttendance.AgentName
    WHERE (tblAttendance.Date)>#2/1/2008# and tblagents.active=true
    GROUP BY tblAgents.AgentName;

    Q4_PA
    Code:
    SELECT q3_PA.AgentName, Sum(q3_PA.Occurances) AS TotalOccurances
    FROM q3_PA
    GROUP BY q3_PA.AgentName
    ORDER BY Sum(q3_PA.Occurances);
    Please RATE posts, click the RATE button to the left under the Users Name.

    Once your thread has been answered, Please use the Thread Tools and select RESOLVED so everyone knows your question has been answered.


    "As I look past the light, I see the world I wished tonight, never the less, sleep has come, and death shall soon follow..." © 1998 Jeremy J Swartwood

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2006
    Location
    Anchorage, Alaska
    Posts
    545

    Re: How to get all employees, who have NOT had attendance entries?

    While I was typing two posts came through. I'll look up IN and Exists.
    Please RATE posts, click the RATE button to the left under the Users Name.

    Once your thread has been answered, Please use the Thread Tools and select RESOLVED so everyone knows your question has been answered.


    "As I look past the light, I see the world I wished tonight, never the less, sleep has come, and death shall soon follow..." © 1998 Jeremy J Swartwood

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2006
    Location
    Anchorage, Alaska
    Posts
    545

    Re: How to get all employees, who have NOT had attendance entries?

    WOOT!!!!

    Alright thank you both so much.

    I think I got it working with NO EXISTS. It returned 261 employees, the same amount as the union query I built, so either they both work, or they are both wrong ;p

    Code:
    SELECT tblAgents.AgentName
    FROM tblAgents
    WHERE tblagents.active=true AND NOT EXISTS  (SELECT tblattendance.agentname FROM tblattendance WHERE tblagents.Agentname=tblAttendance.AgentName AND tblattendance.date>#02/01/2008#);
    Please let me know if I understood the EXISTS correctly, and if I have made any mistakes =)

    Thank you thank you again!
    Please RATE posts, click the RATE button to the left under the Users Name.

    Once your thread has been answered, Please use the Thread Tools and select RESOLVED so everyone knows your question has been answered.


    "As I look past the light, I see the world I wished tonight, never the less, sleep has come, and death shall soon follow..." © 1998 Jeremy J Swartwood

  7. #7
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: How to get all employees, who have NOT had attendance entries?

    Try looking at the execution plan to see which one is more efficient.

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2006
    Location
    Anchorage, Alaska
    Posts
    545

    Re: How to get all employees, who have NOT had attendance entries?

    Only need a list of employees, Don't need to know how many times they were absent. The No Exists works perfect, and I don't end up with multipule queries.

    For the last bit, how do I make sure that if an entry is in the attendence table, but the Exception field ="Approved Unscheduled" or "Deleted" it shows there name as Perfect Attendance?

    We use Approved Unscheduled for FMLA (Famly Medical Leave Act)
    We use Deleted for an entry that was explain, fixed, or approved later.

    So employee's with these two items should still get credit for having perfect attendance.

    I tried to put the tblattendance.execption="Approved Unscheduled" in a few places, but it didn't work.
    Please RATE posts, click the RATE button to the left under the Users Name.

    Once your thread has been answered, Please use the Thread Tools and select RESOLVED so everyone knows your question has been answered.


    "As I look past the light, I see the world I wished tonight, never the less, sleep has come, and death shall soon follow..." © 1998 Jeremy J Swartwood

  9. #9
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: How to get all employees, who have NOT had attendance entries?

    "NOT EXIST" can do the job but I think use "NOT IN" is more efficient because:
    With "NOT EXIST", if tblAgents has 1000 records then the sub query need to be executed 1000 times.
    With "NOT IN", the sub query need to be executed only once:
    Code:
    SELECT tblAgents.AgentName 
    FROM tblAgents 
    WHERE tblAgents.Active = True And tblAgents.AgentName NOT IN
    (SELECT DISTINCT tblAttendance.AgentName FROM tblAttendance
    WHERE tblAttendance.Date > #02/01/2008#);
    Another method is :
    Code:
    SELECT DISTINCT tblAgents.AgentName 
    FROM tblAgents LEFT JOIN tblAttendance 
    ON tblAgents.AgentName = tblAttendance.AgentName
    WHERE tblAgents.Active = True And 
    (tblAttendance.AgentName Is Null OR tblAttendance.Date <= #02/01/2008#);
    But use "NOT IN" may be better.
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  10. #10
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: How to get all employees, who have NOT had attendance entries?

    I tried to put the tblattendance.execption="Approved Unscheduled" in a few places, but it didn't work.
    In this case you need to check if the Exception field is Not equal to one or more values.

    Code:
    SELECT tblAgents.AgentName
    FROM tblAgents
    WHERE tblagents.active=true AND NOT EXISTS  
         (SELECT tblattendance.agentname 
          FROM tblattendance 
          WHERE tblagents.Agentname=tblAttendance.AgentName AND
                    tblattendance.date>#02/01/2008# And 
                    tblAttendance.Exception Not In ('Approved Unscheduled','Deleted')
         )

  11. #11

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2006
    Location
    Anchorage, Alaska
    Posts
    545

    Re: How to get all employees, who have NOT had attendance entries?

    I did a few extra seperate queries to analyze the data. I guess it was working. The records only went up by 2, so I thought perhaps I had messed up. When I went through and counted myself, I got the same number, and the same agents. Everything is working. Thank you all for your input. I am going to attempt to give you all positive feedback, but for some reason, It's very picky about how often you can give someoen positive feedback.
    Please RATE posts, click the RATE button to the left under the Users Name.

    Once your thread has been answered, Please use the Thread Tools and select RESOLVED so everyone knows your question has been answered.


    "As I look past the light, I see the world I wished tonight, never the less, sleep has come, and death shall soon follow..." © 1998 Jeremy J Swartwood

  12. #12
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [RESOLVED] How to get all employees, who have NOT had attendance entries?

    I have to disagree with anhn. "IN" will perform a full table scan, "EXIST" can use any indices on the table columns in the sub query. Surely the EXIST should perform better than the IN. I of course am too lethargic today to do this test myself.

    Incidentally, are you aware that "indices", the correct plural for "index", is marked as an incorrect word by default in the inbuilt Firefox dictionary?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width