我的查询有LEFT JOIN,group by和ROLLUP,如下所示:
Select * from
(
Select user_agent,
value,
recoqty,
count(recoqty) as C
from august_2016_search_stats SS
LEFT JOIN august_2016_extra E
on (SS.id = E.stats_id and E.key = 'personalized')
where time >= '2016-08-22 00:00:00' and
time <= '2016-08-22 23:59:59' and
query_type = 'myfeed' and
recoqty = 'topics'
group by recoqty,
user_agent,
value
with ROLLUP
having recoqty is not null
) D
order by C desc;
给出如下结果:
+------------+-------+---------+------+
| user_agent | value | recoqty | C |
+------------+-------+---------+------+
| NULL | NULL | topics | 1330 |
| abscdef | NULL | topics | 1330 |
| abscdef | NULL | topics | 1285 |
| abscdef | 1 | topics | 25 |
| abscdef | 0 | topics | 20 |
+------------+-------+---------+------+
此处,值(NULL 1285
)归因于LEFT JOIN,值(NULL 1330
)归因于汇总。
但是,是否有办法仅为NULL
替换LEFT JOIN
值,而不是ROLLUP
?
答案 0 :(得分:0)
这有点棘手,因为来自您的数据的NULL
值似乎与来自汇总的NULL
值无法区分。一种可能的解决方法是首先执行非聚合查询,使用NULL
将value
列中的'NA'
值替换为COALESCE
或其他占位符。然后使用汇总GROUP BY
将其聚合为子查询。那么NULL
列中的value
值肯定会来自汇总,而不是您的实际数据。
SELECT t.user_agent,
t.value,
t.recoqty,
t.C
FROM
(
SELECT user_agent,
COALESCE(value, 'NA') AS value
recoqty,
COUNT(recoqty) AS C
FROM august_2016_search_stats SS
LEFT JOIN august_2016_extra E
ON SS.id = E.stats_id AND
E.key = 'personalized'
WHERE time >= '2016-08-22 00:00:00' AND
time <= '2016-08-22 23:59:59' AND
query_type = 'myfeed' AND
recoqty = 'topics'
) t
GROUP BY t.recoqty,
t.user_agent,
t.value
WITH ROLLUP
HAVING t.recoqty IS NOT NULL