Ich habe die folgende Abfrage monatliche kumulative Benutzer zählt zu erhalten:Erhalten kumulative monatliche zählen Benutzer basierend auf Benutzer-E-Mail
SELECT RTRIM(TO_CHAR(DATE_TRUNC('month', created_at), 'Month YYYY')) AS month,
SUM(COUNT(id)::int) OVER (ORDER BY DATE_TRUNC('month', created_at)) AS total,
SUM((email like '%@domain%')::int) AS with_domain,
SUM((email not like '%@domain%')::int) AS without_domain
FROM users
GROUP BY DATE_TRUNC('month', created_at)
ORDER BY DATE_TRUNC('month', created_at);
Das Ergebnis ist wie folgt:
[
{"month"=>"October 2015", "total"=>2, "with_domain"=>1, "without_domain"=>1},
{"month"=>"December 2015", "total"=>6, "with_domain"=>4, "without_domain"=>0},
{"month"=>"January 2016", "total"=>13, "with_domain"=>4, "without_domain"=>3},
{"month"=>"February 2016", "total"=>15, "with_domain"=>2, "without_domain"=>0},
{"month"=>"March 2016", "total"=>36, "with_domain"=>15, "without_domain"=>6},
]
See, wie total
Zählung kumulativ und andere nicht?
Ich habe versucht, die folgende Abfrage:
SELECT RTRIM(TO_CHAR(DATE_TRUNC('month', created_at), 'Month YYYY')) AS month,
SUM(COUNT(id)::int) OVER (ORDER BY DATE_TRUNC('month', created_at)) AS total,
SUM((email like '%@domain%')::int) OVER (ORDER BY DATE_TRUNC('month', created_at)) AS with_domain,
SUM((email not like '%@domain%')::int) OVER (ORDER BY DATE_TRUNC('month', created_at)) AS without_domain
FROM users
GROUP BY DATE_TRUNC('month', created_at)
ORDER BY DATE_TRUNC('month', created_at);
Aber es wirft
column "users.email" must appear in the GROUP BY clause or be used in an aggregate function LINE 3: SUM((email like '%@domain%')::int) OVER (ORDER BY...^: SELECT RTRI
und das Hinzufügen von email
-GROUP BY
Klausel überhaupt keinen Sinn macht ...
Wie kann ich alles machen kumuliert (wie es mit total
getan wird)?
Erwartetes Ergebnis ist wie
[
{"month"=>"October 2015", "total"=>2, "with_domain"=>1, "without_domain"=>1},
{"month"=>"December 2015", "total"=>6, "with_domain"=>5, "without_domain"=>1},
{"month"=>"January 2016", "total"=>13, "with_domain"=>9, "without_domain"=>4},
{"month"=>"February 2016", "total"=>15, "with_domain"=>11, "without_domain"=>4},
{"month"=>"March 2016", "total"=>36, "with_domain"=>26, "without_domain"=>10},
]
Ihr Fehler ist, dass Sie für die laufende Summe zu erhalten '(E-Mail [not] like‚% @ domain% versuchen, ') :: int' anstatt für 'SUM (email [nicht] wie'% @ domain% ') :: int)'. –