Skip to main content

groupBy()

Use this when you want to group data.

Available Keys

Key NameDescriptionOptionalNotes
whereSpecify retrieval conditionsYesIf omitted, all rows are retrieved
orderBySort settingsYesIf specifying only one column, the array can be omitted
takeSet the number of records to retrieveYes
skipSet the number of records to skipYes
_avgAverage display settingsYes
_countHit count display settingsYes
_maxMaximum value display settingsYes
_minMinimum value display settingsYes
_sumSum display settingsYes
bySpecify grouping conditionsNo
havingSpecify conditions after groupingYesIf omitted, all data is retrieved
tip

In where, you can also use relation filters (some / every / none / is / isNot).

Example Sheet

Example Sheet

Explanation

Suppose you want to perform the following operation from the example above.

  • Group by pref

The code would be as follows.

// gassma.{{TARGET_SHEET_NAME}}.groupBy
const result = gassma.sheet1.groupBy({
by: "pref",
});

The return value is in the following format.

[
{ pref: "Ibaraki" },
{ pref: "Tokyo" },
{ pref: "Osaka" },
{ pref: "Aichi" },
{ pref: "Shiga" },
{ pref: "Kyoto" },
{ pref: "Tottori" },
{ pref: "Fukuoka" },
];

You can also specify multiple fields. Suppose you want to perform the following operations.

  • Group by pref
  • Additionally group by age

The code would be as follows.

// gassma.{{TARGET_SHEET_NAME}}.groupBy
const result = gassma.sheet1.groupBy({
by: ["pref", "age"],
});

The return value is in the following format.

[
{ pref: "Ibaraki", age: 22 },
{ pref: "Tokyo", age: 31 },
{ pref: "Tokyo", age: 55 },
{ pref: "Osaka", age: 20 },
{ pref: "Aichi", age: 40 },
{ pref: "Shiga", age: 25 },
{ pref: "Kyoto", age: 45 },
{ pref: "Tottori", age: 29 },
{ pref: "Fukuoka", age: 33 },
];

having

Use this when you want to extract data that meets specific conditions from grouped data.

For example, suppose you want to extract data with the following conditions.

  • Group by pref
  • (After grouping) age => average is 30 or less

The code would be as follows.

// gassma.{{TARGET_SHEET_NAME}}.groupBy
const result = gassma.sheet1.groupBy({
by: ["pref"],
having: {
age: {
_avg: {
lte: 30,
},
},
},
});

The return value is in the following format.

[
{ pref: "Ibaraki" },
{ pref: "Osaka" },
{ pref: "Shiga" },
{ pref: "Tottori" },
];

AND, OR, NOT in having

You can also use AND, OR, and NOT.

For example, suppose you want to perform the following operation.

  • Group by pref
  • (After grouping) age => average is NOT 30 or less

The code would be as follows.

// gassma.{{TARGET_SHEET_NAME}}.groupBy
const result = gassma.sheet1.groupBy({
by: ["pref"],
having: {
NOT: {
age: {
_avg: {
lte: 30,
},
},
},
},
});

The return value would be as follows.

[{ pref: "Tokyo" }, { pref: "Aichi" }, { pref: "Kyoto" }, { pref: "Fukuoka" }];

Also, just like where, you can nest AND inside NOT and create other nested combinations.

Displaying Statistics

You can also display statistics such as averages, just like with aggregate.

For example, suppose you want to perform the following operations.

  • Group by pref
  • Display the average of age

The code would be as follows.

// gassma.{{TARGET_SHEET_NAME}}.groupBy
const result = gassma.sheet1.groupBy({
by: ["pref"],
_avg: { age: true },
});

The return value is in the following format.

[
{ pref: "Ibaraki", _avg: { age: 22 } },
{ pref: "Tokyo", _avg: { age: 43 } },
{ pref: "Osaka", _avg: { age: 20 } },
{ pref: "Aichi", _avg: { age: 40 } },
{ pref: "Shiga", _avg: { age: 25 } },
{ pref: "Kyoto", _avg: { age: 45 } },
{ pref: "Tottori", _avg: { age: 29 } },
{ pref: "Fukuoka", _avg: { age: 33 } },
];