Click here to Skip to main content
15,902,114 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
For example:

Name  | 1stjan   | 2ndjan   | 3rdjan   | 4thjan
jenny | present  | absent   | present  | present
clark | absent   | present  | present  | present

Original tables - emp_details 

id | emp_id |   Name 
1  | 51     |  jenny
2  | 52     |  clark
Original tables - emp_attendance

 id | emp_id   | status       |    Date 
  1 |    51    |   present    |  2017/01/02    
  2 |    52    |   present    |  2017/01/02   


What I have tried:

I tried this and its working fine but after 10 columns ...it throws an error of mysql syntax

DELIMITER $$
    DROP PROCEDURE IF EXISTS `attendance_reg`.`new2` $$
CREATE PROCEDURE new2 (IN ddl1 varchar(100))
BEGIN
SET @sql = NULL;
SET @vardomain := CONCAT(ddl1,'%');
    select GROUP_CONCAT(DISTINCT
               CONCAT(' Max(CASE WHEN DATE_FORMAT(b.time_in, ''%D %M'') = ''',
                       DATE_FORMAT(time_in, '%D %M'),
                       ''' THEN b.status END) AS ''',
                       DATE_FORMAT(time_in, '%D %M'), ''''
                     )
                  ORDER BY time_in)
                  INTO @sql
    from emp_attendance   where time_in like @vardomain;
    SET @sql = CONCAT('SELECT b.emp_id ,a.emp_name ,', @sql, ' from emp_details a  join emp_attendance b  on b.emp_id=a.emp_id  and b.time_in like ',@vardomain,' Group By a.emp_name order by a.emp_dept,a.emp_name');
    PREPARE stmt FROM @sql;
    EXECUTE stmt;
    DEALLOCATE PREPARE stmt;
    END $$

DELIMITER ;
Posted
Updated 16-Jan-17 22:58pm
v5
Comments
Afzaal Ahmad Zeeshan 11-Jan-17 5:53am    
So? Where is the HTML content for the views? The data seems to be working fine (in case there are problems, that I didn't see). Plus one thing, wouldn't you need to JOIN the tables, instead of just selecting them?
mridulkoul123 11-Jan-17 5:58am    
right now view is like:

Name | emp_id| status | Date
jenny| 51 | present | 1stjan
clark| 52 | present | 2ndjan

And i want it like:

Name | 1stjan | 2ndjan | 3rdjan | 4thjan
jenny | present | absent | present | present
clark | absent | present | present | present
Afzaal Ahmad Zeeshan 11-Jan-17 6:06am    
Alright, then you need to transpose the rows based on the Date. In ASP.NET, you can do so by using grouping the data based on the Name as well as the Date of the records.
mridulkoul123 11-Jan-17 6:14am    
can you show this by example
anup.bhunia 13-Jan-17 6:55am    
this article might help you
https://www.codeproject.com/Articles/44274/Transpose-a-DataTable-using-C

1 solution

I solved it myself....the problem was the length of my GROUP_CONCAT
i set it 1000000...
SET SESSION GROUP_CONCAT_max_len = 1000000;
 
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