Why Does a Combined SQL List Change Order Between Reports?
A project report combines milestone names into a single line. Yesterday it read “Intake, Review, Delivery”; today it reads “Review, Intake, Delivery.” No milestone changed. Someone adds an ORDER BY to the outer query, but the line still changes. The report has two ordering problems: the order of project rows and the order of values inside each project's combined list.
Treat the internal list as a small published artifact. If its order communicates sequence, priority or recency, that order belongs in the data contract and in the aggregation expression.
Distinguish row order from list order
PostgreSQL's aggregate-function documentation explains that functions such as string_agg and array_agg can produce different results depending on input order. That order is unspecified by default; an ORDER BY inside the aggregate can make it explicit.
Sorting completed result rows by project name answers which project appears first. It does not specify the order of milestones already assembled inside a result value.
The documentation also cautions that relying on a sorted subquery can fail when later processing reorders its output. Place the ordering where the aggregation contract requires it, rather than relying on a convenient current execution plan.
Specify what the sequence means
Ask the report owner whether the list is chronological, alphabetical, ordered by planned position or ordered by current priority. “Whatever appears first in the database” is not a business definition.
Record the fields that establish that sequence. If two milestones share the same planned position, decide whether a secondary stable identifier should break the tie or whether the duplicate position is a data issue that requires review.
Illustrative example: three milestones
Imagine milestone records with positions 10, 20 and 30 and labels Intake, Review and Delivery. An illustrative PostgreSQL expression is string_agg(label, ', ' ORDER BY position, milestone_id). The second ordering field is intended to make tied positions deterministic, assuming that identifier is unique.
The expression is only appropriate if planned position is the agreed meaning. If the reader wants actual completion order, ordering by planned position would be a consistently wrong answer.
Show a tie before approving the rule
Add another milestone at position 20. Present the two tied records to the owner and ask what the report should communicate. A stable identifier makes the sequence reproducible, but does not establish that one milestone logically precedes the other.
If ties are meaningful parallel work, consider displaying the position alongside each label or retaining separate rows instead of inventing a strict process sequence.
Handle labels without changing identity
Include a missing label, an empty label and two milestones with the same visible name in the fixture. Decide how each should appear. A summary that silently loses an unnamed milestone can mislead a reader even when its remaining values are ordered correctly.
Likewise, a DISTINCT operation on the displayed label can collapse two separate milestones. Deduplication should follow the intended identity rule, not merely make the text shorter.
Do not use the combined display string as the only interchange format. A label containing a comma can make a comma-separated sentence ambiguous. If another system needs individual records, provide a structured representation with stable identifiers and an explicit ordering field.
Review the output in its actual use
Check whether long lists remain readable in the report layout. A cell that truncates after two milestones can hide the very item a reviewer needs. Provide an accessible way to inspect the complete sequence or use a separate detail view.
Compare the number of source milestones with the number represented in the output. Record exclusions and missing labels so a polished sentence does not imply completeness that the data lacks.
Repeat the fixture with source rows inserted in a different order. The agreed result should remain unchanged when insertion order is irrelevant to the business rule.
Preserve the ordering decision
Store the list definition, tie policy, fixture and expected output together. When a future change requests “latest first,” reviewers can see exactly which ordering contract would change.
For ordering result rows themselves, see making a SQL top-ten queue predictable. A FutureOS brief can start with the list's intended meaning and two outputs that look similar but lead readers to different conclusions.
Editorial note
Editorial note: This article was prepared with AI assistance. Primary sources and the linked FutureOS article inventory were checked on October 26, 2026. Examples are illustrative and are not customer results. The current FutureOS introduction describes the project as being in prelaunch; this guide does not assert that a particular product feature, integration or service is available. The procedures are practical proposals to adapt and test in your own setting. No independent expert review is claimed. Start with a specific question, your relevant constraints and a small example of the result you need.




Comments