在group_concat()中添加count()的过程

时间:2017-09-08 06:35:04

标签: mysql stored-procedures

我有以下程序

DELIMITER $$

CREATE DEFINER=`root`@`localhost` PROCEDURE `ptl_head`()
BEGIN
SET @sql = NULL;

SELECT GROUP_CONCAT(DISTINCT
  CONCAT(
         'MAX(CASE WHEN month(alloc_date) = ''',
         month(alloc_date),
         ''' THEN emp_code END) `',
         month(alloc_date),'_',year(alloc_date),
         '`' 
         )

 )
  INTO @sql
  FROM daily_report;

SET @sql = CONCAT('SELECT emp_code, ', @sql, ' 
                     FROM daily_report
                    GROUP BY emp_code');

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

我想将emp_code的计数添加为输出,但我得到emp_code作为输出。有人帮我解决这个问题

1 个答案:

答案 0 :(得分:0)

怎么样 -

SET @sql = CONCAT('SELECT emp_code, ', @sql ' 
                   FROM daily_report
                   GROUP BY emp_code
                   UNION ALL
                   SELECT "Count", COUNT(*), month(alloc_date), '_', year(alloc_date)
                   FROM daily_report');