透视MySQL表

时间:2017-06-29 14:09:05

标签: mysql database pivot transpose

我一直在尝试在MySQL中移动一个表(将行移动到列)。我知道mysql没有pivot功能所以我认为联合是必需的,但不是100%肯定。我有三列,user_id,option_id和question_id。我需要对所有user_id进行分组,并将所有option_ids放在一行中,并将question_ids作为列:

SELECT user_id, option_id, question_id FROM table给了我以下输出:

+----------+-------------+--------------+
| user_id  | option_id   | question id  |
+----------+-------------+--------------+
| 101      | A1          | Q1           |
| 101      | A5          | Q2           |
| 102      | A2          | Q1           |
| 102      | A7          | Q2           |
+---------------------------------------+

我需要输出以下内容:

+----------+-------------+--------------+
| user_id  | Q1          | Q2           |
+----------+-------------+--------------+
| 101      | A1          | A5           |
| 102      | A2          | A7           |
+---------------------------------------+`

我已尝试使用case,但这只给了我第一行user_id,我得到了' null'对于其余的列

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

我有一个请求,要求创建一些简单的Rain Data的平面视图。数据在一个只有3列(字段)的表中,即“ dDate”,“ mm”,“ comments”。

我需要对数据进行转置/透视,以将其输入到用户的Web表中。

这就是我最后的内容(作为一个过程),并认为它可以帮助您构造查询或过程以适合您的需求。

CREATE DEFINER=`xxxx`@`localhost` PROCEDURE `p_GetYearRainTable`(IN yr 
CHAR(4), IN Tot Char(4))
BEGIN
SELECT 
dDay,
SUM(mJan) AS 'Jan',
SUM(mFeb) AS 'Feb',
SUM(mMar) AS 'Mar',
SUM(mApr) AS 'Apr',
SUM(mMay) AS 'May',
SUM(mJun) AS 'Jun',
SUM(mJul) AS 'Jul',
SUM(mAug) AS 'Aug',
SUM(mSep) AS 'Sep',
SUM(mOct) AS 'Oct',
SUM(mNov) AS 'Nov',
SUM(mDec) AS 'Dec'
FROM
    (SELECT 
    dDay,
        CASE WHEN dMnth = '1' THEN mm END AS 'mJan',
        CASE WHEN dMnth = '2' THEN mm END AS 'mFeb',
        CASE WHEN dMnth = '3' THEN mm END AS 'mMar',
        CASE WHEN dMnth = '4' THEN mm END AS 'mApr',
        CASE WHEN dMnth = '5' THEN mm END AS 'mMay',
        CASE WHEN dMnth = '6' THEN mm END AS 'mJun',
        CASE WHEN dMnth = '7' THEN mm END AS 'mJul',
        CASE WHEN dMnth = '8' THEN mm END AS 'mAug',
        CASE WHEN dMnth = '9' THEN mm END AS 'mSep',
        CASE WHEN dMnth = '10' THEN mm END AS 'mOct',
        CASE WHEN dMnth = '11' THEN mm END AS 'mNov',
        CASE WHEN dMnth = '12' THEN mm END AS 'mDec'
FROM
    (SELECT 
    id,
        DAY(dDate) AS dDay,
        MONTH(dDate) AS mnth,
        YEAR(dDate) AS yr,
        mm,
        comments,
        CASE
            WHEN MONTH(dDate) LIKE '1' THEN '1'
            WHEN MONTH(dDate) LIKE '2' THEN '2'
            WHEN MONTH(dDate) LIKE '3' THEN '3'
            WHEN MONTH(dDate) LIKE '4' THEN '4'
            WHEN MONTH(dDate) LIKE '5' THEN '5'
            WHEN MONTH(dDate) LIKE '6' THEN '6'
            WHEN MONTH(dDate) LIKE '7' THEN '7'
            WHEN MONTH(dDate) LIKE '8' THEN '8'
            WHEN MONTH(dDate) LIKE '9' THEN '9'
            WHEN MONTH(dDate) LIKE '10' THEN '10'
            WHEN MONTH(dDate) LIKE '11' THEN '11'
            WHEN MONTH(dDate) LIKE '12' THEN '12'
            ELSE NULL
        END AS dMnth
FROM
    t_rainData
WHERE
    YEAR(dDate) = yr) tab1
GROUP BY dDay , mJan , mFeb , mMar , mApr , mMay , mJun , mJul , mAug , mSep , mOct , mNov , mDec
ORDER BY dDay) S2
group by
case when Tot = 'Tot' then
   Tot else
   dDay    end
ORDER BY dDay
;
END

顺便说一句,您会在此网站上看到技巧的使用(非常感谢RenéNyffenegger,请参见下面的NB),它使我不仅可以提供想要的年份,而且可以在关闭时提供一个用于控制GROUP BY命令的参数。该过程。

NB。显然,您不能支持现有答案来向他人确认有效或可用。我在上面的参考文献中发布了答案,并立即将其删除。