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 1p4rWE-0003BU-9A for pgsql-sql@arkaria.postgresql.org; Mon, 12 Dec 2022 22:43:30 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1p4rWC-0003uj-I6 for pgsql-sql@arkaria.postgresql.org; Mon, 12 Dec 2022 22:43:28 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1p4rWC-0003ua-85 for pgsql-sql@lists.postgresql.org; Mon, 12 Dec 2022 22:43:28 +0000 Received: from mail-ej1-x62c.google.com ([2a00:1450:4864:20::62c]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1p4rW5-0001V8-BF for pgsql-sql@lists.postgresql.org; Mon, 12 Dec 2022 22:43:26 +0000 Received: by mail-ej1-x62c.google.com with SMTP id t17so32099507eju.1 for ; Mon, 12 Dec 2022 14:43:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=24VvwuLb6XMq15JBogUzOSteQB1lPVuYNK1lk8hQ4VI=; b=IakVG/dr0wLx/mbeU67rxA4xVmA8MzB/CvfBNS6cl+qKB/Kona/vd8JNmA1gZSJSHh DFDj9JN1SmVul8QfQbSkMhLOH3yAAD96eFJoe0nvXxmNCSiRxX5ndUR88SmEosXfsu2n o2tgw8v26ExNx5jrIC4mfz8J9hkWnyrJyqtyjnaEe4DnWpu8sosMOnPvx3FqBjTS215w wh94K15Cs3W7SJ8jWzaF4Pjr2CE36nVFmXq4/De0fvN4CpmD5P6qkg57UkBcvtC0amUy M1Prl2yjo/4Nev1A1JtDrm3xwrk0zVyccANa1A4YHXTb/AqfJ70prtybHWeWnYrJwngs qEtw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=to:subject:message-id:date:from:mime-version:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=24VvwuLb6XMq15JBogUzOSteQB1lPVuYNK1lk8hQ4VI=; b=gplZcnnrbc1x45to8haDM2rrEZt0issS/W+exr6/TVq2I4pE7K1zGtxWfeJhHp/UhL J2Iq2vv48nfBAtzkDIC0q7XzDmXwtBY27pIOJGddyc/uTuTUvYGYH1lFiseLhjLThL3+ 0tVDyKE8KbnhixvvSlhwCkxEMYsYGcUC+Rx7jYDzCOFF2F9oAofK+48q9bQaAgf09DpL 20PA1gVa3npTj+n+bVEhJtys+6PsFUH8ezBMrLYnlFL0rkCqGOgNP7Xt6a7hCeqrKmqO kuhKQl7gJo8xeAj//YkJJWDOJpygJ3jXEQpYCzEmw+ABXAae4HGOOPB13JQKqGJ6j5GF +I7A== X-Gm-Message-State: ANoB5pkLpQgFP4HHCbFD+nLjhMJQNVSfM6HDxDNZSmRcNztYvobn9sVo aAHpXbaiA7nBO4sS8Rq6vLW2+aUB+LUehT0wW7YmIKiFMkQ= X-Google-Smtp-Source: AA0mqf5TQsMjs5PdhuiOmkjOfcuYj16pkrMX8LiJTc76EAAusm+qaUyXW7K/ASd5br9vQo5QNa4SeAW8h42/XVeUeHc= X-Received: by 2002:a17:906:38cd:b0:7be:4d3c:1a44 with SMTP id r13-20020a17090638cd00b007be4d3c1a44mr46949218ejd.543.1670884999827; Mon, 12 Dec 2022 14:43:19 -0800 (PST) MIME-Version: 1.0 From: Shaozhong SHI Date: Mon, 12 Dec 2022 22:43:08 +0000 Message-ID: Subject: Tom changed his job role many times To: pgsql-sql Content-Type: multipart/alternative; boundary="000000000000e016e305efa93aef" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --000000000000e016e305efa93aef Content-Type: text/plain; charset="UTF-8" Tom has changed his job role many times. We want to pick up only the records before and after his role change to show what happened. select *, coalesce(lag(department) over(partition by name order by year), department) <> department Changed from (Values (1, 'Tom', 'Sales', 1990),(2, 'Tom', 'Sales', 1991),(3, 'Tom', 'Sales', 1991),(4, 'Tom', 'Management', 1992),(4, 'Tom', 'Management', 1992), (4, 'Tom', 'Sales', 1993),(4, 'Tim', 'Finance', 1981), (4, 'Tim', 'Finance', 1982), (4, 'Tim', 'Management', 1983), (4, 'Tim', 'Management', 1984)) as x(Staff_ID, Name, Department, Year); Regards, David --000000000000e016e305efa93aef Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Tom has changed his job role many ti= mes.

We want to pick up only the records before an= d after his role change to show what happened.

sel= ect *, coalesce(lag(department) over(partition by name order by year), depa= rtment) <> department Changed=C2=A0
from (Values (1, 'T= om', 'Sales', 1990),(2, 'Tom', 'Sales', 1991),(= 3, 'Tom', 'Sales', 1991),(4, 'Tom', 'Management= ', 1992),(4, 'Tom', 'Management', 1992), (4, 'Tom&#= 39;, 'Sales', 1993),(4, 'Tim', 'Finance', 1981), (4= , 'Tim', 'Finance', 1982), (4, 'Tim', 'Manageme= nt', 1983), (4, 'Tim', 'Management', 1984)) as x(Staff_= ID, Name, Department, Year);

Regards,
David
--000000000000e016e305efa93aef--