透视mysql

时间:2019-02-07 05:22:10

标签: mysql sql concat group-concat

我尝试在透视后获取串联的字符串值。

当前,只有一个值并且可以正常工作。

以下是查询:

DROP PROCEDURE IF EXISTS getAllUserLunchReport;
DELIMITER $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `getAllUserLunchReport`(IN `start_date` DATETIME, IN `end_date` DATETIME)
BEGIN

SET GLOBAL group_concat_max_len=4294967295;
SET @SQL = NULL;
SET @start_date = DATE(start_date);
SET @end_date = DATE(end_date);

    SELECT
    COALESCE(GROUP_CONCAT(DISTINCT
    CONCAT(
    'SUM(CASE WHEN date = "',DATE(issuedDateTime),'" AND lunchStatus = 1 THEN (SELECT CONCAT((SELECT rate FROM lunch_rate WHERE DATE(created_on) <= date ORDER BY created_on DESC LIMIT 1), "," ,(SELECT rate FROM lunch_gbd_rate WHERE DATE(created_on) <= date ORDER BY created_on DESC LIMIT 1))) ELSE 0 END) AS `',DATE(issuedDateTime),'`'
    ) ORDER BY issuedDateTime
), '0 as `NoMatchingRows`') INTO @SQL

        FROM `lunch_status` 
        WHERE DATE(issuedDateTime) BETWEEN @start_date AND @end_date;

    SET @SQL 
    = CONCAT
    (
    '
        SELECT concat(u.firstname," ",u.lastname) as Employee, w.*
        FROM users u
        INNER JOIN
        (SELECT userId, ', @SQL, ' 
        FROM
        (
            SELECT userId, lunchStatus, DATE(issuedDateTime) as date 
            FROM `lunch_status` 
            WHERE DATE(issuedDateTime) BETWEEN "',@start_date,'" AND "',@end_date,'" 
        ) as a
        GROUP BY userId) w
        ON u.id = w.userId
        ORDER BY Employee;
    '
    );

PREPARE stmt FROM @SQL;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;


END$$
DELIMITER ;

我只得到第一个值,而没有其他要串联的值。

这是我期望的结果:

employee    |   2019-1-15  |  2019-1-16
----------------------------------------
Jack        |   30,140     |  30,140

虽然可以单独执行串联查询。

注意:在上面的存储过程中,如果删除了分隔符,则将值连接起来。 CONCAT((select ...), (select...))给出30140

quotes可能是问题,但我已经尝试了所有方法。

仅供参考,这是@SQL在第一个查询后产生的结果:

SUM(CASE WHEN date = "2019-01-15" AND lunchStatus = 1 THEN (SELECT CONCAT((SELECT rate FROM lunch_rate WHERE DATE(created_on) <= date ORDER BY created_on DESC LIMIT 1), ",", (SELECT rate FROM lunch_gbd_rate WHERE DATE(created_on) <= date ORDER BY created_on DESC LIMIT 1))) ELSE 0 END) AS `2019-01-15`,
SUM(CASE WHEN date = "2019-01-16" AND lunchStatus = 1 THEN (SELECT CONCAT((SELECT rate FROM lunch_rate WHERE DATE(created_on) <= date ORDER BY created_on DESC LIMIT 1), ",", (SELECT rate FROM lunch_gbd_rate WHERE DATE(created_on) <= date ORDER BY created_on DESC LIMIT 1))) ELSE 0 END) AS `2019-01-16`

看起来有效。

编辑:看来它在使用“整数”值时工作正常。 例如

CONCAT((SELECT rate FROM lunch_rate WHERE DATE(created_on) <= date ORDER BY created_on DESC LIMIT 1), "111" ,(SELECT rate FROM lunch_gbd_rate WHERE DATE(created_on) <= date ORDER BY created_on DESC LIMIT 1))

给予301140

但是使用其他字符,/./-/_,仅显示第一个参数:30

1 个答案:

答案 0 :(得分:1)

,中包含CONCAT时,代码无法正常工作的原因是,CONCAT位于SUM内部,并且不会能够将30,140解释为一个数字,它可以对30140进行解释(因此没有分隔符,您将得到结果)。在不了解表数据和所需结果的更多详细信息的情况下,很难准确说明如何解决问题,但是也许您想要这样的东西:

CASE WHEN date = "2019-01-15" AND lunchStatus = 1 THEN CONCAT((SELECT SUM(rate) FROM lunch_rate WHERE DATE(created_on) <= date ORDER BY created_on DESC LIMIT 1), ",", (SELECT SUM(rate) FROM lunch_gbd_rate WHERE DATE(created_on) <= date ORDER BY created_on DESC LIMIT 1)) ELSE 0 END) AS `2019-01-15`