Parsware
All articles

Queries

Totals and groups

A Parsware query can answer "how many" and "how much" as well as "which". Group records by a column, count and sum them, keep only the groups that pass a test, and take the top five, all in the query designer, with the database doing the arithmetic.

The query designer in dark mode showing five groups, Tehran, Berlin, Chicago, Phoenix and Dallas, with how many accounts each city has and how many people they employ, next to the title "Totals and groups"

Most queries answer which records: these contacts, those invoices. A chart or a counted tile needs a different answer: how many, and how much. In Parsware the same query designer asks both. You tick one box, and the query returns groups with totals instead of records.

This article builds Accounts by city: how many accounts each city has and how many people they employ, for cities with at least seven accounts, the five biggest workforces first. It uses the Account table every environment starts with, in a solution of our own called Sales. Every picture is the real product.

Groups instead of records

Open your solution, choose Saved Queries, then New. Name the query, choose the Account table, and go to the Advanced tab. Under Group and measure, tick Return groups instead of records. Three lists appear:

Group and measure with Return groups instead of records ticked: two measures, Count of Records called accounts and Sum of Number of Employees called employees; Group by City; and a condition on the measures, accounts Is at least 7

  • Measures say what to work out. We added two: Count of records, called accounts, and Sum of Number of Employees, called employees. The functions are Count, Sum, Average, Minimum and Maximum. Sum and Average offer only number columns.
  • Group by says what to split them by. We chose City. You can use up to two keys, and a date column also offers a period: day, week, month, quarter or year.
  • Conditions on the measures say which groups come back. We asked for cities with at least 7 accounts.

The name you give a measure matters. It's the name the result comes back under, and the name a chart or a script reads.

Leave Group by empty and the query returns one group over the whole table: a single total, which is what a counted tile shows.

A filter or a condition on a measure?

They look alike, but they answer different questions.

  • The filter decides which records go into a group. "Accounts in the United States" is a filter: each account knows its own country.
  • A condition on a measure decides which groups come out. "Cities with at least seven accounts" can't be a filter, because no single account knows how many accounts share its city. Only the group does.

You can use both in one query: filter the records first, then keep only the groups you want.

Sort, and the top five

A grouped query has no ordinary columns to sort by: its rows are groups. So Sort offers the grouping key and the measures. We sorted by employees, descending, and set Return at most to 5:

Sort by employees, Descending, and Return at most 5, above the Options: Remove duplicate rows, Include the text of lookup columns, and Time zone

That is the usual "top five" question: sort by a measure, biggest first, and cap it.

Save, then look at the answer

Save checks that every field, grouping and measure exists in this environment. Preview runs it:

The Results tab: 5 groups, with columns City, accounts and employees: Tehran 11 and 8150, Berlin 9 and 7275, Chicago 10 and 6200, Phoenix 9 and 5525, Dallas 7 and 5325

Five cities, with their counts and totals, biggest workforce first. Chicago has more accounts than Berlin but fewer people, so it comes third. The database did the counting and adding. The query never fetched a single account to do it, so it gives the same answer for fifty accounts or fifty thousand.

While writing this we found that this table headed the city column address1_city, the column's internal name, even though record queries already showed display names. It now reads City.

The query is a document

The JSON tab shows what you built. The two measures, the grouping and the condition are all under aggregate, and the condition on the measures is called having, as in SQL:

The JSON tab: entity account, and an aggregate with measures accounts, a Count, and employees, a Sum of numberofemployees; groupBy on address1_city; and the start of having

A report, a script or a custom app that names this query gets these five groups. Each group has the key and the measures by name, such as address1_city, accounts and employees.

The aggregate, as text

Written out in full, Accounts by city is this document:

{
  "entity": "account",
  "aggregate": {
    "measures": [
      { "alias": "accounts", "function": "Count" },
      { "alias": "employees", "function": "Sum", "field": "numberofemployees" }
    ],
    "groupBy": [{ "field": "address1_city" }],
    "having": {
      "connector": "And",
      "conditions": [{ "measure": "accounts", "operator": "GreaterOrEqual", "value": 7 }]
    }
  },
  "order": [{ "field": "employees", "descending": true }],
  "top": 5
}

Each measure has an alias, the name its result comes back under, a function, and for everything except Count the field it works on. groupBy lists the keys. having is the condition on the measures, with the same connectors and operators as a filter, but naming a measure instead of a field. And the order sorts by a measure's alias, which is how biggest first is said.

Grouping by a date

A date column can be grouped by a period: day, week, month, quarter or year. That is what turns a table of claims into a line chart of spending per month:

{
  "entity": "par_expense",
  "aggregate": {
    "measures": [{ "alias": "spent", "function": "Sum", "field": "par_amount" }],
    "groupBy": [{ "field": "created_at_utc", "bucket": "Month" }]
  },
  "order": [{ "field": "created_at_utc", "descending": false }]
}

One group per month that has claims, in date order. A month with no claims has nothing to group, so it isn't in the result; a chart drawn from it simply has no point for that month. Add a second key, such as the status, and each month is split again, which is what a stacked bar chart of spent per month, by status is drawn from.

Questions people ask, as queries

Most "how many" and "how much" questions come in a few shapes:

Question Measures Group by Condition
How many open cases are there? Count none (filter: State is Active)
How much is each customer owed? Sum of amount Customer (filter: status is Unpaid)
What is the average order, by region? Average of total Region
Which suppliers have more than ten late deliveries? Count Supplier Count at least 11
What did we spend each month this year? Sum of amount Created On, by month (filter: Created On at least 1 January)
Who has the oldest open ticket? Minimum of created on Owner

The first is a single number for a tile. The rest are rows for a chart, a report table or a script.

Reading the result in a script

A grouped query comes back as rows, one per group, each holding the keys and the measures by name. A server rule can use one directly. This one refuses a new expense claim that would take a person's claims for the month over a limit:

var rows = runQuery({
  entity: 'par_expense',
  filter: {
    connector: 'And',
    conditions: [
      { field: 'created_by', operator: 'Equals', value: userId() },
      { field: 'created_at_utc', operator: 'GreaterOrEqual', value: '2026-11-01' }
    ]
  },
  aggregate: { measures: [{ alias: 'spent', function: 'Sum', field: 'par_amount' }] }
});

var spent = 0.0;
if (!isEmpty(rows[0].spent)) {
  spent = rows[0].spent;
}
if (spent + getValue('par_amount') > 2000) {
  throwValidationError('That would take your claims this month over 2,000.');
}

With no groupBy the query always answers one row, so rows[0] is safe; and the isEmpty check handles the person's first claim of the month, whose sum is empty rather than zero. In a real rule the month's first day would be worked out from today's date rather than written into the script. Query records from a script covers runQuery in full.

Where a grouped query is used

  • Report tables and charts, which repeat over the groups. Groups and totals in the reports series groups inside the report itself, which suits printed subtotals.
  • Dashboard tiles and charts, drawn on a Studio screen. A dashboard on a screen shows one.
  • Scripts, as above, where the answer decides whether a save goes through.

Wherever it is used, it runs as whoever is asking, so each total counts only the records that person may read.

Two things that catch people out

A sum over nothing is empty, not zero. Group by nothing and filter to a customer with no invoices, and you still get one group. Its Count is 0, but its Sum is empty, exactly as in SQL. A script has to check for that before it adds the total to anything. We found a sample plugin step that didn't, which is in the previous article.

A join can double a total. If you join a table the "many" way, such as an account's contacts, each account comes back once per contact. Sum the account's employees after that join and every account is counted as many times as it has contacts. Remove duplicate rows doesn't fix this, because a grouped query still counts the repeated rows. Join the "one related record" way when you total, or group by the table you're totalling.

Next: BS Lang, the one scripting language every script in Parsware is written in.