我遇到了以下问题:
+------+---------+--------+ | Date | Keyword | clicks | +------+---------+--------+ | D 1 | kw 1 | 50 | | D 2 | kw 1 | 23 | | D 3 | kw 1 | 78 | | D 4 | kw 1 | 80 | | D 5 | kw 1 | 80 | | D 6 | kw 1 | 11 | | D 7 | kw 1 | 100 | | D 3 | kw 2 | 4 | | D 4 | kw 2 | 11 | | D 5 | kw 2 | 67 | | D 6 | kw 2 | 90 | | D 7 | kw 2 | 34 | | D 8 | kw 2 | 64 | | D 9 | kw 2 | 25 | | D 10 | kw 2 | 62 | | D 11 | kw 2 | 42 | +------+---------+--------+
我努力了,但找不到能给我这个结果的查询:
+---------+-----------------------+--------------------------+ | Keyword | Total clicks all time | Total clicks last 3 days | +---------+-----------------------+--------------------------+ | kw 1 | 421 | 190 | | kw 2 | 398 | 129 | +---------+-----------------------+--------------------------+
非常感谢您的帮助!
答案 0 :(得分:3)
您可以使用sum()
和select case
获得所需的点击次数。
select keyword
, sum(case when date >= now() - interval 3 day then clicks else 0 end) as Total_3_Days
, sum(clicks) as Total_Clicks
from tableA
group by keyword