Skip to content
Source
Sign inSign up
TanStack/db

SQL STRING_AGG function in groupBy #422

Send to my agent
OpenUziniii(GitHub profile, opens in a new tab) opened this issue 1 comment
new Query().from({ brand: brandsCollection })
      .innerJoin({ productBrand: productBrandsCollection }, ({ productBrand, brand }) => eq(productBrand.brandId, brand.id))
      .groupBy(({ productBrand }) => productBrand.productId)
      .select(({ brand, productBrand }) => ({
        id: productBrand.productId,
        brands: stringAgg(brand.name, ", ", { orderBy: { val: productBrand.order, order: "asc" | "desc" }}),
      }))

result

[
  { id: 1, brands: "coca, fanta" },
]
  1. Or an arrayAgg that transform brands in ["coca", "fanta"] and that can supports number too ([1,2,3])

Sign in to join this conversation.