Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1i6zoN-0000wM-Mv for pgsql-sql@arkaria.postgresql.org; Sun, 08 Sep 2019 16:13:11 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1i6zoM-0005DY-Ab for pgsql-sql@arkaria.postgresql.org; Sun, 08 Sep 2019 16:13:10 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1i6zoM-00058w-0i for pgsql-sql@lists.postgresql.org; Sun, 08 Sep 2019 16:13:10 +0000 Received: from mail02.mbizgroup.biz ([66.220.9.54] helo=mail.drivehq.com) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1i6zoI-00080s-8n for pgsql-sql@lists.postgresql.org; Sun, 08 Sep 2019 16:13:09 +0000 Received: from (192.168.10.210) by EM244o80 with SMTP User ID0; Sun, 08 Sep 2019 16:13:00 GMT Received: from 126.255.162.228 (126.255.162.228) by mail.drivehq.com with HTTPUserID9736706; Sun, 08 Sep 2019 16:12:59 GMT X-Priority: 3 Message-ID: <2261999271000000009736706@www> From: "jj08" To: v_kalees@yahoo.com, vyegorov@gmail.com Cc: pgsql-sql@lists.postgresql.org Subject: RE:Re: RE:Re: RE:Re: A complex SQL query Date: Sun, 08 Sep 2019 09:12:59 -0700 MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_NextPart_226199927_9736706_637035307798840385" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk This is a multi-part message in MIME format. ------=_NextPart_226199927_9736706_637035307798840385 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable v_kalees: Thank you for your suggestion. I tried your code at http://www.sqlfiddle.com/#!17/4e386/4/1 but couldn't make it work. (I wanted to see two rows, but your query re= turns only one row.) My original question was to find people working for company "Micro= ", so I took liberty to add "where employer=3D'Micro'" co= ndition to your solution. = May be I put the where clause at a wrong place? Anyway, for me it is getting more complex :) Victor's solution is producing 2 rows as I wanted, so for now a crisis = has been averted! = = From: v_kalees@yahoo.com --To: vyegorov@gmail.com,jj08@drivehq.com --CC: pgsql-sql@lists.postgresql.org --Date: 9/7/2019 8:20:57 PM --Subject: Re: RE:Re: RE:Re: A complex SQL = query This is much cleaner way = select * from ( select usr_id, = employer, = start_date, end_date, rank() OVER (PARTITION BY employer ORDER BY start_date DESC) AS = rnk from ) t where t.rnk=3D1; Thanks On Sunday, September 8, 2019, 7:40:05= AM GMT+5:30, jj08 wrote: = Hello Vicor! It works!!! Thank you very much. = = = = --From: vyegorov@gmail.com --To: jj08@drivehq.com --CC: pgsql-sql@lists.postgresql.org --Date: 9/7/2019 2:33:40 PM --Subject: Re: RE:Re: A complex SQL query = =D1=81=D0=B1, 7 =D1=81=D0=B5=D0=BD=D1=82. 2019 =D0=B3. =D0=B2 21:23, jj0= 8 <jj08@drivehq.com>: = To Janis or other members of the forum: = My sample table contains other columns as well. = One of the columns that I did not include in my first message is "= position". The last table has been modified as follows: = +--------+----------+------------+------------------------+ | usr_id | employer | position + start_date | end_date | +--------+----------+------------+------------------------+ | A | Goo | CTO | | 201904 | = | A | Micro | Mgr | 201704 | 201903 | = | B | Micro | Engg_Mgr | 201706 | - | | B | Goo | Researcher | 201012 | 201705 | | B | Micro | Postdoc | 201001 | 201011 | +--------+-= ---------+------------+------------------------+ = SELECT DISTINCT ON (usr_id) * FROM employment WHERE employer=3D'Micro' ORDER BY usr_id, start_date DESC; = -- Victor Yegorov = = ------------------------- Online Storage & Sharing, Online Backup, FTP / Email Server Hosting and= More. = Drive Headquarters. Top quality services designed for business! Sign u= p free at: www.DriveHQ.com. = = = ------------------------- Online Storage & Sharing, Online Backup, FTP / Email Server Hosting and= More. = Drive Headquarters. Top quality services designed for business! Sign u= p free at: www.DriveHQ.com . = ------=_NextPart_226199927_9736706_637035307798840385 Content-Type: text/html; charset="utf-8" Content-Transfer-Encoding: quoted-printable

v_kalees= :

Thank you for your suggestion.

I tried your code at http://www.sqlfiddle.com/#!17/4e386/4/1

but couldn't make it work. (I wanted to see two rows, but your query = returns only one row.)

My original question was to find people working for company "Mic= ro", so I took liberty to add "where employer=3D'Micro'" = condition to your solution.

May be I put the where clause at a wrong place?

Anyway, for me it is getting more complex :)

Victor's solution is producing 2 rows as I wanted, so for now a crisi= s has been averted!

 

From: v_kalees@yahoo.com
--To: vyegorov@gmail.com,jj08@drivehq.com
--CC: pgsql-sql@lists.postgresql.org
--Date: 9/7/2019 8:20:57 PM
--Subject: Re: RE:Re: RE:Re: A complex SQL query

 
This is much cleaner way
 
select * from (
select usr_id,
       employer,
       start_date,
       end_date,
       rank() OVER (PARTITION BY employer = ORDER BY start_date DESC) AS rnk
from ) t
where t.rnk=3D1;
 
Thanks
On Sunday, September 8, 2019, 7:40:05 AM GMT+5:30, jj08 wrote:
 
 

-------------------------
Online Storage & Sharing, Online Backup, FTP / Email Server Hosting and = More.
Drive Headquarters. Top quality services designed for business!
Sign up free at:
www.DriveHQ.com<= /a>.

 


-------------------------
Online Storage & Sharing, Online Backup, FTP / Email Server Hosting and = More.
Drive Headquarters. Top quality services designed for business!
Sign up free at:
www.DriveHQ.com. =
------=_NextPart_226199927_9736706_637035307798840385--