# The GNUmed Report Generator

GNUmed offers two fundamentally different ways to search the database:

1. across the EMR of the currently active patient
2. across the entire medical database regardless of the active patient

The second approach is sometimes called data mining. GNUmed has a plugin called Reports to enable you to create database-wide reports.

## Scope

The plugin is intended to generate simple reports. The powers of this tool do not go beyond what you can do from within PostgreSQL. However, one can enhance PostgreSQL with the "R" procedural language (or, in fact, any other one) in order to unleash considerable statistical powers right from within the SQL query.

For anything more sophisticated than that (say, post-processing the report results) one will have to turn to custom scripting, off-the-shelf report generators or data mining tools such as NetEpi.

## Usage

### Generating Reports

To generate a report from the database you need to run an SQL query. The query has to be typed into the Command (SQL) field of the Reports plugin. Then hit the button [Run]. The results will be shown in the list at the bottom. The columns of the list will correspond to the columns of the database table(s) you collect data from with the query. You may want to use the SQL AS column alias syntax to map database columns to convenient list column labels.

Here are a few things to know:

- you don't need to worry about leading or trailing whitespace/linefeeds
- your query may span several lines
- you don't need to end your query with a semicolon (";") - but you can
- you can paste query text from elsewhere via the clipboard
- you can drag a file onto the Reports plugin and GNUmed will interpret the file content as the query to run
  - note that you need to drag the file onto an area of the plugin outside the actual query command field (this will be improved later)

- the plugin will artificially limit the results list to 1000 rows (one thousand) such as to somewhat safeguard against queries going berserk

- the queries are run in a read-only connection with the credentials of the user who logged on with this client
  - no writing to the database is possible, not even as a side-effect of a `select my_writing_func()` query

- you might want to make your query returns a column named `pk_patient`
  - this will enable you to double-click on any row in the results list which will activate the patient identified by the database ID found in `pk_patient`
  - if there is no such column nothing will happen (besides an error message being shown)
  - note that you can, of course, make a query return an arbitrary number in a column named `pk_patient` in which case the corresponding patient will, indeed, be activated upon double-clicking a row but there may not, in fact, be any other meaningful correlation of the patient with that row

### Visualizing reports

Hitting the [Visualize] button will let you select a column from the report results list from each of the x- and y-axis. The data in those columns is extracted from the report and sent to gnuplot for display.

### Reusing report definitions

The Report field acts as a phrasewheel offering names of reports that were previously saved in the database. You can either type part of a name our part of a query (such as a table name) and select a report definition from the appearing dropdown match list. The corresponding query will be loaded from the database.

If you press [Save] the report definition will be saved in the database. If the report name is already known in the database the existing report definition will be overwritten. If not a new report definition will be created.

Hitting [Contribute] will email the report definition (name and query - nothing else) to the mailing list of the GNUmed community for all to share. This will happen anonymously. If you want to receive credit for it you'll have to actively claim it on the mailing list.

Note that report results are only preserved as long as the client instance they were generated in stays open. They will, however, survive changing the active patient.

The [Schema] button will take you to the GNUmed database schema documentation in our wiki for your reference.

### Sample queries

       select count(*) from dem.identity

perhaps adding

       where deleted is FALSE / TRUE

and/or

       where deceased is NULL / NOT NULL

depending on what is wanted.

- a query that (new in gnumed_v9) can search for patients based on the diagnostic code

       select *
       from
          dem.v_basic_person
             inner join
          clin.v_coded_item_narrative
             using (pk_identity)
       where
          code = ...
          and coding_system = ...
          and soap_cat = ...
       ;

- a query that would help by providing more fields (and sample values) that could be altered and used to find a patient when the standard patient search field did not permit a patient to be found, perhaps including the communication channels (phone numbers). Such queries could be

       select *
       from
          dem.v_basic_person
             inner join
          dem.v_person_comms / dem.v_person_jobs / dem.v_external_ids4identity
             using (pk_identity)
       where
          dem.v_person_comms.url = ... /
          dem.v_person_jobs.l10n_occupation = ... /
          dem.v_external_ids4identity.value = ...
       ;