0
              SET @sql = NULL;

              SELECT 
                GROUP_CONCAT(DISTINCT
                              CONCAT('sum(case when week = ',week,
                                     'then totaltime else 00 end) AS `',
                                         week, '`')
                             )INTO @sql
                FROM   mytable;

                 SET @sql = CONCAT('SELECT TRIM(UPPER(mytable.empcode))AS CODE, 
                 TRIM(UPPER(mytable.empname)) EMPNAME,
                 ', @sql, ' 
                 from  mytable
                 group by empcode
                 order by empname ');
           PREPARE stmt FROM @sql;
           EXECUTE stmt;
           DEALLOCATE PREPARE stmt;
           END

I am using pivot table here, want to make sum of totaltime according to week field which is dynamically generated by query, but I got an error :

"#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'from mytable group by empcode or' at line 6"

5
  • where is from timesheetreport1 ? code incomplete?
    – Tharif
    Apr 10, 2015 at 8:57
  • oops sorry it was my mistake its mytable
    – Terrasom
    Apr 10, 2015 at 8:58
  • add relevant portions in question/edit
    – Tharif
    Apr 10, 2015 at 8:59
  • it runs properly when my time diff between fromdt and todt is less but gives me an error when i increase it
    – Terrasom
    Apr 10, 2015 at 9:17
  • Issue has been solved i have to add SET SESSION group_concat_max_len = 1000000;
    – Terrasom
    Apr 10, 2015 at 11:03

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.