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 1i6FKj-0001hY-4n for pgsql-sql@arkaria.postgresql.org; Fri, 06 Sep 2019 14:35:29 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1i6FKi-000741-0G for pgsql-sql@arkaria.postgresql.org; Fri, 06 Sep 2019 14:35:28 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1i6FKh-0006o3-HM for pgsql-sql@lists.postgresql.org; Fri, 06 Sep 2019 14:35:27 +0000 Received: from mail02.mbizgroup.biz ([66.220.9.54] helo=mail.drivehq.com) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.92) (envelope-from ) id 1i6FKb-0004yS-0i for pgsql-sql@lists.postgresql.org; Fri, 06 Sep 2019 14:35:26 +0000 Received: from (192.168.10.210) by EM244o80 with SMTP User ID0; Fri, 06 Sep 2019 14:35:17 GMT Received: from 60.111.238.211 (60.111.238.211) by mail.drivehq.com with HTTPUserID9736706; Fri, 06 Sep 2019 14:35:17 GMT X-Priority: 3 Message-ID: <2259433721000000009736706@www> From: "jj08" To: jj08@drivehq.com, pgsql-sql@lists.postgresql.org, janis@puris.lv Subject: RE:Re: A complex SQL query Date: Fri, 06 Sep 2019 07:35:17 -0700 MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_NextPart_225943372_9736706_637033521174417679" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk This is a multi-part message in MIME format. ------=_NextPart_225943372_9736706_637033521174417679 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Yes, that's exactly what works for me. Thanks a bunch!!! = = --From: janis@puris.lv --To: pgsql-sql@lists.postgresql.org,jj08@drivehq.com --Date: 9/6/2019 5:56:10 AM --Subject: Re: A complex SQL query Would s= omething like this work for you ? http://www.sqlfiddle.com/#!17/2e45eb/9= select user_id, max(start_date) as start_date, case when= max(end_date) < max(start_date) then null else max(end_date) end as end= _date from employment where employer =3D 'Micro' group by = user_id ; On 6 Sep 2019, 14:31 +0200, jj08 , wrote: = I hope someone can give me some pointers. Here is my table. +--------+----------+------------+-----------+ | usr_id | employer | start_date | end_date | +--------+----------+------------+-----------+ | A | Goo | 201904 | - | | A | Micro | 201704 | 201903 | = | B | Micro | 201706 | - | | B | Goo | 201012 | 201705 | | B | Micro | 201001 | 201011 | +--------+----------+------------+-----------+ = I am trying to list up people working for a company called "Micro&= quot;. Some people work for a company multiple times, like user B. I only need one line per user, displaying only the latest affiliation d= ate. = For user_id "B", I could do Select user_id, MAX(start_date), = end_date where employer =3D 'Micro', but that would fail to get record for user_id "A". = If multiple records exist, I want to do MAX, but if only a single recor= d exists, I don't need MAX. How do I do that? = From the above data, I would like to see only two lines: | A | Micro | 201704 | 201903 | | B | Micro | 201706 | - | Thank you. = = ------------------------- 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 . = = ------------------------- 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_225943372_9736706_637033521174417679 Content-Type: text/html; charset="utf-8" Content-Transfer-Encoding: quoted-printable

Yes, tha= t's exactly what works for me.

Thanks a bunch!!!

 

--From: janis@puris.lv
--To: pgsql-sql@lists.postgresql.org,jj08@drivehq.com
--Date: 9/6/2019 5:56:10 AM
--Subject: Re: A complex SQL query

Would something like this work for you ? http://www.sqlfiddle.com/#!17/2= e45eb/9
 
select
    user_id,
    max(start_date) as start_date,
    case when max(end_date) < max(start_date= ) then null else max(end_date) end as end_date
from
    employment
where
    employer =3D 'Micro'
group by
    user_id
;
On 6 Sep 2019, 14:31 +0200, jj08 , wrote:

 I hope someone can give me some pointers.

Here is my table.

+--------+----------+------------+-----------+

| usr_id | employer | start_date | end_date  |

+--------+----------+------------+-----------+

| A      | Goo      | 201904  &nbs= p;  | -         |

| A      | Micro    | 201704    &n= bsp;| 201903    |

 

| B      | Micro    | 201706    &n= bsp;| -         |

| B      | Goo      | 201012  &nbs= p;  | 201705    |

| B      | Micro    | 201001    &n= bsp;| 201011    |

+--------+----------+------------+-----------+

 

I am trying to list up people working for a company called "Micr= o".

Some people work for a company multiple times, like user B.

I only need one line per user, displaying only the latest affiliation= date.

 

For user_id "B", I could do Select user_id, MAX(start_date)= , end_date where employer =3D 'Micro',

but that would fail to get record for user_id "A".

 

If multiple records exist, I want to do MAX, but if only a single rec= ord exists, I don't need MAX.

How do I do that? 

From the above data, I would like to see only two lines:

| A      | Micro    | 201704    &n= bsp;| 201903    |

| B      | Micro    | 201706    &n= bsp;| -         |

Thank you.


-------------------------
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.

-------------------------
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_225943372_9736706_637033521174417679--