Skip to content
Exit presentation

Data Handling II: Databases & Structured Data

Counting the groups in the answer

A report needs two steps, not one. First ask the query. Then count the kept rows by group. A group here is a section, such as 9-A. A tally is one count for each group.

Three groups: 9-A, 9-B, 10-C.
Step 1 of 4
  1. Three groups: 9-A, 9-B, 10-C.
  2. 9-A grows to 4 kept rows.
  3. 9-B grows to 4 kept rows.
  4. 10-C stays 0 — a real, honest count.

The rule

Count the answer, not the table. This report lists its groups first. So a group with no answer rows still shows a 0.

Here sectionAt is the position of the section column in a row. at is a variable name, not the English word. It holds the position of the row's section in groups.

  1. const groups = ['9-A', '9-B', '10-C'];
  2. const tally = [0, 0, 0];
  3. for (const row of answer) {
  4. const at = groups.indexOf(row[sectionAt]);
  5. tally[at] = tally[at] + 1;
  6. }

Line 2 starts every count at 0. Line 5 adds 1 to the count of the row's group.

The query "due at least 1000" keeps 8 of the 12 records. Counted per section, that gives 9-A four, 9-B four, and 10-C zero.

A list built from kept rows alone would leave out 10-C.

The lab has four cards: open, ask, count, show. Try them in different orders.

Loading lab…

Ask what list a report counted, before you trust its numbers.

Jump to lesson

This track

  1. 0 Start here
  2. 1 One record, one row
  3. 2 The naming value
  4. 3 Naming the columns
  5. 4 Asking the register one question
  6. 5 Two conditions, both true
  7. 6 The answer, in order
  8. 7 Counting the groups in the answer
  9. 8 What a database adds
  10. 9 The big picture
  11. 10 Track quiz

Other tracks