# The data behind a survey (/docs/survey-apps/survey-data)



A survey's answers are ordinary data in your workspace. Factophant keeps them in a
small set of tables it builds and maintains for you, and those tables are what the
Results page reads, what the CSV export writes, and what an analysis app imports.

## The Pipeline page [#the-pipeline-page]

The editor's **Pipeline** page draws those tables as a pipeline, read-only. Every node
on it is generated by Factophant and kept in step with your form: there is nothing to
add, edit or run. A new survey has its whole pipeline from the start, empty
until the first response arrives.

Two kinds of node sit on it:

* **Survey data** - the three tables answers are written into. Open one to see its
  columns and their types on the Configuration tab and the rows on the Data tab.
* **Survey view** - readings over those tables, worked out live whenever they are read.
  Open one to see the statement behind it; the rows are on its Data tab. Its **Run** is
  unavailable, because a view never needs running.

### The three stored tables [#the-three-stored-tables]

| Node            | One row per                                                                           | What it holds                                                                                                                                                                                     |
| --------------- | ------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| **Questions**   | page, question, option and table column, for each published version somebody answered | The form as data: `node_id`, `kind` (`section`, `question`, `option` or `field`), `parent_id`, `label`, `question_type`, `is_required`, `is_archived`, the condition, and `structure_version_id`. |
| **Answers**     | single answer                                                                         | `response_id`, `question_id`, and the answer in exactly one of `option_id`, `value_text`, `value_number`, `value_boolean` or `value_date`, with `structure_version_id`.                           |
| **Submissions** | response                                                                              | `response_id`, `submitted_at` (UTC), `structure_version_id`, and `deleted_at`, set when the response was deleted.                                                                                 |

A few things about **Answers** are worth knowing before you query it:

* `question_id` is the question's **Column name**, shown in its
  [Responses dataset](./build-the-form.mdx#where-each-answer-is-stored) group.
* A **multiple-choice** answer is one row per option picked, so counting an option is a
  simple group-by.
* An **Other** answer is one row with the option id `__other__` and the typed words in
  `value_text`.
* A **table** answer is one row per filled cell: `repeat_index` is the row (from 0) and
  `field_id` the column.
* A **file** answer is one row whose `value_text` describes the file, including its
  `name`.
* A question somebody was never shown, and an optional question left blank, both leave
  no row.

**Questions** is written when a published version receives its first answer, not when
you publish, so every answer can always be traced to the exact words its respondent
saw.

### The six views [#the-six-views]

| Node                 | What it shows                                                                                                                                  |
| -------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------- |
| **Current form**     | Each question at its newest answered version.                                                                                                  |
| **Question changes** | For each question and answered version, what changed and which versions may be counted together.                                               |
| **Labelled answers** | Every answer with its question and option written out in your words, marked where the question has since been redefined.                       |
| **Option counts**    | How many responses picked each option, including options nobody picked.                                                                        |
| **Responses**        | One row per response and one column per question - what the Individual grid and the CSV export read.                                           |
| **Crossed answers**  | Every answer with the respondent's own answers to the first few single-choice questions beside it, so one question can be filtered by another. |

The views that read answers leave deleted responses out.

## Using the answers in an analysis app [#using-the-answers-in-an-analysis-app]

A survey app collects answers; it does not transform them. When you want to join the
answers to another dataset, chart them your own way, read one group's answers question
by question, or feed them into a report, you bring them into an analysis app, which is
where transforms and runs live.

An [analysis-app-data node](../analytical-apps/pipeline/analytic-data.mdx) does that:

1. In the analysis app's editor, add an **Analysis app data** node.
2. Pick the survey as the **Source analysis app*&#x2A;. The list names every survey beside your
   analysis apps, marked &#x2A;(survey)&#x2A;; a survey that has never been published is marked
   &#x2A;(survey - not published yet)* and cannot be picked, because it has nothing to
   import.
3. Pick the **Source node** to import. A survey offers its three stored tables, named
   after the survey: `- questions`, `- answers` and `- responses`, the last being the
   **Submissions** table. The views cannot be imported. Add one node per table you
   need; most analyses use all three.
4. Optionally choose which **Columns** to import; leave it on **All columns** to take
   every one.

Like any node you add by hand, the import lands on the analysis app's **draft**. Adding
it publishes nothing and runs nothing: run the pipeline to see the answers flow through
it, and publish when the result is what you want.

A survey has no runs of its own, so each run of the analysis app reads the survey's
tables **as they are at that moment** and keeps that copy in its own history. New
responses reach the analysis app on its next run.

### Reading the imported tables [#reading-the-imported-tables]

The answers arrive one row per answer, which keeps every question type and every
version of the form in one shape. A SQL node puts them back together. Join **answers**
to **questions** for the words, and to **submissions** for the time - and keep only the
submissions whose `deleted_at` is empty, so a response you deleted in the survey stays
out of your results.

With import nodes named `commute_answers`, `commute_questions` and
`commute_submissions`, this counts how many people picked each answer to one question:

```sql title="commute_mode_counts.sql"
SELECT q.label AS mode,
       COUNT(*) AS respondents
  FROM commute_answers a
  JOIN commute_submissions s
    ON s.response_id = a.response_id
  JOIN commute_questions q
    ON q.kind = 'option'
   AND q.parent_id = a.question_id
   AND q.node_id = a.option_id
   AND q.structure_version_id = a.structure_version_id
 WHERE a.question_id = 'how_do_you_get_to_work'
   AND s.deleted_at IS NULL
 GROUP BY q.label
```

An **Other** answer has no option row, so this query leaves those out; read them from
`value_text` where `option_id` is `__other__`. You do not have to write any of this
yourself: describe the result you want to Vera, and it writes the SQL node. See
[SQL nodes](../analytical-apps/pipeline/sql-nodes.mdx).

The usual rules for importing apply - who may import which app, and what an import
does to who can change the importing app. See
[Analysis app data](../analytical-apps/pipeline/analytic-data.mdx).
