SQL查询连接

时间:2015-12-23 20:48:47

标签: mysql sql

我有一张表格如下:

name        week    effort
quentin     1       1
quentin     1       2
quentin     2       1
tracy       1       1       
joe         2       2

只有少数几个独特的名称,所以它不需要是动态的

我想查询它返回类似

的内容
week    QuentinEffortSum    TracyEffortSum  JoeEffortSum
1       3                   1               0
2       1                   0               2

我尝试了一些

的内容
SELECT SUM(Effort) AS JoeEffort, Min (Week) AS week FROM [Group$]
WHERE name = "Joe"
GROUP BY week
ORDER By week

返回:

week   JoeEffort
1      3
2      1

现在我需要其他列并想象在涉及连接但不确定如何完成任务

请帮忙

由于

1 个答案:

答案 0 :(得分:1)

我认为PIVOT表可以正常工作,如下:

SELECT *
FROM (
    SELECT 
        week,name,effort 
    FROM [Group$]
) as s
PIVOT
(
    SUM(effort)
    FOR [name] IN ('quentin','joe','tracy')
)AS pvt