# SELECT DISTINCT in Smart Segment

**URL:** <https://community.forestadmin.com/t/select-distinct-in-smart-segment/4518>\
**Category:** Help me!\
**Created:** [April 20, 2022, 2:39pm UTC](https://community.forestadmin.com/t/select-distinct-in-smart-segment/4518 "2022-04-20T14:39:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Simon\_BRAMI1](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/simon_brami1/32/2753_2.png) [@Simon\_BRAMI1](https://community.forestadmin.com/u/Simon_BRAMI1)\
**Post date:** [April 20, 2022, 2:39pm UTC](https://community.forestadmin.com/t/select-distinct-in-smart-segment/4518/1 "2022-04-20T14:39:09Z")

</div>

Hello,

Is it possible to create a segment that will return only the values from a SELECT DISTINCT query ?

It keeps returning all the values…

I’m running with Postgres.

Thank you

---

<div class="post-metadata">

**Author:** ![Arnaud\_Moncel](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/arnaud_moncel/32/24_2.png) [@Arnaud\_Moncel](https://community.forestadmin.com/u/Arnaud_Moncel)\
**Post date:** [April 21, 2022, 8:03am UTC](https://community.forestadmin.com/t/select-distinct-in-smart-segment/4518/2 "2022-04-21T08:03:53Z")

</div>

Hi @Simon_BRAMI1 👋 according to [the documentation](https://docs.forestadmin.com/documentation/reference-guide/smart-segments) you can make a request like below inside the `where` property.

```javascript
const { amodel } = require('../models');
where: (record) => {
  return amodel.findAll({
    attributes: ['id'],
    distinct: true,
  }).then(records => {
    const ids = records.map(({ id }) => id);
    return { id: { [Op.in]: ids } };
  });
}

```

Let me know 🙏

---

<div class="post-metadata">

**Author:** ![Simon\_BRAMI1](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/simon_brami1/32/2753_2.png) [@Simon\_BRAMI1](https://community.forestadmin.com/u/Simon_BRAMI1)\
**Post date:** [April 21, 2022, 9:27am UTC](https://community.forestadmin.com/t/select-distinct-in-smart-segment/4518/3 "2022-04-21T09:27:51Z")

</div>

> [@Arnaud\_Moncel](#):
>
> ```javascript
> return amodel.findAll({
> attributes: ['id'],
> distinct: true,
> }).then(records => {
> const ids = records.map(({ id }) => id);
> return { id: { [Op.in]: ids } };
> });
> 
> ```

This could probably work if I had only one PK but unfortunately I have an other PK…

I have `'id'` and `'hash'` that makes every row unique, but I want to Select Distinct based on `'hash'`.

So I tried this:

```javascript
return models.table.findAll({
  attributes: [
    [Sequelize.fn('DISTINCT', Sequelize.col('hash')) ,'hash']
  ],
  distinct: true,
}).then(records => {
  const hashes = records.map(({ hash }) => hash);
  return { hash: { [Op.in]: hashes } };
});

```

But it will return all the rows that contains the hashes (which are not unique) instead of returning only one of them 😔

---

<div class="post-metadata">

**Author:** ![Simon\_BRAMI1](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/simon_brami1/32/2753_2.png) [@Simon\_BRAMI1](https://community.forestadmin.com/u/Simon_BRAMI1)\
**Post date:** [April 21, 2022, 10:04am UTC](https://community.forestadmin.com/t/select-distinct-in-smart-segment/4518/4 "2022-04-21T10:04:43Z")

</div>

Just found a way to do it 🚀

```javascript
let res = await models.connections.default.query(`
  select "id", "hash"
  from (
    select *,
            ROW_NUMBER() over (partition by "hash" order by "created_at" desc) as row
    from table) as rows

  where rows.row = 1
`, { type: QueryTypes.SELECT })

var condition = res.map(val => {
  return {
    [Op.and]: [
      { hash: val.hash },
      { id: val.id }
    ]
  }
})
  
return { [Op.or]: condition}

```
