Parsware
All articles

Queries

Design a query

A saved query in Parsware is a question your solution owns. Build one in the query designer from a table, the tables joined to it, the columns it returns and the conditions that narrow it, and every save is checked against the environment's real tables before anything can use it.

The query designer in dark mode showing the Results tab of Contacts at large accounts: 21 contacts with their job titles and their accounts' names, cities and employee counts, next to the title "Design a query"

A view answers one question about one table: which contacts, in which order, with which columns. A saved query answers questions a view can't: contacts together with their account, filtered by something only the account knows. It is also shared. Once saved, the same query can feed a report, a script or a custom app, instead of each one keeping its own copy.

This article builds one in the query designer: contacts at accounts with 1,000 employees or more, with the account's name, city and size beside each person. We used the Account and Contact tables every environment starts with, and a solution of our own called Sales. Every picture is the real product.

Start a query

Open your solution, choose Saved Queries, then New. The designer fills the page. Give the query a display name and a description, then choose its table. Everything else is built from that table and the tables joined to it. We chose Contact:

The query designer for a new saved query named Contacts at large accounts, with the Simple tab showing the Contact table chosen

There are four tabs, all showing the same query:

  • Simple: a table, its columns, a filter, a sort and a row limit.
  • Advanced: all of that, plus related tables, grouping and options.
  • JSON: the query as a document.
  • Results: what the query returns.

Change something on one tab and the others show it, so they can't disagree. We work on Advanced, because we want a related table.

Join the account

Choose Add related table. The list comes from the relationships your environment already has, so you never type a field name. Each entry says which way the join goes:

  • one related record: following a lookup, here the contact's account. This is the usual join.
  • many related records, can repeat rows: the other way round, such as a contact's assets. The contact then comes back once per asset, which is how a total ends up counted twice. Use it when you mean it.

We chose Account, one related record. It gets a name, account, and a join setting. Keep rows with no match (the default) keeps a contact who has no account. Only matching rows drops them, silently, so keep the default unless you want that.

Choose the columns

The left pane lists every column the query can return: the contact's own, then the account's. Type to search it, and tick a column to add it to Returned columns on the right:

Related tables showing Account called account, joined company to id, keeping rows with no match; below, the column picker with Full Name ticked and Returned columns listing Contact Full Name, Contact Job Title, and the account's Account Name, City and Number of Employees

Order matters: it is the order the columns come back in, and the order a report prints them in. Drag a row by its handle to move it. Leave the right pane empty to return the table's own columns.

You don't need to know which table a column belongs to. Ticking the account's City files it under the join for you.

Narrow it

The Filter is the same And/Or builder a view uses (filters with And and Or), but it offers every column in the query, the joined table's as well. We asked for accounts with at least 1,000 employees, and sorted by the account's name:

The Filter with one condition, Account Number of Employees Is at least 1000, then Group and measure unticked, and Sort by Account Name ascending

That condition is the whole point of the join. A contact doesn't know how big its account is; only the account does.

Is at least and Is at most are there because "at least 1,000" written as "greater than 999" is only right for whole numbers. On a decimal or a date it is quietly wrong.

Save, then look at the answer

Save checks the query three ways: that it is a well-formed document, that it is a valid query, and that it would run here, with every table, join and column found in this environment. A query naming a field that doesn't exist is refused now, instead of failing later for whoever opens the report.

Preview runs the saved query and opens Results:

The Results tab: This runs the query as saved, with your own permissions, 21 matching records, and a table of contacts with Full Name, Job Title and the account's Account Name, City and Number of Employees, all 1050 or more

21 contacts, each with their account beside them, sorted by account. It runs with your own permissions and shows the query as saved, so save again after a change before you preview.

The query is a document

JSON shows what you built: the table, the columns, the join with its from and to, the filter. This is what a report or a script refers to, and what travels in the solution to Test and Production:

The JSON tab showing the query document: entity contact, select fullname and jobtitle, a link to account from company to id of kind Outer selecting name, address1_city and numberofemployees, and the start of the filter

You can edit it here too, for anything the form doesn't draw. It's checked the same way when you save.

The document, as text

This is the query from the JSON tab, written out in full. It is worth reading slowly once, because every saved query, every report's data and every runQuery in a script uses the same shape:

{
  "entity": "contact",
  "select": ["fullname", "jobtitle"],
  "links": [
    {
      "alias": "account",
      "entity": "account",
      "from": "company",
      "to": "id",
      "kind": "Outer",
      "select": ["name", "address1_city", "numberofemployees"]
    }
  ],
  "filter": {
    "connector": "And",
    "conditions": [
      { "field": "account.numberofemployees", "operator": "GreaterOrEqual", "value": 1000 }
    ]
  },
  "order": [{ "field": "account.name", "descending": false }]
}
  • entity is the table the rows come from: one row per contact.
  • links holds the joins. from is the field on the contact, its company lookup; to is the field on the account, its id. "kind": "Outer" is Keep rows with no match; "Inner" would be Only matching rows.
  • A joined field goes by its alias, account.numberofemployees, in the filter, in the order and in the results.
  • order sorts by the account's name, a column of the joined table.

There are two more things a query can say that this one doesn't: a top, the most rows to return, and an aggregate, which turns it into a question about totals.

A query, a view, or a script?

All three can answer which records?. Choosing between them is mostly about where the answer is needed.

You need Use Why
A list people work in, on one table A view It has a form behind every row, sorting, searching and commands
Columns from two tables side by side A saved query A view lists one table's fields
The same rows in a report, a dashboard and a script A saved query One definition, so the three never disagree
A one-off answer inside a rule runQuery in a script The question is part of the rule
A total or a count A query with an aggregate The database adds up, with no row limit

The shared row is the one that matters most over time. When a report and a dashboard each keep their own copy of which requests are waiting, they drift: one gets a new condition and the other doesn't, and two screens start giving two answers. A saved query is the one place that question is written down. Save a query and use it everywhere shows a report and a script sharing one.

Joins that count twice

The one join mistake worth learning to spot is many related records. Joining contacts to their account is safe: each contact has at most one account, so each contact still comes back once. Joining accounts to their contacts is different: an account with five contacts comes back five times. That is right if you want a list of contacts with their company, and wrong if you then add up the accounts' revenue, which is counted five times.

A rule of thumb: start from the table you want one row per of. One row per contact starts from Contact. One row per account, with how many contacts it has starts from Account and uses grouping, which Totals and groups covers.

Who sees what

A query runs as whoever runs it. Preview runs it as you. A report runs it as the person who opens the report, and a script as the person whose save it is part of. Every table in the query, the joined ones too, is limited to the records that person may read, so a report on contacts at large accounts shows each salesperson their own share and a sales director all of them, from the same query.

That is also why the Results tab says what it says: this runs the query as saved, with your own permissions. Twenty-one for you may be twelve for a colleague, and both are right.

Questions that come up

Can a query join more than one table? Yes. A link can have links of its own, so contacts can be joined to their account and the account to its owner. Each gets its own alias.

Can I filter on a joined table without returning its columns? Yes. Give the join no columns to select and filter on its alias. Contacts at accounts in London doesn't have to show the city.

Why is my query refused when I save it? Save checks that every table, join and column exists in this environment. A typo in the JSON tab, or a field that was renamed or deleted, is named in the message.

Does a saved query travel to Production? Yes, it is part of the solution, like the tables it reads and the reports that use it, so a report arrives in Production with the same question behind it.

Grouping, for another day

Tick Return groups instead of records and a query answers "how many" and "how much" instead of "which": counts and sums by group, which is what a chart or a counted tile is drawn from. That is a subject of its own.

Next: saving a query once and using it in a report and a script.