-1
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

根据需要返回该日期期间总收入大于 1000 的 id 的行。但是这个查询要慢得多。有更快的方法吗?

4

2 回答 2

1

确保在dateid列上有索引,并尝试以下变体:

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
于 2013-11-23T23:54:07.777 回答
1

不是 MySQL,而是 Vertica ;)

克里斯,order by你在使用什么投影CREATE TABLE???

database designer 试试看http://my.vertica.com/docs/6.1.x/HTML/index.htm#14415.htm

于 2013-11-25T05:54:27.813 回答