Information Technology Topic
Sorting and Grouping Records
- Read notes
- Flashcards
- Quiz
- Confident
Study focus - 3 things
- Sorting puts records in order by one or more fields: numbers by value, text one character at a time from the left, and a number typed into a text field sorts as text
- Grouping gathers the records that share a value in a field into one group, and count, sum and average then give one result for each group
- Count says how many records, sum adds the values, and average is the sum divided by the count, so the group with the biggest sum does not always have the biggest average
Section 1Sorting records in a database
A database table is a set of records (rows) made of fields (columns). Sorting puts the records into an order that you choose, using the values in one or more fields. The data itself does not change. Only the order in which you see the records changes. Ascending order goes from the smallest to the largest number, or from A to Z. Descending order is the opposite. Here's the trick: never assume that records come back in the order you typed them. A database gives you no guaranteed order unless you ask for one, so when the order matters, set a sort. You can sort the table itself, the output of a query, or the records in a report. A quick sort works on one column. Several columns need the advanced sort, where you choose the fields one after the other.
Section 2Numeric, alphabetic and alphanumeric sorts
A numeric sort compares the values as numbers. 8 comes before 9, and 9 comes before 10. An alphabetic sort compares text one character at a time, starting from the left (the finer rules for capital letters, spaces and symbols depend on the program, so the examples here use plain capitalised words and codes). Allen, Bailey, Baker, Brown, Walker is in order because A comes before B, and in Bailey and Baker the third letters i and k decide. An alphanumeric sort works the same way on fields that mix letters and digits, such as product codes. Each digit is treated as a character, not as part of a number. So P101 comes before P12, which comes before P25, which comes before P3 and P7. After the P, P101 and P12 both have a 1, so the sort moves on and compares 0 with 2. The 0 wins, so P101 comes before P12. Watch out: a number typed into a text (alphanumeric) field sorts as text. The values 9, 10, 100, 25 and 8 come out as 10, 100, 25, 8, 9. Store quantities, prices and marks in a numeric field and they sort by value: 8, 9, 10, 25, 100.
Section 3Sorting on more than one field
Several records can share a value, such as three sales made in Kingston. The primary sort field decides the main order. The secondary sort field only puts records in order when they tie on the primary field. Each field has its own direction. Take the sales table (SaleID, Branch, Category, Amount in JMD): 1 Kingston Bags 4500; 2 Mandeville Jewellery 2800; 3 Montego Bay Bags 5200; 4 Kingston Jewellery 3100; 5 Kingston Bags 3900; 6 Mandeville Bags 4100; 7 Montego Bay Jewellery 2600; 8 Mandeville Jewellery 3300; 9 Montego Bay Bags 4800; 10 Kingston Jewellery 2900; 11 Montego Bay Jewellery 3400; 12 Kingston Bags 3600. Sort by Branch ascending, then Amount descending. Kingston comes first, and its sales go from the biggest amount to the smallest: sales 1, 5, 12, 4, 10. Mandeville follows with 6, 8, 2, and Montego Bay ends with 3, 9, 11, 7. Easy to mix up: Branch then Amount is not the same as Amount then Branch. The first gives branches in blocks. The second gives one list from biggest to smallest amount, with branches mixed.
Section 4Grouping records
Grouping collects the records that share the same value in a chosen field into one group. Grouping the sales table by Branch gives three groups, because there are three different branches: Kingston, Mandeville and Montego Bay. Grouping by Category gives two groups: Bags and Jewellery. The number of groups is the number of different values in the group field, not the number of records. Twelve sales still make only three branch groups. In a report, each group can have its own header, which names the group, and its own footer, which holds the group's results. Sort the report by the group field as well, so that the records of each group sit together. You can group inside a group. Grouping by Branch and then by Category gives six small groups, such as Kingston Bags and Kingston Jewellery.
Section 5Count, sum and average
Three summary features turn a group of records into single figures. Count says how many records are in the group. Sum adds up the values of a numeric field, such as Amount. Average is the sum divided by the count. For the Kingston group: 5 records (sales 1, 4, 5, 10 and 12), a sum of 4500 + 3100 + 3900 + 2900 + 3600 = 18000, and an average of 18000 ÷ 5 = 3600. Sum and average need a numeric field. Counting works on any field, because it just counts records (some programs skip empty cells when you count a field, so count a field that is always filled in, such as the ID). When the average does not divide evenly, round it. The Jewellery group has a sum of 18100 and a count of 6, so the average is 3016.666... which is 3016.67 to 2 decimal places. Watch out: a SaleID is a number, but adding up SaleIDs means nothing. Choose the field that holds a real amount.
Section 6Reading the results for each group
Grouping the sales by Branch with count, sum and average gives: Kingston: count 5, sum 18000, average 3600. Mandeville: count 3, sum 10200, average 3400. Montego Bay: count 4, sum 16000, average 4000. Across the whole table the count is 12, the sum is 44200 and the average is 44200 ÷ 12 = 3683.33. Here's the trick: the biggest sum is not always the biggest average. Kingston has the largest sum because it has the most sales, but Montego Bay has the highest average per sale. A quick check: the group counts must add up to the number of records (5 + 3 + 4 = 12) and the group sums must add up to the overall sum (18000 + 10200 + 16000 = 44200).
Section 7Putting it together for a report
A report for a manager usually needs all of these ideas at once. A sensible order of work is: 1. Decide which fields appear, for example Branch, SaleID, Category and Amount. 2. Sort: Branch ascending, then Amount descending, so the biggest sale in each branch is at the top of its block. 3. Group by Branch, so each branch has its own header and footer. 4. Add the summaries to the group footer: count, sum and average of Amount. 5. Add the overall results at the end of the report. Some report tools offer sum, minimum and maximum directly but not average, so the average may need a formula or a figure worked out in the query. 6. Check: the group counts and sums add up to the overall figures, and the first and last record of each group look right. Sorting in the query behind the report is often cleaner than sorting in the report itself, because the report just receives the records already in order.
Examples
A school keeps sports-day house points in a table (Event, House, Points): Relay Red 12; Hurdles Blue 9; Discus Red 8; Long jump Blue 15; Shot put Red 10; High jump Blue 6; Sprint Red 14. (a) Group the records by House and give the count, sum and average of Points for each house. (b) Give the average of all seven records to 2 decimal places. (c) The five values 45, 9, 120, 33 and 8 are sorted in ascending order, first from a numeric field and then from a text field. Write both orders.
(a) Red: events Relay, Discus, Shot put and Sprint, so the count is 4, the sum is 12 + 8 + 10 + 14 = 44 and the average is 44 ÷ 4 = 11. Blue: events Hurdles, Long jump and High jump, so the count is 3, the sum is 9 + 15 + 6 = 30 and the average is 30 ÷ 3 = 10. Grouping by House gives 2 groups, and the counts add up: 4 + 3 = 7 records. (b) The overall sum is 44 + 30 = 74. The average is 74 ÷ 7 = 10.5714..., which is 10.57 to 2 decimal places. (c) From a numeric field the values go by size: 8, 9, 33, 45, 120. From a text field the characters are compared from the left: 120 (starts with 1), 33, 45, 8, 9. So the order is 120, 33, 45, 8, 9. The same numbers sort differently because the text sort does not read them as numbers.
An island craft business with branches in Kingston, Mandeville and Montego Bay keeps its sales in a table (SaleID, Branch, Category, Amount in JMD): 1 Kingston Bags 4500; 2 Mandeville Jewellery 2800; 3 Montego Bay Bags 5200; 4 Kingston Jewellery 3100; 5 Kingston Bags 3900; 6 Mandeville Bags 4100; 7 Montego Bay Jewellery 2600; 8 Mandeville Jewellery 3300; 9 Montego Bay Bags 4800; 10 Kingston Jewellery 2900; 11 Montego Bay Jewellery 3400; 12 Kingston Bags 3600. (a) The table is sorted by Branch ascending, then Amount descending. List the SaleIDs from top to bottom. (b) The table is grouped by Branch. Give the count, sum and average of Amount for each branch. (c) Give the count, sum and average for the whole table, the average to 2 decimal places. (d) State which branch has the highest sum and which has the highest average, and explain why they are different.
(a) Branch ascending gives Kingston, Mandeville, Montego Bay. Inside each branch, the highest amount comes first. Kingston: 4500 (sale 1), 3900 (5), 3600 (12), 3100 (4), 2900 (10). Mandeville: 4100 (6), 3300 (8), 2800 (2). Montego Bay: 5200 (3), 4800 (9), 3400 (11), 2600 (7). So the order is 1, 5, 12, 4, 10, 6, 8, 2, 3, 9, 11, 7. (b) Kingston: count 5, sum 18000, average 18000 ÷ 5 = 3600. Mandeville: count 3, sum 10200, average 10200 ÷ 3 = 3400. Montego Bay: count 4, sum 16000, average 16000 ÷ 4 = 4000. (c) The count is 5 + 3 + 4 = 12. The sum is 18000 + 10200 + 16000 = 44200. The average is 44200 ÷ 12 = 3683.33 to 2 decimal places. (d) Kingston has the highest sum (18000) and Montego Bay has the highest average (4000). Kingston has five sales and Montego Bay only four, so Kingston adds up to more even though each sale is smaller on average. A sum depends on how many records there are, but an average does not.
Now let's water it
Session runs notes to flashcards to quiz back-to-back.
Where this comes from
- CSEC Information Technology Syllabus (CXC 30/G/SYLL 08, revised 2000, 2008, 2013) · Caribbean Examinations Council (CXC) · Section 8, Specific Objective 7 · Syllabus scope
- PostgreSQL 18 Documentation (sections 2.2, 2.6, 2.7, 5.5, 5.7, 7.5, 8, 9.1, 9.2, 11.1) · PostgreSQL Global Development Group · Chapter 7, Sorting Rows (section 7.5), checked against (https://www.postgresql.org/docs/current/queries-order.html) · Facts
- PostgreSQL 18 Documentation (sections 2.2, 2.6, 2.7, 5.5, 5.7, 7.5, 8, 9.1, 9.2, 11.1) · PostgreSQL Global Development Group · Chapter 2, Aggregate Functions (section 2.7), checked against (https://www.postgresql.org/docs/current/tutorial-agg.html) · Facts
- LibreOffice Base Guide 7.2 · The Document Foundation (LibreOffice Documentation Team) · Chapter 3, Tables, Sorting tables, checked against (https://books.libreoffice.org/en/BG72/BG7203-Tables.html) · Facts
- LibreOffice Base Guide 7.2 · The Document Foundation (LibreOffice Documentation Team) · Chapter 6, Reports, Grouping, Sorting and Summary functions, checked against (https://books.libreoffice.org/en/BG72/BG7206-Reports.html) · Facts