Contingency Table Marginal Totals

Quick Overview

Write a SQL query that returns the row, column and grand marginal totals of a two-way contingency table stored as one row per subject. It tests counting at several grains in one result set and producing a deterministic row order with a custom sort.

Contingency Table Marginal Totals

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

The `crosstab_data` table has one row per subject, with `row_cat` (`'A'` or `'B'`) and `col_cat` (`'X'` or `'Y'`). Return the marginal totals of this cross-tab in three columns, `margin_type`, `category` and `total`: - one `'row'` row per `row_cat` level, with the level as `category` and its count as `total`, - one `'col'` row per `col_cat` level, built the same way, - a final `'grand'` row with `category` set to `'all'` and the overall count as `total`. ```hint Watch the sort Sorting on `margin_type` alphabetically puts `'col'` first and `'row'` last, which is not the order the output asks for. ``` ### Input Table(s) Table `crosstab_data`: | Column | Type | Meaning | |---|---|---| | `row_cat` | VARCHAR(5) | Row level, `'A'` or `'B'` | | `col_cat` | VARCHAR(5) | Column level, `'X'` or `'Y'` | The table has no primary key. Each row is one subject, so identical rows are different subjects. ### Output Contract - Columns, in this order: `margin_type`, `category`, `total`. - `total` is an integer count of subjects. - Row order: all `'row'` rows first, then all `'col'` rows, then the `'grand'` row. Within the `'row'` and `'col'` blocks, sort by `category` ascending. - The pair (`margin_type`, `category`) is unique, so this order is fully determined. ### Example Input `crosstab_data`: | row_cat | col_cat | |---|---| | A | X | | A | X | | B | Y | | A | Y | | A | Y | | A | Y | | B | Y | | B | Y | | A | Y | | B | X | | A | Y | | B | X | | A | X | | B | X | | B | X | | B | Y | | A | X | | A | X | Expected output: | margin_type | category | total | |---|---|---| | row | A | 10 | | row | B | 8 | | col | X | 9 | | col | Y | 9 | | grand | all | 18 | A appears in 10 rows and B in 8. X and Y appear in 9 rows each. Both sets of margins add up to the grand total of 18. ### Constraints and Clarifications - `row_cat` and `col_cat` are never NULL. - Duplicate rows are separate subjects and all of them count. - A level that has no rows does not appear in the output. The `'grand'` row always appears, with a total of 0 if the table is empty.

Overview: Write a SQL query that returns the row, column and grand marginal totals of a two-way contingency table stored as one row per subject. It tests counting at several grains in one result set and producing a deterministic row order with a custom sort.

Nov 25, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

The crosstab_data table has one row per subject, with row_cat ('A' or 'B') and col_cat ('X' or 'Y'). Return the marginal totals of this cross-tab in three columns, margin_type, category and total:

  • one 'row' row per row_cat level, with the level as category and its count as total ,
  • one 'col' row per col_cat level, built the same way,
  • a final 'grand' row with category set to 'all' and the overall count as total .

Input Table(s)

Table crosstab_data:

ColumnTypeMeaning
row_catVARCHAR(5)Row level, 'A' or 'B'
col_catVARCHAR(5)Column level, 'X' or 'Y'

The table has no primary key. Each row is one subject, so identical rows are different subjects.

Output Contract

  • Columns, in this order: margin_type , category , total .
  • total is an integer count of subjects.
  • Row order: all 'row' rows first, then all 'col' rows, then the 'grand' row. Within the 'row' and 'col' blocks, sort by category ascending.
  • The pair ( margin_type , category ) is unique, so this order is fully determined.

Example

Input crosstab_data:

row_catcol_cat
AX
AX
BY
AY
AY
AY
BY
BY
AY
BX
AY
BX
AX
BX
BX
BY
AX
AX

Expected output:

margin_typecategorytotal
rowA10
rowB8
colX9
colY9
grandall18

A appears in 10 rows and B in 8. X and Y appear in 9 rows each. Both sets of margins add up to the grand total of 18.

Constraints and Clarifications

  • row_cat and col_cat are never NULL.
  • Duplicate rows are separate subjects and all of them count.
  • A level that has no rows does not appear in the output. The 'grand' row always appears, with a total of 0 if the table is empty.