0

i know this is kind of question that might have been asked before.

But i am my self not sure if i am doing it the right way so i have to ask.

i want this below table in output

EmpID   Name          Desig     Year       Q1       Q2      Q3       Annual 
---------------------------------------------------------------------------------
1       Haider          0       2016      setup    setup    setup     AnotherString
2       Arif            1       2016      setup    setup    setup     AnotherString

But the tables i have in db are.

Employee Table:

EmpID   Name          Desig 
-------------------------------
1       Haider          0   
2       Arif            1

Appraisal Table

ID      Title    
-----------------
1       Q1  
2       Q2
3       Q3
4       Annual      

The values in appraisal table are the columns in the output table(first table).

I don't understand how can i do that with pivot. i have seen examples but not sure how to have table setup like this.

Plus There is no year information in the table. the year will be displayed from the current year against every employee.

This is kinda query i tried to work on. but i don't know how to join it to other table there is no employee information there or anything.

SELECT 
  * 
FROM
  employee E 

  INNER JOIN employment ET 
    ON ET.`employee_id` = E.`employee_id` 
    AND ET.`trashed` = 0
    AND ET.`current` = 1



WHERE E.`enrolled` = 1 
  AND E.`trashed` = 0 
  AND E.`employee_id` IN (8,1,3,17,6,19,23)

=-=-=-=-=-==-==-=-

UPDATE

Here is what i have tried so far. but when i run it, i get no table ??

  SET @sql = NULL;

SELECT GROUP_CONCAT(DISTINCT AppraisalTitle) INTO @sql
FROM `ml_appraisal_title`
WHERE Trash = 0;

  SET @sql = CONCAT('SELECT E.employee_id
                    , E.full_name AS Name, ', @sql, ' 
                   FROM employee E
                   LEFT JOIN ml_appraisal_title AS MLPT 
                    ON MLPT.ID = E.employee_id
                   GROUP BY E.employee_id');

PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

i don't quite full understand it. but im keep trying to figure it out.

=-=-=-=--=-=-=-=-=

UPDATE:2

I think i am reaching somewhere with this. xD..

 SET @sql = NULL;

SELECT GROUP_CONCAT(DISTINCT CONCAT('MAX(IF(AppraisalTitle=''',AppraisalTitle,''', AppraisalTitle, NULL)) AS ',AppraisalTitle )) INTO @sql
FROM `ml_appraisal_title`
WHERE Trash = 0;

  SET @sql = CONCAT('SELECT E.employee_id
                    , E.full_name AS Name, ', @sql, ' 
                   FROM employee E
                   LEFT JOIN ml_appraisal_title AS MLPT 
                    ON MLPT.AppraisalID = E.employee_id
                   GROUP BY E.employee_id');

PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

i got result below result with above query. enter image description here

But this thing is the values in columns needs to come from another table. Confused how can i do that. As you can see in output there is setup which will act as link. and it can be changed to any other value depending on another table.

Appraisal Notification Table

ID      Notification
-----------------
1       setup
2       Create
3       Complete

How would i show setup from another table? i can understand i need to join it.. But i am thinking if i can use left join and if values are null then setup should show. Is it possible?

Sizzling Code
  • 5,932
  • 18
  • 81
  • 138
  • hi where u calculate year and q1,q2..... values. i mean values 2016 e.t.c – dev Apr 19 '16 at 10:16
  • @dharmendrachaudhary Year is not calculated value, it only needs to be displayed in table/grid and q1, q2, q2 are the values stored in table, by q1 i mean Quarter 1 of the year. (Means first 3 months of an year(year would be displayed against it.)) – Sizzling Code Apr 19 '16 at 10:19
  • Have you read this question? [Pivot Table](http://stackoverflow.com/questions/7674786/mysql-pivot-table?answertab=active#tab-top) It gives you clear path from ROWS to COLUMN approach – Pirate X Apr 19 '16 at 10:29
  • i read the article clear but i did not find the actual problem like what is the relation b/w two table here in the above question – dev Apr 19 '16 at 10:31
  • @PirateX Yes, thats why i am trying to do it through that. cuz there is no other option i see to get values as rows. Plus i have updated my question. i added an extra code that i have tried so far. but its not giving me any table when i run it. :( – Sizzling Code Apr 19 '16 at 10:35
  • @dharmendrachaudhary I want values in second table to act as columns in first table – Sizzling Code Apr 19 '16 at 10:36
  • @SizzlingCode Is there a lot of data in Appraisal Table? – Blank Apr 19 '16 at 10:42
  • how can simple use like this select table1.name, if(table2.col = 'some_value' ,table2.col ,0) as 'some_name',if(table2.col = 'some_value' ,table2.col ,0) as 'another_name', like this u can try – dev Apr 19 '16 at 11:44
  • @No. Limited Data. I think it will be max 10-12 Items... Minimum 4 Items that you are seeing... – Sizzling Code Apr 20 '16 at 04:21

0 Answers0