Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eCQks-0004bP-Dl for pgsql-sql@arkaria.postgresql.org; Wed, 08 Nov 2017 13:50:58 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1eCQkr-0000tj-SJ for pgsql-sql@arkaria.postgresql.org; Wed, 08 Nov 2017 13:50:57 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1eCQkq-0000tW-Tf for pgsql-sql@postgresql.org; Wed, 08 Nov 2017 13:50:57 +0000 Received: from post.visena.com ([46.226.10.50]) by makus.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84_2) (envelope-from ) id 1eCQki-0000Ne-VX for pgsql-sql@postgresql.org; Wed, 08 Nov 2017 13:50:55 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=visena.com; s=20141101.wh; h=Content-Type:MIME-Version:Subject:In-Reply-To:Message-ID:To:From:Date; bh=OB02S92bx0xtiYoc198wIUSbqZB4+BXTCD2icPcQYz4=; b=lhO7mbAfyy2ZwmbGS9G4GY8yRE9gpc7/2fzwUeqP7Em1uuSAa7cQRSeOLEDC7HIewbsH4S8jvOLweik/AKGVFTxBVXx9xRj2NtRKJamHtktVwJ3CSbo7PS4fq3UafdJ2m7rW59haZTQ1GDgGeym0uoEPyyDC1Z4q82oL08XaMuE=; Received: from [10.0.1.10] (helo=tc7-visena.wh.internal.visena.com) by post.visena.com with esmtp (Exim 4.82) (envelope-from ) id 1eCQka-0006QY-TY for pgsql-sql@postgresql.org; Wed, 08 Nov 2017 14:50:46 +0100 Received: from localhost ([127.0.0.1] helo=tc7-visena.wh.internal.visena.com) by tc7-visena.wh.internal.visena.com with esmtp (Exim 4.86_2) (envelope-from ) id 1eCQkH-0003z2-0a for pgsql-sql@postgresql.org; Wed, 08 Nov 2017 14:50:21 +0100 Date: Wed, 8 Nov 2017 14:50:20 +0100 (CET) From: Andreas Joseph Krogh To: pgsql-sql@postgresql.org Message-ID: In-Reply-To: Subject: Re: Problems with PARTITION BY with count() in window-func MIME-Version: 1.0 X-Mailer: Visena Mail 2.1.0-SNAPSHOT X-Spam-Score: -1.0 X-Spam-Report: SpamAssasin (score=-1.0, required 5.0 ALL_TRUSTED=-1,HTML_MESSAGE=0.001) Content-Type: multipart/related; boundary="----=_Part_311_1938767750.1510149020776" List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-sql Precedence: bulk Sender: pgsql-sql-owner@postgresql.org ------=_Part_310_1247780025.1510149020775 Content-Type: multipart/related; boundary="----=_Part_311_1938767750.1510149020776" ------=_Part_311_1938767750.1510149020776 Content-Type: multipart/alternative; boundary="----=_Part_312_2114681951.1510149020799" ------=_Part_312_2114681951.1510149020799 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable P=C3=A5 onsdag 08. november 2017 kl. 11:17:39, skrev Andreas Joseph Krogh < andreas@visena.com >: Hi all. =C2=A0 I'm trying to count()=C2=A0all log-entries per activity per month in a sepa= rate=20 column using count() over(partition by ...) but get an error I don't unders= tand. [snip] select date_trunc('month', log.start_date::TIMESTAMP WITHOUT TIME ZONE) AS= =20 month , log.activity_id , count(log.entity_id) AS num_logs , count(log .entity_id)OVER total_for_month_window AS total_for_month FROM log_entry lo= g=20 WHERE1 =3D 1 AND date_trunc('month', log.start_date::TIMESTAMP WITHOUT TIME= ZONE)=20 BETWEEN'2017-01-01'::DATE AND '2017-05-01'::DATE GROUP BY month, log.activi= ty_id WINDOWtotal_for_month_window AS (PARTITION BY date_trunc('month', log .start_date::TIMESTAMP WITHOUT TIME ZONE)) ORDER BY date_trunc('month', log .start_date::TIMESTAMP WITHOUT TIME ZONE) ASC ;=20 =C2=A0 Note the missing log.entity_id in GROUP BY.=C2=A0 =C2=A0 but this gives the error: =C2=A0 [42803] column "log.entity_id" must appear in the GROUP BY clause or be use= d=20 in an aggregate function Details =C2=A0 This query gives the desired results: =C2=A0 SELECT DISTINCT date_trunc('month', log.start_date::TIMESTAMP WITHOUT TIME = ZONE )AS month , log.activity_id , count(log.entity_id) OVER(partition by date_t= runc( 'month', log.start_date::TIMESTAMP WITHOUT TIME ZONE), log.activity_id) AS= =20 num_logs_per_activity ,count(log.entity_id) OVER (partition by date_trunc( 'month', log.start_date::TIMESTAMP WITHOUT TIME ZONE)) AS total_for_month F= ROM=20 log_entrylog ORDER BY date_trunc('month', log.start_date::TIMESTAMP WITHOUT= =20 TIME ZONE) ASC, log.activity_id ASC ;=20 month activity_id num_logs_per_activity total_for_month 2017-01-01=20 00:00:00.000000 1 2 4 2017-01-01 00:00:00.000000 2 2 4 2017-02-01=20 00:00:00.000000 1 5 6 2017-02-01 00:00:00.000000 2 1 6=20 =C2=A0 =C2=A0 But I'd like a solution without the DISTINCT, if one exists? =C2=A0 If I introduce a new column, logged_for, and want to list number of logged= =20 entries per person per activity I can use this: =C2=A0 SELECT DISTINCT date_trunc('month', log.start_date::TIMESTAMP WITHOUT TIME = ZONE )AS month , log.logged_for , log.activity_id , count(log.entity_id) OVER( partition bydate_trunc('month', log.start_date::TIMESTAMP WITHOUT TIME ZONE= ),=20 log.logged_for, log.activity_id) AS num_logs_per_person_for_activity , coun= t(log .entity_id)OVER (partition by date_trunc('month', log.start_date::TIMESTAMP= =20 WITHOUT TIME ZONE), log.logged_for) AS total_for_person_for_month FROM log_= entry logORDER BY date_trunc('month', log.start_date::TIMESTAMP WITHOUT TIME ZONE= ) ASC ;=20 Which gives: month logged_for activity_id num_logs_per_person_for_activity=20 total_for_person_for_month 2017-01-01 00:00:00.000000 5 1 2 4 2017-01-01=20 00:00:00.000000 5 2 2 4 2017-01-01 00:00:00.000000 6 1 2 4 2017-01-01=20 00:00:00.000000 6 2 2 4 2017-02-01 00:00:00.000000 5 1 5 6 2017-02-01=20 00:00:00.000000 5 2 1 6 2017-02-01 00:00:00.000000 6 1 5 6 2017-02-01=20 00:00:00.000000 6 2 1 6=20 =C2=A0 =C2=A0 Is this the recommended=C2=A0way to do this, I mean - having "group by" in = the=20 "partition by" belonging to the OVER()-clause of the count-aggregates? =C2=A0 Thanks. =C2=A0 -- Andreas Joseph Krogh CTO / Partner - Visena AS Mobile: +47 909 56 963 andreas@visena.com www.visena.com =C2=A0 ------=_Part_312_2114681951.1510149020799 Content-Type: text/html;charset=UTF-8 Content-Transfer-Encoding: quoted-printable
P=C3=A5 onsdag 08. november 2017 kl. 11:17:39, skrev Andreas Joseph Kr= ogh <andreas@visena.com>:
Hi all.
=C2=A0
I'm trying to count()=C2=A0all log-entries per activity per month in a= separate column using count() over(partition by ...) but get an error I do= n't understand.
[snip]
select
      date_trunc('month', log.start_date::TIMESTAMP WITHOUT TIME ZONE) AS month
    , log.activity_id
    , count(log.entity_id) AS num_logs
    , count(log.entity_id) OVER total_for_month_window AS total_for_month
FROM
    log_entry=
 log
WHERE 1 =3D 1
      AND date_=
trunc('month'=
, log.start_date::TIMESTAMP WITHOUT TIME ZONE) BETWEEN '2017-01-01'::DATE AND '2017-05-01'::DATE
GROUP BY month, log.activity_id
WINDOW total_for_mon=
th_window AS (PARTITION BY date_trunc('month', log.start_date::=
TIMESTAMP WITHOUT TIME ZONE))

ORDER BY date_trunc('month', log.start_date::=
TIMESTAMP WITHOUT TIME ZONE) ASC
;
=C2=A0
Note the missing log.entity_id in GROUP BY.=C2=A0
=C2=A0
but this gives the error:
=C2=A0
[42803] column "log.entity_id" must appear in the GROUP BY c= lause or be used in an aggregate function
Details
=C2=A0
This query gives the desired results:
=C2=A0
SELECT DISTINCT
      date_trunc('month', log.start_date::TIMESTAMP WITHOUT TIME ZONE) AS month
    , log.activity_id
    , count(log.entity_id) OVER(partition by date_trunc('month'=
, log.start_d=
ate::TIMESTAMP WITHOUT TIME=
 ZONE), log.a=
ctivity_id) AS num_l=
ogs_per_activity
    , count(log.entity_id) OVER (partition by date_trunc('month=
', log.start_=
date::TIMESTAMP WITHOUT TIM=
E ZONE)) AS t=
otal_for_month
FROM
    log_entry=
 log
ORDER BY date_trunc('month', log.start_date::TIMESTAMP WITHOUT TIME ZONE) ASC, log.activity_id ASC
;
=09 =09=09 =09=09=09 =09=09=09 =09=09=09 =09=09=09 =09=09 =09=09 =09=09=09 =09=09=09 =09=09=09 =09=09=09 =09=09 =09=09 =09=09=09 =09=09=09 =09=09=09 =09=09=09 =09=09 =09=09 =09=09=09 =09=09=09 =09=09=09 =09=09=09 =09=09 =09=09 =09=09=09 =09=09=09 =09=09=09 =09=09=09 =09=09 =09
monthactivity_idnum_logs_per_activitytotal_for_month
2017-01-01 00:00:00.000000124
2017-01-01 00:00:00.000000224
2017-02-01 00:00:00.000000156
2017-02-01 00:00:00.000000216
=C2=A0
=C2=A0
But I'd like a solution without the DISTINCT, if one exists?
=C2=A0
If I introduce a new column, logged_for, and want to list number of lo= gged entries per person per activity I can use this:
=C2=A0
SELECT DISTINCT
    date_trunc('month', log.start_date::TIMESTAMP WITHOUT TIME ZONE) AS month
    , log.logged_for
    , log.activity_i=
d
    , count(log.entity_id) OVER(partition by date_trunc('month'=
, log.start_d=
ate::TIMESTAMP WITHOUT TIME=
 ZONE), log.l=
ogged_for, log.activ=
ity_id) AS num_logs_=
per_person_for_activity
    , count(log.entity_id) OVER (partition by date_trunc('month=
', log.start_=
date::TIMESTAMP WITHOUT TIM=
E ZONE), log.=
logged_for) AS total=
_for_person_for_month
FROM
    log_entry=
 log
ORDER BY date_trunc('month', log.start_date::TIMESTAMP WITHOUT TIME ZONE) ASC
;
Which gives:
=09 =09=09 =09=09=09 =09=09=09 =09=09=09 =09=09=09 =09=09=09 =09=09 =09=09 =09=09=09 =09=09=09 =09=09=09 =09=09=09 =09=09=09 =09=09 =09=09 =09=09=09 =09=09=09 =09=09=09 =09=09=09 =09=09=09 =09=09 =09=09 =09=09=09 =09=09=09 =09=09=09 =09=09=09 =09=09=09 =09=09 =09=09 =09=09=09 =09=09=09 =09=09=09 =09=09=09 =09=09=09 =09=09 =09=09 =09=09=09 =09=09=09 =09=09=09 =09=09=09 =09=09=09 =09=09 =09=09 =09=09=09 =09=09=09 =09=09=09 =09=09=09 =09=09=09 =09=09 =09=09 =09=09=09 =09=09=09 =09=09=09 =09=09=09 =09=09=09 =09=09 =09=09 =09=09=09 =09=09=09 =09=09=09 =09=09=09 =09=09=09 =09=09 =09
monthlogged_foractivity_idnum_logs_per_person_for_activitytotal_for_person_for_month
2017-01-01 00:00:00.0000005124
2017-01-01 00:00:00.0000005224
2017-01-01 00:00:00.0000006124
2017-01-01 00:00:00.0000006224
2017-02-01 00:00:00.0000005156
2017-02-01 00:00:00.0000005216
2017-02-01 00:00:00.0000006156
2017-02-01 00:00:00.0000006216
=C2=A0
=C2=A0
Is this the recommended=C2=A0way to do this, I mean - having "gro= up by" in the "partition by" belonging to the OVER()-clause = of the count-aggregates?
=C2=A0
Thanks.
=C2=A0
--
Andrea= s Joseph Krogh
CTO / Partner<= /span> - Visena AS
Mobile: +47 90= 9 56 963
=3D""
=C2=A0
------=_Part_312_2114681951.1510149020799-- ------=_Part_311_1938767750.1510149020776 Content-Type: image/png Content-Transfer-Encoding: base64 Content-Disposition: inline Content-ID: iVBORw0KGgoAAAANSUhEUgAAAIUAAAAYCAYAAADUIj6hAAAABHNCSVQICAgIfAhkiAAABzBJREFU aEPtmNFxHDcMhmVP3i1VECpvnjzkVIHWFfhcgVcVRKrAUgWRK/C6Al8H3lTgy0PGbzFdQc4VJP/H ADs43q6kROeJNbOYgQACIAgCWJKng4MZ5gxUGXh0U0b+SIuF9G+EWXj2Q15vbrE/NPtk9uub7Gfd t5mBx1NhqSEo8HshjbEUtlO2yIM9tsx5dZP9rPt2MzDZFAqZE4LGcFjdsg3saQaHt7fY70X949On aS+OZidDBkavD33157L4xaw2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