Sub topic 7
Designing a daily table you can query
This chapter assumes you can read basic SQL or pandas, know the Search Console report columns (query, page, clicks, impressions, CTR, position), and have seen how data is pulled on a schedule. Here you decide what one row in your own table means. Later chapters on position averaging, segmentation, and trends all depend on that decision.
Why the grain comes first
The grain of a table is what one row represents. Write it as a sentence before you write any SQL: "One row is the performance of one query, landing on one page, in one country, on one device, on one day."
A wrong or vague grain causes most reporting mistakes. If some rows are daily and others weekly, sums double count. If a row holds a ratio such as CTR, you cannot combine it with another row without the original counts. If you cannot say what a row is, you cannot say what SUM(clicks) means.
For Search Console data, a good default grain is:
| Column | Role | Example |
|---|---|---|
| `date` | Dimension (key) | 2026-01-05 |
| `query` | Dimension (key) | blue widgets |
| `page` | Dimension (key) | https://example.com/widgets |
| `country` | Dimension (key) | usa |
| `device` | Dimension (key) | MOBILE |
| `clicks` | Measure (additive) | 4 |
| `impressions` | Measure (additive) | 125 |
| `position` | Measure (needs weighting) | 4.3 |
Dimensions identify the row. Measures describe it. Together the dimensions form the primary key, so the database rejects duplicates.
Store additive facts, derive ratios
Clicks and impressions are additive. You can sum them across any dimension and the result is still a true count. CTR is clicks divided by impressions, so it is not additive. Averaging the CTR of two rows gives the wrong answer unless both rows have equal impressions.
The rule: store the raw counts and compute ratios in the query. Do not store ctr as a column you will later average. The API gives you a ctr value, but you can always recompute it from clicks and impressions, so dropping it removes a way to go wrong.
Position needs more care. The API returns an average position for each row, and the correct weight for combining rows is impressions. A row with 5,000 impressions at position 3 should count for far more than a row with 5 impressions at position 40. The next chapter covers this in detail. For the table, it means you must keep impressions next to position on every row. You can then compute SUM(position * impressions) / SUM(impressions) at any level.
Add a totals table
The Search Console query dimension leaves out anonymized queries, which are rare queries Google withholds for privacy. If you sum your query rows for a day, the total is lower than the property total shown in the console.
To keep both views, load a second, coarser table pulled without the query dimension:
gsc_daily: the detailed table above.gsc_daily_totals: one row per date, country, and device (and optionally page), with no query.
Comparing the two tells you what share of clicks you can attribute to named queries. The caveats chapter covers why the gap exists. Here you only need to have a place to store it.
Make loading safe to repeat
Recent days in Search Console can still change. Your loader will re-pull a trailing window of several days. If the table only supports inserts, every re-pull creates duplicates. The primary key plus an upsert (insert, or update if the key already exists) makes the load idempotent, which means running it twice gives the same result as running it once.
Add a fetched_at timestamp column so you can tell which load produced a row. It is metadata, not part of the key.
-- PostgreSQL syntax
CREATE TABLE gsc_daily (
date date NOT NULL,
query text NOT NULL,
page text NOT NULL,
country text NOT NULL,
device text NOT NULL,
clicks integer NOT NULL CHECK (clicks >= 0),
impressions integer NOT NULL CHECK (impressions >= clicks),
position numeric(8,3) NOT NULL CHECK (position >= 1),
fetched_at timestamptz NOT NULL DEFAULT now(),
PRIMARY KEY (date, query, page, country, device)
);
CREATE INDEX gsc_daily_page_date ON gsc_daily (page, date);
CREATE INDEX gsc_daily_query_date ON gsc_daily (query, date);The CHECK constraints turn silent data problems into loud load errors. The two indexes support the two most common questions: "how did this page do over time" and "how did this query do over time". The primary key already covers lookups that start with date.
The same idea in a runnable Python script, using SQLite so you can try it without a server:
import sqlite3
conn = sqlite3.connect(":memory:")
conn.execute("""
CREATE TABLE gsc_daily (
date TEXT NOT NULL,
query TEXT NOT NULL,
page TEXT NOT NULL,
country TEXT NOT NULL,
device TEXT NOT NULL,
clicks INTEGER NOT NULL,
impressions INTEGER NOT NULL,
position REAL NOT NULL,
PRIMARY KEY (date, query, page, country, device)
)
""")
UPSERT = """
INSERT INTO gsc_daily
(date, query, page, country, device, clicks, impressions, position)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
ON CONFLICT (date, query, page, country, device) DO UPDATE SET
clicks = excluded.clicks,
impressions = excluded.impressions,
position = excluded.position
"""
first_load = [
("2026-01-05", "blue widgets", "https://example.com/widgets", "usa", "MOBILE", 3, 120, 4.2),
("2026-01-05", "blue widgets", "https://example.com/widgets", "usa", "DESKTOP", 5, 80, 3.1),
("2026-01-05", "widget repair", "https://example.com/repair", "usa", "MOBILE", 1, 20, 8.0),
]
# A later re-pull of the same day: one row has revised numbers.
reload_rows = [
("2026-01-05", "blue widgets", "https://example.com/widgets", "usa", "MOBILE", 4, 125, 4.3),
]
conn.executemany(UPSERT, first_load)
conn.executemany(UPSERT, reload_rows)
print("rows:", conn.execute("SELECT COUNT(*) FROM gsc_daily").fetchone()[0])
query = """
SELECT query,
SUM(clicks) AS clicks,
SUM(impressions) AS impressions,
ROUND(1.0 * SUM(clicks) / SUM(impressions), 4) AS ctr,
ROUND(SUM(position * impressions) / SUM(impressions), 2) AS avg_position
FROM gsc_daily
GROUP BY query
ORDER BY query
"""
for row in conn.execute(query):
print(row)The script prints rows: 3, not 4, because the reload replaced the existing row. Then it prints ('blue widgets', 9, 205, 0.0439, 3.83) and ('widget repair', 1, 20, 0.05, 8.0). The blue widgets position is 3.83, not the plain average of 4.3 and 3.1 (3.7), because the mobile row has more impressions.
Keep descriptions out of the fact table
Notice what the table does not contain: brand or non-brand flags, page group names, or keyword priority. Those labels change. You will redefine "brand" when you notice a misspelling, and you will reorganize page groups when the site changes.
If you write labels into every fact row, each change means rewriting millions of rows. Keep them in small lookup tables and join them at query time.
-- PostgreSQL syntax
CREATE TABLE query_labels (
query text PRIMARY KEY,
is_brand boolean NOT NULL
);
CREATE TABLE page_groups (
page text PRIMARY KEY,
grp text NOT NULL
);
CREATE VIEW gsc_daily_labeled AS
SELECT d.*,
COALESCE(q.is_brand, false) AS is_brand,
COALESCE(p.grp, 'unassigned') AS page_group
FROM gsc_daily d
LEFT JOIN query_labels q USING (query)
LEFT JOIN page_groups p USING (page);Using LEFT JOIN with a default keeps unlabeled rows in the totals instead of dropping them. A report that silently loses unlabeled rows will not match the raw table, and that is hard to debug. The segmentation chapter builds on this view.
Keep rank-tracker data separate
A rank tracker checks specific keywords from a chosen location and device, and it reports a single rank. That is a different grain from Search Console, where rows come from real impressions and position is an average. Do not put both in one table.
Give the tracker its own table keyed by date, keyword, location, and device, with the rank measure and perhaps the URL that ranked. You can join the two tables on date and keyword when you want to compare them, as long as you remember each side's meaning.
Common errors
- Missing days stay missing. If a day had no impressions for a query, no row exists. For trend work you will later need a complete calendar, so join to a date series rather than assuming gaps are zeros in the table. For a query, a missing day usually means zero impressions, but a failed load also looks like a missing day. Record each load in a small log table to tell the two apart.
- Unnormalized URLs.
https://example.com/widgetsandhttps://example.com/widgets/become two keys. Decide on one form (for example, no fragment, consistent trailing slash) and apply it in the loader. - Query text case. Search Console already lowercases queries, but data from other tools might not. Lowercase and trim on load.
- Storing CTR and averaging it. Compute it from clicks and impressions.
- Mixed time zones. Search Console dates use Pacific Time. Web analytics dates use the property time zone. Keep each source's own date in its own table and note the difference.
Exercise
A teammate adds a ctr column to gsc_daily and reports weekly CTR as AVG(ctr) per query. A query has two rows in a week: 1 click on 10 impressions, and 50 clicks on 1,000 impressions. What does AVG(ctr) give, what is the correct weekly CTR, and how should the table change?
Show answer
AVG(ctr) gives (0.10 + 0.05) / 2 = 0.075. The correct weekly CTR is (1 + 50) / (10 + 1000) = 51 / 1010, about 0.0505. The plain average gives the small row as much weight as the large one. Remove the stored ctr column and compute SUM(clicks) * 1.0 / SUM(impressions) in the query, so the result is correct at any level of aggregation.
Exercise
Your loader re-pulls the last 5 days every night. Which two features of the table design make this safe, and what would go wrong without the first one?
Show answer
The composite primary key on (date, query, page, country, device) and an upsert that updates the measures on conflict. Without the key, each re-pull would insert duplicate rows, which doubles clicks and impressions for the overlapping days and makes every sum wrong.