Thanks @Lee, I use the 'Distinct without SQL' to get a DISTINCT list of a field in my table, I couldn't workout how to get the count as well, can you let me know how to do that? I'm not sure what the bounds should be for the other way.
`export const getDistinctValues = query({
args: {
field: v.string(),
},
handler: async (ctx, args) => {
const distinctValues = new Set();
let indexToUse:
| "by_adviser_company"
| "by_adviser_nameWithoutTitle"
| "by_status";
if (args.field === "adviser.company") {
indexToUse = "by_adviser_company";
} else if (args.field === "adviser.nameWithoutTitle") {
indexToUse = "by_adviser_nameWithoutTitle";
} else {
indexToUse = "by_status";
}
let doc = await ctx.db
.query("referrals")
.withIndex(indexToUse)
.order("desc")
.first();
while (doc !== null) {
// Extract nested value
let value = doc as any;
if (args.field === "adviser.company") {
value = value?.adviser?.company;
} else if (args.field === "adviser.nameWithoutTitle") {
value = value?.adviser?.nameWithoutTitle;
} else {
value = value?.status;
}
if (value !== undefined && value !== null) {
distinctValues.add(value);
}
// Extract nested value like we did above
let fieldValue;
if (args.field === "adviser.company") {
fieldValue = doc.adviser?.company;
} else if (args.field === "adviser.nameWithoutTitle") {
fieldValue = doc.adviser?.nameWithoutTitle;
} else {
fieldValue = doc.status;
}
doc = await ctx.db
.query("referrals")
.withIndex(indexToUse, (q) =>
q.lt(args.field as any, fieldValue as any)
)
.order("desc")
.first();
}
return Array.from(distinctValues).sort();
},
});`