# Filtered Distinct Values API

> Retrieve unique values based on query filters for refined data extraction.

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

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

## Structure

**Distinct Query**

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

## Simple get distinctWithQuery

**Simple distinctWithQuery**

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

## distinctWithQuery with multiple distinctField
```typescript
let distinctQuery = await g.sys.db.gen.distinctQueryGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    distinctField: "first_name,last_name"
});
```

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

## distinctWithQuery with order
### ascending

**distinctWithQuery with ascending order**

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

**distinctWithQuery with descending order**

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

## distinctWithQuery with headers and order

**distinctWithQuery with headers**

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

## distinctWithQuery with find
```typescript
let distinctQuery = await g.sys.db.gen.distinctQueryGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    distinctField: "first_name",
    find: {
        first_name: "Bob"
    }
});
```

## $lt distinctWithQuery

**distinctWithQuery with $lt**

```typescript
let distinctQuery = await g.sys.db.gen.distinctQueryGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    distinctField: "first_name",
    find: {
        customer_id: {
            "$lt": 2
        }
    }
});
```

## $lte distinctWithQuery

**distinctWithQuery with $lte**

```typescript
let distinctQuery = await g.sys.db.gen.distinctQueryGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    distinctField: "first_name",
    find: {
        customer_id: {
            "$lte": 2
        }
    }
});
```

## $gt distinctWithQuery

**distinctWithQuery with $gt**

```typescript
let distinctQuery = await g.sys.db.gen.distinctQueryGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    distinctField: "first_name",
    find: {
        customer_id: {
            "$gt": 2
        }
    }
});
```

## $gte distinctWithQuery

**distinctWithQuery with $gte**

```typescript
let distinctQuery = await g.sys.db.gen.distinctQueryGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    distinctField: "first_name",
    find: {
        customer_id: {
            "$gte": 2
        }
    }
});
```

## $eq distinctWithQuery

**distinctWithQuery with $eq**

```typescript
let distinctQuery = await g.sys.db.gen.distinctQueryGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    distinctField: "first_name",
    find: {
        customer_id: {
            "$eq": 2
        }
    }
});
```

## $ne distinctWithQuery

**distinctWithQuery with $ne**

```typescript
let distinctQuery = await g.sys.db.gen.distinctQueryGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    distinctField: "first_name",
    find: {
        customer_id: {
            "$ne": 2
        }
    }
});
```

## $not distinctWithQuery

**distinctWithQuery with $not**

```typescript
let distinctQuery = await g.sys.db.gen.distinctQueryGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    distinctField: "first_name",
    find: {
        customer_id: {
            "$not": {
                "$in": [1,2,3]
            }
        }
    }
});
```

## $and distinctWithQuery

**distinctWithQuery with $and**

```typescript
let distinctQuery = await g.sys.db.gen.distinctQueryGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    distinctField: "first_name",
    find: {
        "$and": [
            {
                "customer_id": 1
            },
            {
                "pincode": 382345
            }
        ]
    }
});
```

## $or distinctWithQuery

**distinctWithQuery with $or**

```typescript
let distinctQuery = await g.sys.db.gen.distinctQueryGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    distinctField: "first_name",
    find: {
        "$or": [
            {
                "customer_id": 1
            },
            {
                "pincode": 382345
            }
        ]
    }
});
```

## $in distinctWithQuery

**distinctWithQuery with $in**

```typescript
let distinctQuery = await g.sys.db.gen.distinctQueryGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    distinctField: "first_name",
    find: {
        customer_id: {
            "$in": [1, 2,3]
        }
    }
});
```

## $nin distinctWithQuery

**distinctWithQuery with $nin**

```typescript
let distinctQuery = await g.sys.db.gen.distinctQueryGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    distinctField: "first_name",
    find: {
        customer_id: {
            "$nin": [1, 2,3]
        }
    }
});
```

## $like distinctWithQuery

**distinctWithQuery with $like**

```typescript
let distinctQuery = await g.sys.db.gen.distinctQueryGen({
    instance: "mysql_8",
    database: "inventory",
    collection: "customers",
    distinctField: "first_name",
    find: {
        first_name: {
            "$like": "Bob%"
        }
    }
});
```

## 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 some customers of the tenant acme**

```typescript
let distinctQuery = await g.sys.db.gen.distinctQueryGen({
    instance: "crm::acme", database: "crm", collection: "customers",
    distinctField: "first_name",
    find: {
        "last_name": "Lin"
    }
});
```

## Related

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