Locoia
Search…
Useful SQL Examples
Some SQL Examples that might help you calculate that one value.

Calculating a cumulative Value

Situation: You have a calculation that tells you e.g. daily new subscribers. Now you want to add a bar-chart that displays the running total for each day (but this value doesn't exist anywhere). We can achieve this by joining the table onto itself:
1
SELECT
2
t1.date,
3
(
4
SELECT
5
SUM(new_subscriptions)
6
FROM
7
daily_subscriber_change t2 where t1.date >= t2.date
8
) active_subscribers
9
FROM daily_subscriber_change t1
10
WHERE
11
date >= "$start_date"
12
AND
13
date <= "$end_date"
Copied!
Last modified 3d ago
Copy link