Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1p5BzL-0005zq-59 for pgsql-sql@arkaria.postgresql.org; Tue, 13 Dec 2022 20:34:55 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1p5ByK-0005rI-Jz for pgsql-sql@arkaria.postgresql.org; Tue, 13 Dec 2022 20:33:52 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1p5ByK-0005r9-9y for pgsql-sql@lists.postgresql.org; Tue, 13 Dec 2022 20:33:52 +0000 Received: from mail-ed1-x52e.google.com ([2a00:1450:4864:20::52e]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1p5ByH-00084T-Bi for pgsql-sql@lists.postgresql.org; Tue, 13 Dec 2022 20:33:51 +0000 Received: by mail-ed1-x52e.google.com with SMTP id s5so19543673edc.12 for ; Tue, 13 Dec 2022 12:33:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=TzNH7nsBD7j8HPRcyhyKg/ZPOxwuR3VhxKkAmoSiLRY=; b=nR4OA75H6GR2ac1TEQbxE1m1HWHBJVD2UgsG3V35Fioh0yU+fxx8H9A59MfYSaQT50 I9qoG/l6peBK/NqkPzxTYzwSIpY8j8v+gOCaE+VqbsrgWoXj5q9fhTfxmQZHajCEeZk4 mX2PYZ0I+2DA5Ar4VOCIO7YVUeeEOccEGMoCWM1jTrtt+9Ug5pMThzEpHOSCzSmKb+xM /48wfrWqUFbDIxQRT22QPUe1jr4Qd0UzLOfjUOR7z7j4ZKO14rJLmtcK/9rUDnHKB+Uu Aa5xh3MT3I2aB4wntFjoJJGhDEKSAX2WCJ0wZwsWOXyY0RRKmx/W9/mE3+YR4VugYDZK o/vQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=TzNH7nsBD7j8HPRcyhyKg/ZPOxwuR3VhxKkAmoSiLRY=; b=rxQOvIqMTHWUXD5N+BlHTAwITQ5gHmnl0Eis0BPFtcQZG1A54wjP2UdYF4LCtv/BwB PwtSwDAqvJQ/P5X2g/nBECnbuSf0GktRGdSv3Cbf/QWEaknAV40S2nPrceeEkBirhacO k2FhbeNNB0255Ie0Vre3TDAOgjU69yJ+hgwSFrDMX4IsiUvWohGAN68eY+GOPwYDDiyS xpG91rxt62XHWXwI38AykZ09RZSVOnU6kXv+4d7U0tsHTlCrB8dJ+FKGpD7VP9CtsZuW yPDEnSOfVOmj0fr77NkGtFPeJr9mloQVq6rUuzwe01kj2KFtH70a9ptW4kq1evBIYIgH MgxQ== X-Gm-Message-State: ANoB5pleSJkea/0zshfkgVRSAsbqA3rj9tR/YCnVsjW8HU+QSExodBDg /HtKGsMKZGhj4HzQBTvg2tDSbZNWpCNfaA/OgBk= X-Google-Smtp-Source: AA0mqf7sJIjuErIFw6fCD9t/Y65z0q6IivEI3MOINqI1uYRoB0nF790y2NvenQg3T8yrRBWmukBo5O4Xo93Bfy+AFGA= X-Received: by 2002:aa7:daca:0:b0:46b:8078:2431 with SMTP id x10-20020aa7daca000000b0046b80782431mr36653045eds.408.1670963628410; Tue, 13 Dec 2022 12:33:48 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: From: Shaozhong SHI Date: Tue, 13 Dec 2022 20:33:37 +0000 Message-ID: Subject: Re: Tom changed his job role many times To: David Rowley Cc: pgsql-sql Content-Type: multipart/alternative; boundary="000000000000810b4205efbb89ee" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --000000000000810b4205efbb89ee Content-Type: text/plain; charset="UTF-8" Thanks, David. So many different ways to produce answers. Brilliant. Regards, David On Tue, 13 Dec 2022 at 01:02, David Rowley wrote: > On Tue, 13 Dec 2022 at 11:43, Shaozhong SHI > wrote: > > We want to pick up only the records before and after his role change to > show what happened. > > Window functions cannot appear in the WHERE clause, but you can add a > subquery and perform the filtering there. > > SELECT * FROM ( > SELECT name,year,department, > lag(department) over (partition by name order by year) > last_department > from mytable) t > WHERE department <> last_department or last_department is null; > > what you put in the WHERE clause will depend on what you want to > happen when the employee is first hired. last_department will be NULL > because I didn't add any default to lag like you did. You may want to > check that the COALESCE() is what you want. You may want to use > LAG(department, 1, department) instead. These are the same if > department is defined as NOT NULL, but not if there is null values in > there. > > There are a few details in [1] about window functions and how to > filter on them that you might get something out of reading. > > David > > [1] https://www.postgresql.org/docs/current/tutorial-window.html > --000000000000810b4205efbb89ee Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Thanks, David.=C2=A0 So many different ways to produce ans= wers.=C2=A0 Brilliant.=C2=A0 Regards,=C2=A0 David

On Tue, 13 Dec 2022 at 01:= 02, David Rowley <dgrowleyml@gma= il.com> wrote:
On Tue, 13 Dec 2022 at 11:43, Shaozhong SHI <shishaozhong@gmail.com> wrote= :
> We want to pick up only the records before and after his role change t= o show what happened.

Window functions cannot appear in the WHERE clause, but you can add a
subquery and perform the filtering there.

SELECT * FROM (
=C2=A0 =C2=A0 SELECT name,year,department,
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0lag(department) over (partition by= name order by year)
last_department
=C2=A0 =C2=A0 from mytable) t
WHERE department <> last_department or last_department is null;

what you put in the WHERE clause will depend on what you want to
happen when the employee is first hired. last_department will be NULL
because I didn't add any default to lag like you did. You may want to check that the COALESCE() is what you want.=C2=A0 You may want to use
LAG(department, 1, department) instead.=C2=A0 These are the same if
department is defined as NOT NULL, but not if there is null values in
there.

There are a few details in [1] about window functions and how to
filter on them that you might get something out of reading.

David

[1] https://www.postgresql.org/docs/curr= ent/tutorial-window.html
--000000000000810b4205efbb89ee--