Separate a category report into a named summary stage and a filtering stage. The outer query can refer to total_fee as an ordinary column of that named result. Keep both stages in one SQL statement.
These tables are already loaded at the start of this lesson.
gearOne row represents one equipment catalog entry.
| Field / SQL type | Meaning | Rules |
|---|---|---|
gear_id · integer | Identifier of the equipment entry. | Primary key. Required; cannot be NULL. |
name · text | Equipment display name. | Required; cannot be NULL. |
category · text | Equipment grouping, such as repair, outdoors or art. | Required; cannot be NULL. |
fee · numeric(6,2) | Daily borrowing charge in currency units, stored to two decimal places. | Required; cannot be NULL. |
available · boolean | Whether the equipment is available to borrow: true or false. | Required; cannot be NULL. |
note · text | Optional handling note. | Nullable; NULL means no note is recorded. |
Only note may be missing. Category labels are sample values, not an enforced vocabulary. No currency code or nonnegative-fee constraint is declared; a fee is a daily charge, not a count of items.
category_totalsOne row represents one category group.
| Field / SQL type | Meaning | Rules |
|---|---|---|
category · text | Grouping label from gear.category. | Logical group key, not a physical primary key. Required in the base table. |
total_fee · numeric | Sum of gear.fee in this group, in daily borrowing currency units. | Computed value; no fixed precision or scale. Known for each nonempty group because fees are required. |
The CTE is a named result within one statement, not a preloaded table or persistent view. Its grouped label is logically unique, with NULL grouping handled as one group where allowed.
The setup below is already loaded before each attempt. It is reference material, not code to paste into script.sql.
CREATE TABLE gear (
gear_id integer PRIMARY KEY,
name text NOT NULL,
category text NOT NULL,
fee numeric(6,2) NOT NULL,
available boolean NOT NULL,
note text
);
INSERT INTO gear VALUES
(1, 'Hand drill', 'repair', 4.00, true, NULL),
(2, 'Tent', 'outdoors', 12.00, true, 'Dry before return'),
(3, 'Saw', 'repair', 6.00, false, 'New blade'),
(4, 'Camp stove', 'outdoors', 6.00, true, NULL),
(5, 'Brush kit', 'art', 3.00, false, 'Wash bristles');
Edit script.sql, use Run to inspect the result, then Submit.
| category | total_fee |
|---|---|
| outdoors | 18 |
| repair | 10 |
The outer WHERE can use a total exposed as a CTE column.