This question follows [this one](https://stackoverflow.com/questions/47291359/several-case-whens-with-output-on-same-row).

I now have this code:

```
select
    min(purchaseDate) filter (where fruitType = 'apple') as appleFirstPurchaseDate,
    min(purchaseDate) filter (where fruitType = 'orange') as orangeFirstPurchaseDate,
    customer
from fruitPurchases
group by customer

```

Which gives the following output:

```
appleFirstPurchaseDate  orangeFirstPurchaseDate
----------------------  -----------------------
       2017-05-03              2016-11-25

```

You should also know that each piece of fruit has an ID. With this ID, I'd like to create two keys: one that concatenates the fruit ID with the `appleFirstPurchaseDate` and the other one that concatenates the fruit ID with the `orangeFirstPurchaseDate`

Therefore I tried this:

```
select
    fruitId || '-' || min(purchaseDate) filter (where fruitType = 'apple') as appleKey,
    min(purchaseDate) filter (where fruitType = 'apple') as apple,
    fruitId || '-' || min(purchaseDate) filter (where fruitType = 'orange') as orangeKey,
    min(purchaseDate) filter (where fruitType = 'orange') as orange,
    customer
from fruitPurchases
group by customer, fruitId

```

But when a customer has already bought apples as well as oranges, it unfortunately gives me this:

```
  appleKey        appleFirstPurchaseDate    orangeKey    orangeFirstPurchaseDate
  --------        ----------------------    ---------    -----------------------
283-2017-05-03          2017-05-03           [NULL]             [NULL]
   [NULL]                 [NULL]          322-2016-11-25      2016-11-25

```

While I would like this:

```
  appleKey        appleFirstPurchaseDate    orangeKey    orangeFirstPurchaseDate
  --------        ----------------------    ---------    -----------------------
283-2017-05-03          2017-05-03       322-2016-11-25        2016-11-25

```

Last piece of information: an earlier `FirstPurchaseDate` does not imply a lower `fruitID`.