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

loading...


0

我需要通过将两个相似但略有不同的维度字段值合并到一个新行中来清理一些数据,这两个新的行将两个度量值相加,保持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 并不重要,我曾计划在新表中重新增加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回答

  • 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);
    

loading...

评论

暂时没有评论!