如何将多行数据转换为具有多列的行


8

我有一个这样的MySQL表:

User_Id  course_name     course_location   course_id
1        course name 1   location 1        1
1        course name 2   location 2        2
1        course name 3   location 1        3
2        course name 2   location 1        2
2        course name 4   location 4        4

我怎样才能得到这样的结果数据:

User_id  course 1       course2        course3        course4
1        yes-location1  yes-location2  yes-location1  NULL
2        NULL           yes-location1  NULL           yes-location4

1
您正在使用哪个版本的MySQL?您想要的被称为数据透视,这里的这个答案stackoverflow.com/questions/7674786/mysql-pivot-table应该告诉您您正在寻找什么。
乔纳森·费特

Answers:


7

您必须使用GROUP BYwith MAX汇总数据透视表,并使用CASE过滤条件User_id

查询:

SELECT User_id
    , MAX(
        CASE WHEN course_id = 1 THEN course_location END
    ) as Course_1
    , MAX(CASE WHEN course_id = 2 THEN course_location END) as Course_2
    , MAX(CASE WHEN course_id = 3 THEN course_location END) as Course_3
    , MAX(CASE WHEN course_id = 4 THEN course_location END) as Course_4
FROM data
GROUP BY User_id;

SQL Fiddle中的示例查询。

您可以替换course_locationCONCAT('YES-', course_location)是全球领先的YES确实是必要的。

输出:

User_Id | Course_1      | Course_2      | Course_3      | Course_4
1       | location 1    | location 2    | location 1    | (null)
2       | (null)        | location 1    | (null)        | location 4

这是一个旧线程,但是执行此操作的性能如何?如果我要注入15个CASE字段,使用这种方法,如果在数据库中说有100,00o的结果,将会有严重的性能吗?问题?
timothymarois,

-1

表创建:

CREATE TABLE VMRRTEST_DATA (
        ID INTEGER, 
        CATG VARCHAR(30 ), 
        PRICE INTEGER, 
        DATE TIMESTAMP
    )

插入:

INSERT INTO VMRRTEST_DATA VALUES(1,'CAR',1000,CURRENT TIMESTAMP);

INSERT INTO VMRRTEST_DATA VALUES(2,'CAR',2000,(CURRENT TIMESTAMP)- 10 MINUTE);

INSERT INTO VMRRTEST_DATA VALUES(3,'CAR',1000,CURRENT TIMESTAMP - 20 minute);


INSERT INTO VMRRTEST_DATA VALUES(4,'CAR',30000,CURRENT TIMESTAMP);

INSERT INTO VMRRTEST_DATA VALUES(5,'CAR',5000,(CURRENT TIMESTAMP)-20 minute);

结果集:

ID CATG PRICE DATE
 -- ---- ----- --------------------------
  1 CAR   1000 2018-10-09 22:17:59.907636

  4 CAR  30000 2018-10-09 22:18:32.58254

  2 CAR   2000 2018-10-09 22:30:30.875961

  3 CAR   1000 2018-10-09 22:20:43.80537

  5 CAR   5000 2018-10-09 22:21:14.787224

预期结果集:

 RANK ID CATG PRICE DATE
 ---- -- ---- ----- --------------------------
    1  3 CAR   1000 2018-10-09 22:20:43.80537
    1  2 CAR   2000 2018-10-09 22:30:30.875961
    1  5 CAR   5000 2018-10-09 22:21:14.787224
    1  4 CAR  30000 2018-10-09 22:18:32.58254

解:

SELECT Rank,ID,CATG,PRICE,DATE FROM 
( 

SELECT
--ROW_NUMBER() OVER(ORDER BY PRICE ASC) AS RW_NM,
RANK() OVER (PARTITION BY PRICE ORDER BY DATE DESC) AS Rank,

ID,CATG,
PRICE
,DATE 
FROM  
VMRRTEST_DATA 
--WHERE Rank 
GROUP BY ID,DATE,CATG,PRICE

HAVING COUNT(*)>0
ORDER BY PRICE,DATE DESC
)
WHERE Rank = 1

抱歉,您的答案无法回答问题。
Colin't Hart
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.