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 1p4tgL-0000ad-U2 for pgsql-sql@arkaria.postgresql.org; Tue, 13 Dec 2022 01:02:05 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1p4tgK-0007bV-PY for pgsql-sql@arkaria.postgresql.org; Tue, 13 Dec 2022 01:02:04 +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 1p4tgK-0007bM-FR for pgsql-sql@lists.postgresql.org; Tue, 13 Dec 2022 01:02:04 +0000 Received: from mail-oi1-x232.google.com ([2607:f8b0:4864:20::232]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1p4tgH-0004nJ-VM for pgsql-sql@lists.postgresql.org; Tue, 13 Dec 2022 01:02:04 +0000 Received: by mail-oi1-x232.google.com with SMTP id t62so12896910oib.12 for ; Mon, 12 Dec 2022 17:02:01 -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=56WeLgZ3r4Lnta3qQq+LZyGpYPRd/yAt3eJa9n/Eyow=; b=l1d8qN41XRNg35I5MHnqq9bIN5q9LZwHxob7Xp81Kbf7t+XGhQzUqhQPsa44K3jq0q x1+nUlweWMxnrpeynlFDjgaKED3c1Kuzf7Ce/rzFJeSBYCx7yTPErsTaXoE4ren8tKfs E26mu5TfWax7K9w5/FMrU2oTFDRd4sH4e3BMkGw/oB1MS7Jxi4/GvFdtwFCpdNnDoKFT CZrJmDhln987vk0iCCmQ57F7d3NQTmwzjAHRP5NmwqelJl1vGR9P1N/aYaveXloWHxz3 t+YeaYa/ts6nQVmH4f0d11gh6SlffC2UEueWYt6nYMMyGRITO5Il2H9TkzLVHdmSIMoG qX1w== 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=56WeLgZ3r4Lnta3qQq+LZyGpYPRd/yAt3eJa9n/Eyow=; b=arO0fq3h4UVkxOPsOXY8D7euVkMaDX2+ijZrjluAxYfVCqStqnoYS035G3kmTyCdwC eaevy8REhHEetv1j0Xq3D4QS+DqLJPK7uSKs7oSt6/yIXPZmeXqtUUcYsg5IzuupmLKo gRh8zEN0XRTIV9RKV1yvKfpGvau3u3CmStuxfRIGLbcvZESMxjV+m9bZfaAxwAiPzCuO q93NCmMYNN8k83XLOPPf2H/kUm6dKaaJUNAwcKWPRCfIMz+jfs1ok6EsdhDyzOIpB60g J0RTJx1Cc3Xp/0JFZMZlL5wFPKa1t/2vbFwQwSfLg47vHLBVt6W8XUh/AkgC9qn6n3A8 mY1w== X-Gm-Message-State: ANoB5pmb+VteT7qAM8P+d9D+GpCFouZUkliwiPwE91uIn2ENL8RJBY7u d3U0X9DUWKAGgKT4GdP/u6X0HYgq87kSEV9g8Rgojq89wds= X-Google-Smtp-Source: AA0mqf77+3lhIEWAABVohSkpsrvhSvl89QJ1Ek1xjwHqWm+KEf64+zy3pvfVLS10qQxeqUxAOecb1t+RH07csM+iULU= X-Received: by 2002:a05:6808:8f9:b0:359:dc34:5b5e with SMTP id d25-20020a05680808f900b00359dc345b5emr45004oic.259.1670893319980; Mon, 12 Dec 2022 17:01:59 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: From: David Rowley Date: Tue, 13 Dec 2022 14:01:35 +1300 Message-ID: Subject: Re: Tom changed his job role many times To: Shaozhong SHI Cc: pgsql-sql Content-Type: text/plain; charset="UTF-8" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk 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