SELECT date, id, sum(revenue)
FROM table
WHERE date between '2013-01-01' and '2013-01-08'
GROUP BY date, id
HAVING sum(revenue)>1000
返回收入>> 1000的行。
SELECT date, id, sum(revenue)
FROM table
WHERE date between '2013-01-01' and '2013-01-08'
AND id IN (SELECT id FROM table where date between '2013-01-01' and '2013-01-08' GROUP BY id HAVING sum(revenue)>1000)
GROUP BY date, id
返回id的行,其日期间的总收入根据需要为> 1000。但是这个查询要慢得多。有没有更快的方法呢?
答案 0 :(得分:1)
确保您在date
和id
列上有索引,并尝试以下变体:
select t.date, t.id, sum(t.revenue)
from table t
inner join (
select id
from table
where date between '2013-01-01' and '2013-01-08'
group by id
having sum(revenue) > 1000
) ts on t.id = ts.id
where t.date between '2013-01-01' and '2013-01-08'
group by t.date, t.id
答案 1 :(得分:1)
它不是MySQL,它是Vertica;)
Cris,您在order by
???
CREATE TABLE
您尝试使用database designer
吗?
见http://my.vertica.com/docs/6.1.x/HTML/index.htm#14415.htm