我如何在mysql中做模糊组?

时间:2015-07-29 19:23:16

标签: mysql

我在MySQL中有这个查询:

select 
    hour,
    traffic_source_name,
    COUNT(*)
from 
    my_data_table
group by
    hour,
    traffic_source_name

这给了我这样的结果:

'h', 'traffic_source_name', 'COUNT(*)
'2015-07-28 00:00', 'google', '628'
'2015-07-28 00:00', 'Google Tier 2 Search', '1'
'2015-07-28 00:00', 'Taboola/Outbrain Content', '3'
'2015-07-28 00:00', 'yahoo', '3'
'2015-07-28 00:00', 'Email', '67'
'2015-07-28 00:00', 'msn', '253'
'2015-07-28 00:00', 'Facebook Social Media', '139'
'2015-07-28 01:00', 'yahoo', '2'
'2015-07-28 01:00', 'Email', '46'
'2015-07-28 01:00', 'Popular Marketing', '1'
'2015-07-28 01:00', 'Yahoo Stream Ads Content', '3'
'2015-07-28 01:00', 'Facebook Social Media', '183'
'2015-07-28 01:00', 'google', '530'
'2015-07-28 01:00', 'msn', '210'

事情是,我想结合谷歌'和'谷歌第2层搜索'作为' google'和'雅虎'和雅虎流广告内容'进入雅虎'。我该怎么做呢?

1 个答案:

答案 0 :(得分:2)

转换组之前的值,如下所示:

select 
    hour,
    IF (traffic_source_name IN ('google', 'Google Tier 2 Search'), 'google'
    , IF (traffic_source_name IN ('yahoo', 'Yahoo Stream Ads Content'), 'yahoo'
    , traffic_source_name
    )) AS tsn,
    COUNT(*)
from 
    my_data_table
group by
    hour,
    tsn

这可能具有更好的可读性:

select 
    hour,
    CASE traffic_source_name 
    WHEN 'Google Tier 2 Search' THEN 'google'
    WHEN 'Yahoo Stream Ads Content' THEN 'yahoo'
    ELSE traffic_source_name
    END AS tsn,
    COUNT(*)
from 
    my_data_table
group by
    hour,
    tsn

第一个允许你使分组值更明显,而第二个让你避免必须维护嵌套的IF(,, IF(,, IF ...)))东西....或者我们可以做到这一点最好的两个:

select 
    hour,
    CASE 
    WHEN traffic_source_name IN ('google', 'Google Tier 2 Search') 
      THEN 'google'
    WHEN traffic_source_name IN ('yahoo', 'Yahoo Stream Ads Content')
      THEN 'yahoo'
    ELSE traffic_source_name
    END AS tsn,
    COUNT(*)
from 
    my_data_table
group by
    hour,
    tsn