如何将两个不同的字段值合并为一行?

时间:2013-12-24 00:18:21

标签: mysql sql merge

我需要通过将两个相似但略有不同的维度字段值合并为一个新行来清理一些数据,这两个新行将两个指标值相加,保持uid和日期不变。

目前的设置如下:

 ╔═════╦═════════════╦══════╦═══════════╦═══════════╗
 ║ id  ║ date        ║ uid  ║ source    ║ pageviews ║
 ╠═════╬═════════════╬══════╬═══════════╬═══════════╣
 ║  1  ║ 2013-12-11  ║ 111  ║ source1   ║   14      ║
 ║  3  ║ 2013-12-11  ║ 111  ║ source1a  ║   1       ║
 ║  11 ║ 2013-12-11  ║ 222  ║ source1   ║   3       ║
 ║  19 ║ 2013-12-11  ║ 222  ║ source1a  ║   11      ║
 ╚═════╩═════════════╩══════╩═══════════╩═══════════╝

我想将source1和source1a视为相等并合并两者,得到这个:

 ╔═════╦═════════════╦══════╦══════════╦═══════════╗
 ║ id  ║ date        ║ uid  ║ source   ║ pageviews ║
 ╠═════╬═════════════╬══════╬══════════╬═══════════╣
 ║  1  ║ 2013-12-11  ║ 111  ║ source1  ║   15      ║
 ║  2  ║ 2013-12-11  ║ 222  ║ source1  ║   14      ║
 ╚═════╩═════════════╩══════╩══════════╩═══════════╝

id并不重要,我原计划在新表中重新增加结果

这是我尝试的,但它没有合并两个记录 - 我得到匹配的值但仍然是单独的行:

SELECT date, uid, (SELECT CASE
WHEN source = 'source1a' THEN 'source1'
ELSE source
END) AS 'source', pageviews
FROM trafficSourceMedium
GROUP BY date, source, userid

1 个答案:

答案 0 :(得分:2)

聚合查询应该执行您想要的操作:

select `date`, uid,
       (case when source = 'source1a' then 'source1' else source end) as source,
       sum(pageviews) as pageviews
from trafficSourceMedium
group by `date`, uid,
          (case when source = 'source1a' then 'source1' else source end);