vudupins
vudupins

Reputation: 39

How would you query a cumulative sum over x amount of days?

I'm trying to find the cumulative sum of sessions of a link for its first 3 days. I tried this but it doesn't seem to take the date clause into account:

    select
        date,
        link,
        sum(sessions) as sessions
            from ga
            where date <= date+interval 3 day
            group by link

But if I manually enter a date, it seems to work. Why is it not seeing date+interval 3 day as a proper date...?

Any help would be greatly appreciated! :)

Upvotes: 0

Views: 195

Answers (2)

Ravi
Ravi

Reputation: 31417

You need to also add date column in GROUP BY clause. Also, avoid using column name as date. It will create confusion.

Try below query :

select date_column,
        link,
        sum(sessions) as sessions
            from ga
            where date_column BETWEEN CURDATE()-3 AND CURDATE()
            group by link, date_column

Upvotes: 0

Read Hughes
Read Hughes

Reputation: 53

Date is a column, not a value, you need to provide a specific date entry. Also "between" is a better keyword to use in this situation.

Upvotes: 1

Related Questions