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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T07:03:30+00:00 2026-05-30T07:03:30+00:00

I am stucked with this problem from long time. Though you will find this

  • 0

I am stucked with this problem from long time. Though you will find this is big question, but it’s not…………

I have following tables, with dummy data, so you can also try it.

     users Table with Columns

        emp_id  emp_Name  Joining_Date         state_id
          1       Lauren  11-07-2011           123         
          2       John    01-08-2012           234
          3       Smith   02-09-2011           234
____________________________________________________    
CREATE TABLE users 
( emp_id int
, emp_Name varchar(20)
, Joining_Date DATE 
, state_id int 
);

INSERT INTO users (emp_id, emp_Name, Joining_Date, state_id) 
VALUES 
  (1, 'John',  '2011-08-10', 123) ,
  (2, 'Smith', '2011-09-11', 234) ; 

__________________________________________________________________
       cl_doctor_call Table

        Subject Call_Date     call_Done_By(emp_id)
        Call       15-01-2012      1
        CA        21-02-2012       2
___________________________________________________________________     
CREATE TABLE cl_doctor_call 
( Subject  VARCHAR(10)
, Call_Date DATE
, call_Done_By INT 
) ;

INSERT INTO cl_doctor_call (Subject, Call_Date,call_Done_By) 
VALUES 
  ('sub1', '2011-08-15', 1) ,
  ('sub2', '2011-09-16', 2) ;
___________________________________________________________________

        cl_ chemist Table

        Subject       Call_Date     call_Done_By(emp_id)
        Chemist       1-02-2012              2
        Texo          21-03-2012             1

____________________________________________________________________________________
CREATE TABLE cl_chemist ( Subject  VARCHAR(10),  Call_Date DATE, call_Done_By INT   );

INSERT INTO cl_chemist (Subject, Call_Date,call_Done_By) VALUES ('sub3','2011-08-19',2),('sub5','2011-09-25',1);

___________________________________________________________
        cl_Stock Table

        Subject  Call_Date     call_Done_By(emp_id)
        Sub1     1-02-2012            1
        Sub2     21-03-2012           3
____________________________________________________________
CREATE TABLE cl_Stock 
( Subject  VARCHAR(10)
, Call_Date DATE
, call_Done_By INT
);

INSERT INTO cl_Stock (Subject, Call_Date,call_Done_By) 
VALUES 
  ('ABC',  '2011-10-13', 1) , 
  ('sub5', '2011-11-17', 2) ;

______________________________________________________    
        Meetings Table

        Subject Meeting_Date   call_Done_By(emp_id)
        Sub1     11-02-2012          1
        Sub2     23-03-2012          2    _____________________________________________________________________________________       
CREATE TABLE Meetings (   Subject  VARCHAR(10),   meet_Date DATE, meet_Done_By INT    );

INSERT INTO Meetings (Subject, meet_Date,meet_Done_By)
VALUES 
  ('Planning','2011-11-01',2),
  ('Deploy','2011-12-15',1);

______________________________________________________________________________________
        Leave Table
        Subject     from_Date         to_date       Requested_By(emp_id)   status
        Sub1        01-02-2012       03-02-2012           2               Declined
        Sub2       21-03-2012        22-03-2012           1               Approved

        Holiday Table

        Holiday_Name              Holiday_Date      States_Id
        New Year                  01-01-2012            123

       Independence Day          15-08-2012            234
______________________________________________________________________________________
CREATE TABLE Holiday (    Holiday_Name  VARCHAR(10),Holiday_Date DATE,   State_Id_c INT     );


INSERT INTO Holiday (Holiday_Name,Holiday_Date,State_Id_c) VALUES ('NEW_YEAR','2012-01-01',123), ('CHRISHMAS','2011-12-25',234);
______________________________________________________________________________________    
    Replace with date
    for stock='Y'
    for chemist='Y'
    for doctor='Y'
    for leave='L'
    for Sunday='S'
    for meeting ='M'
    for Holiday='H'

Hope so still you are with me.

 Now Admin will select 'month' and 'year' and according to that attendance report for 

 employee will displayed.

        Output :
        Employee     Year       Month     1 2 3 4 5 6 7.... for all 31 days
        John         2011       Nov       Y Y Y H Y L S....
        Smith        2011       Nov       Y Y Y H Y M S.....
          .           .          .        ................ 
          .           .          .        ................
        &so on      &so on     &so on     & so on      

here in output 1,2,3…. are days from 0-31 for a month which we can write using ‘case’

Consider if employee is present on day show its status as ‘Y’ ,if he is on leave show it’s status as ‘L; if there is Sunday show status ‘S’ for holiday show ‘H’ and so on.

So basically I want a write a query to retrieve all these details.

So pl z suggest me ways here, that How can I display this attendance Report by a query or procedure.

I have tried so far this …..

DELIMITER $$

USE `leosatyen_claris`$$

DROP PROCEDURE IF EXISTS `Test`$$

CREATE DEFINER=`leosatyen_claris`@`%` PROCEDURE `Test`(IN month_last_date DATE)
BEGIN
SELECT 
users.id AS Id,
users.first_name AS Employee,
CASE MAX(CASE WHEN DATE_FORMAT(main.cdate,'%d')='01' THEN main.task
              WHEN DATE_FORMAT(DATE_ADD(month_last_date, INTERVAL 1 DAY),'%W')='SUNDAY' THEN 0
              WHEN main.cdate IS NULL THEN -1
         ELSE -2 END)
     WHEN -1 THEN 'NA'
     WHEN  0 THEN 'S'
     WHEN  1 THEN 'Y'
     WHEN  2 THEN 'C'
     WHEN  3 THEN 'S1'
     WHEN  4 THEN 'P'
     WHEN  5 THEN 'M'

END Day1,
CASE MAX(CASE WHEN DATE_FORMAT(main.cdate,'%d')='02' THEN main.task
              WHEN DATE_FORMAT(DATE_ADD(month_last_date, INTERVAL 2 DAY),'%W') ='SUNDAY' THEN 0
              WHEN main.cdate IS NULL THEN -1
         ELSE -2 END)
     WHEN -1 THEN 'NA'
     WHEN  0 THEN 'S'
      WHEN  1 THEN 'Y'
      WHEN  2 THEN 'C'
      WHEN  3 THEN 'S1'
      WHEN  4 THEN 'P'
      WHEN  5 THEN 'M'

END Day2,
.
.
. ## UP TO 31 DAYS

CASE MAX(CASE WHEN DATE_FORMAT(main.cdate,'%d')='31' THEN main.task
              WHEN DATE_FORMAT(DATE_ADD(month_last_date, INTERVAL 31 DAY),'%W')='SUNDAY' THEN 0
              WHEN main.cdate IS NULL THEN -1
         ELSE -2 END)
     WHEN -1 THEN 'NA'
     WHEN  0 THEN 'S'
     WHEN  1 THEN 'Y'
     WHEN  2 THEN 'C'
     WHEN  3 THEN 'S1'
     WHEN  4 THEN 'P'
     WHEN  5 THEN 'M'
END Day31 
FROM
users  
LEFT JOIN
(
SELECT DATE_FORMAT(cl_doctor_call.date_entered,'%Y-%m-%d')AS cdate,
  cl_doctor_call.created_by  AS emp,
  1     task     
  FROM 
  cl_doctor_call  
  WHERE 
  DATE_FORMAT(cl_doctor_call.date_entered,'%Y-%m-%d')>month_last_date AND
  DATE_FORMAT(cl_doctor_call.date_entered,'%Y-%m-%d')<=DATE_ADD(month_last_date, INTERVAL 30 DAY)

  UNION ALL
  SELECT 
  DATE_FORMAT(cl_chemist_call.date_entered,'%Y-%m-%d') AS  cdate,
  cl_chemist_call.created_by AS emp,
  2    task     
  FROM 
  cl_chemist_call
  WHERE 
  DATE_FORMAT(cl_chemist_call.date_entered,'%Y-%m-%d')>month_last_date AND
  DATE_FORMAT(cl_chemist_call.date_entered,'%Y-%m-%d')<=DATE_ADD(month_last_date, INTERVAL 30 DAY)

  UNION ALL
  SELECT 
  DATE_FORMAT(cl_stockist_call_cstm.calldate_c,'%Y-%m-%d') AS cdate,
  cl_stockist_call.created_by AS emp,
  3         task
  FROM 
  cl_stockist_call LEFT JOIN cl_stockist_call_cstm
  ON cl_stockist_call.id=cl_stockist_call_cstm.id_c
  WHERE 
  DATE_FORMAT(cl_stockist_call_cstm.calldate_c,'%Y-%m-%d')> month_last_date AND
  DATE_FORMAT(cl_stockist_call_cstm.calldate_c,'%Y-%m-%d')<=DATE_ADD(month_last_date, INTERVAL 30 DAY)

  UNION ALL
  SELECT DATE_FORMAT(cl_product_pramotional_call.date_entered,'%Y-%m-%d') AS cdate,
  cl_product_pramotional_call.created_by AS emp,
  4       task
  FROM cl_product_pramotional_call
  WHERE  
  DATE_FORMAT(cl_product_pramotional_call.date_entered,'%Y-%m-%d')>month_last_date AND
  DATE_FORMAT(cl_product_pramotional_call.date_entered,'%Y-%m-%d')<=DATE_ADD(month_last_date, INTERVAL 30 DAY)

  UNION ALL

  SELECT DATE_FORMAT(meetings.date_start,'%Y-%m-%d') AS cdate,
  meetings.created_by AS emp,
  5        task
  FROM meetings
  WHERE  
  DATE_FORMAT(meetings.date_start,'%Y-%m-%d')> month_last_date AND
  DATE_FORMAT(meetings.date_start,'%Y-%m-%d') <= DATE_ADD(month_last_date, INTERVAL 30 DAY)

 )AS main 
 ON main.emp = users.id 
 WHERE DATE_FORMAT(users.date_entered,'%Y-%m-%d')<= month_last_date
 GROUP BY users.id ;
 END$$

DELIMITER ;

Here I am getting all Sunday’s of that month AND All customer calls like Doctor_call,
chemist call, stockist call and meetings entries correctly.

Now I just need to add ‘Leave’ and Holiday Entries. So anybody can assist me to achieve holidays and leave in my existing procedure?

any suggestions will be helpful for me….

  • 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-05-30T07:03:32+00:00Added an answer on May 30, 2026 at 7:03 am

    Finally I achieved answer for Attendance Report.

    We need to execute this lengthy stored procedure to get monthly attendance of all Employees.

    To execute this procedure we need to pass IN parameter as last date of previous month like if we need to see December 2011 attendance Report pass ‘2011-11-30’ AS IN parameter.

    DELIMITER $$
    
    USE `Sample`$$
    
    DROP PROCEDURE IF EXISTS `Test`$$
    
    CREATE DEFINER=`Sample`@`%` PROCEDURE `Test`(IN month_last_date DATE)
    BEGIN
    SELECT 
    users.id AS Id,DATE_FORMAT(month_last_date,'%Y')AS YEAR,DATE_FORMAT(month_last_date,'%M')AS MONTH,
    CONCAT(users.first_name,users.last_name) AS Employee,cl_territories.name AS Territory,
    CASE MAX(CASE WHEN DATE_FORMAT(main.cdate,'%d')='01' THEN main.task
                  WHEN DATE_FORMAT(DATE_ADD(month_last_date, INTERVAL 1 DAY),'%W')='SUNDAY' THEN 0
                  WHEN main.cdate IS NULL THEN -1
             ELSE -2 END)
         WHEN -1 THEN 'NA'
         WHEN  0 THEN 'S'
         WHEN  1 THEN 'Y'
         WHEN  2 THEN 'C'
         WHEN  3 THEN 'S1'
         WHEN  4 THEN 'P'
         WHEN  5 THEN 'M'
         WHEN  6 THEN 'H'
         WHEN  7 THEN 'L'
    END Day1,
    CASE MAX(CASE WHEN DATE_FORMAT(main.cdate,'%d')='02' THEN main.task
                  WHEN DATE_FORMAT(DATE_ADD(month_last_date, INTERVAL 2 DAY),'%W') ='SUNDAY' THEN 0
                  WHEN main.cdate IS NULL THEN -1
             ELSE -2 END)
         WHEN -1 THEN 'NA'
         WHEN  0 THEN 'S'
          WHEN  1 THEN 'Y'
          WHEN  2 THEN 'C'
          WHEN  3 THEN 'S1'
          WHEN  4 THEN 'P'
          WHEN  5 THEN 'M'
          WHEN  6 THEN 'H'
          WHEN  7 THEN 'L'
    END Day2,
    .
    .
    . # upto 31 days
    
    CASE MAX(CASE WHEN DATE_FORMAT(main.cdate,'%d')='31' THEN main.task
                  WHEN DATE_FORMAT(DATE_ADD(month_last_date, INTERVAL 31 DAY),'%W')='SUNDAY' THEN 0
                  WHEN main.cdate IS NULL THEN -1
             ELSE -2 END)
         WHEN -1 THEN 'NA'
         WHEN  0 THEN 'S'
         WHEN  1 THEN 'Y'
         WHEN  2 THEN 'C'
         WHEN  3 THEN 'S1'
         WHEN  4 THEN 'P'
         WHEN  5 THEN 'M'
         WHEN  6 THEN 'H'
         WHEN  7 THEN 'L'
    END Day31
    FROM
    users  LEFT JOIN users_cstm
    ON users.id=users_cstm.id_c
    LEFT JOIN cl_territories
    ON users_cstm.cl_territories_id1_c=cl_territories.id
    LEFT JOIN
    (
    SELECT DATE_FORMAT(cl_doctor_call.date_entered,'%Y-%m-%d')AS cdate,
      cl_doctor_call.created_by  AS emp,
      1     task     
      FROM 
      cl_doctor_call  
      WHERE 
      DATE_FORMAT(cl_doctor_call.date_entered,'%Y-%m-%d')>month_last_date AND
      DATE_FORMAT(cl_doctor_call.date_entered,'%Y-%m-%d')<=DATE_ADD(month_last_date, INTERVAL 30 DAY)
    
      UNION ALL
      SELECT 
      DATE_FORMAT(cl_chemist_call.date_entered,'%Y-%m-%d') AS  cdate,
      cl_chemist_call.created_by AS emp,
      2    task     
      FROM 
      cl_chemist_call
      WHERE 
      DATE_FORMAT(cl_chemist_call.date_entered,'%Y-%m-%d')>month_last_date AND
      DATE_FORMAT(cl_chemist_call.date_entered,'%Y-%m-%d')<=DATE_ADD(month_last_date, INTERVAL 30 DAY)
    
      UNION ALL
      SELECT 
      DATE_FORMAT(cl_stockist_call_cstm.calldate_c,'%Y-%m-%d') AS cdate,
      cl_stockist_call.created_by AS emp,
      3         task
      FROM 
      cl_stockist_call LEFT JOIN cl_stockist_call_cstm
      ON cl_stockist_call.id=cl_stockist_call_cstm.id_c
      WHERE 
      DATE_FORMAT(cl_stockist_call_cstm.calldate_c,'%Y-%m-%d')> month_last_date AND
      DATE_FORMAT(cl_stockist_call_cstm.calldate_c,'%Y-%m-%d')<=DATE_ADD(month_last_date, INTERVAL 30 DAY)
    
      UNION ALL
      SELECT DATE_FORMAT(cl_product_pramotional_call.date_entered,'%Y-%m-%d') AS cdate,
      cl_product_pramotional_call.created_by AS emp,
      4       task
      FROM cl_product_pramotional_call
      WHERE  
      DATE_FORMAT(cl_product_pramotional_call.date_entered,'%Y-%m-%d')>month_last_date AND
      DATE_FORMAT(cl_product_pramotional_call.date_entered,'%Y-%m-%d')<=DATE_ADD(month_last_date, INTERVAL 30 DAY)
    
      UNION ALL
    
      SELECT DATE_FORMAT(meetings.date_start,'%Y-%m-%d') AS cdate,
      meetings.created_by AS emp,
      5        task
      FROM meetings
      WHERE  
      DATE_FORMAT(meetings.date_start,'%Y-%m-%d')> month_last_date AND
      DATE_FORMAT(meetings.date_start,'%Y-%m-%d') <= DATE_ADD(month_last_date, INTERVAL 30 DAY)
    
      UNION ALL
    SELECT DATE_FORMAT(cl_holidays_structures.holiday_date,'%Y-%m-%d') AS cdate,
    users.id AS emp,
    6    task
    FROM users LEFT JOIN users_cstm
    ON users.id=users_cstm.id_c
    LEFT JOIN cl_states
    ON users_cstm.cl_states_id1_c=cl_states.id
    LEFT JOIN cl_holidays_structures
    ON cl_holidays_structures.cl_states_id_c=users_cstm.cl_states_id1_c
    WHERE  
    DATE_FORMAT(cl_holidays_structures.holiday_date,'%Y-%m-%d')> month_last_date AND
    DATE_FORMAT(cl_holidays_structures.holiday_date,'%Y-%m-%d') <= DATE_ADD(month_last_date, INTERVAL 30 DAY)
    UNION ALL
    SELECT cl_leave_management_cstm.from_date_c+ INTERVAL td.days DAY cdate, 
    users.id AS emp,
    7    task
    FROM
      cl_leave_management LEFT JOIN cl_leave_management_cstm
      ON cl_leave_management.id=cl_leave_management_cstm.id_c
    LEFT JOIN users 
      ON cl_leave_management.created_by = users.id
    JOIN temp_days td
      ON DATEDIFF(cl_leave_management_cstm.to_date_c, cl_leave_management_cstm.from_date_c) >= td.days
    WHERE DATE_FORMAT(cl_leave_management_cstm.from_date_c,'%Y-%m-%d')> month_last_date AND
     DATE_FORMAT(cl_leave_management_cstm.from_date_c,'%Y-%m-%d')<=DATE_ADD(month_last_date, INTERVAL 30 DAY) AND
     cl_leave_management_cstm.status_c='Approved'
    
     )AS main 
     ON main.emp = users.id 
     WHERE DATE_FORMAT(users.date_entered,'%Y-%m-%d')<= month_last_date
     GROUP BY users.id ;
     END$$
    
    DELIMITER ;
    

    Thanks to all who guide me to get this result.

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

Sidebar

Related Questions

I am unable to find the solution to this problem. I just have to
I am new in Prolog and I am stucked in a problem. I have
i have big problem with binding Stacked Column Series to my chart. I have
So, I am having a very difficult time explaining my problem, I hope this
So I have the following data-structure coming back from my server. {date:'2/1', name: 'product
I have stocked in this error many times but know I have no way
stucked with one problem that in my application i have one button which is
i have this problem that has been buggin me for the last hours. Lets
I have decided to use Simple XML serialization and was stucked with basic problem.
I have a form that will multiple Panel controls stacked on top of each

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.