# Distinct Values Retrieval API

> Fetch unique field values easily—optimize filters and UI dropdowns with distinct data.

Source: https://docs.apimaker.dev/v1/examples/sys/db/gen/distinctGen.html

`g.sys.db.gen.distinctGen` calls the [distinct generated API](https://docs.apimaker.dev/v1/docs/apis-all/generated-apis/auto-generated-distinct-api.html) from your code : no schema, the data as it is in the table.

## Structure

**Distinct method**

```typescript
let distinct = await g.sys.db.gen.distinctGen({
    instance: "INSTANCE_NAME", database: 'DB_NAME', collection: "COLLECTION_NAME",
    distinctField: "COLUMN_NAME",
    order: "asc"
});
```

## Simple distinct
```typescript
let distinct = await g.sys.db.gen.distinctGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    distinctField: "first_name"
});
```

## distinct with header
```typescript
let distinct = await g.sys.db.gen.distinctGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    distinctField: "first_name",
    headers: {
        "x-am-response-case": "capitalCase"
    }
});
```

## distinct with order
### ascending

**distinct with order**

```typescript
let distinct = await g.sys.db.gen.distinctGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    distinctField: "first_name",
    order: "asc"
});
```
### descending

**distinct with header**

```typescript
let distinct = await g.sys.db.gen.distinctGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    distinctField: "first_name",
    order: "desc"
});
```

## distinct with headers and order

**distinct with header & order**

```typescript
let distinct = await g.sys.db.gen.distinctGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    distinctField: "first_name",
    headers: {
        "x-am-response-case": "capitalCase"
    },
    order: "asc"
});
```

## Multi-tenant
- `instance: "crm::acme"` works on the database of the tenant `acme` of the [multi-tenant](https://docs.apimaker.dev/v1/docs/features/multi-tenant.html) instance `crm`.
- With `instance: "crm"`, the call works on the database of the tenant of the request: a custom API called with the [x-am-tenant-username](https://docs.apimaker.dev/v1/docs/apis-all/header/requestHeader.html#x-am-tenant-username) header passes it on to the APIs it calls. A request without a tenant works on the structure database.

**Distinct first names of the customers of the tenant acme**

```typescript
let distinct = await g.sys.db.gen.distinctGen({
    instance: "crm::acme", database: "crm", collection: "customers",
    distinctField: "first_name"
});
```

## Related

- [Distinct generated API](https://docs.apimaker.dev/v1/docs/apis-all/generated-apis/auto-generated-distinct-api.html) · [Schema API from code](https://docs.apimaker.dev/v1/examples/sys/db/distinct.html) · [The global object g](https://docs.apimaker.dev/v1/docs/pre-defined-terms/global-object-g.html)
