Note: This page only applies to OpenBiblio 0.6.0 and later. OpenBiblio versions 0.5.2 and earlier use a completely different syntax for writing reports. So you want some information from OpenBiblio. You know the information is there, you just don't know how to get at it. This how-to is for you. If you've already got the information in a report, but you want to know how to format it into letters or labels or some other format, what you need is a layout. See: Report Layouts. If you already know basically how to write reports, but you need more information about one of the report language's features, see RPT Syntax. Jump StartSomeone may already have written the report you need or one that you can modify to fit. This tutorial will help you start from scratch, but you may not have to. Take a look at the reports that have already been written. PrerequisitesIn order for this tutorial to be much help to you, there are a few things you should understand first. You need to know how to make a plain text file and place it in a folder inside your OpenBiblio installation. You also should have at least a basic understanding of SQL and OpenBiblio's database structure. You can Google "sql tutorial" for a start on SQL. For OpenBiblio's database structure, you'll either have to figure it out on your own or ask questions on the Forums. There isn't any real documentation for the database format yet. Getting startedFirst, we need to know what information we want from the database. For this how-to, we'll write a report that shows us the average age of the items in our collections. We can guage the age of an item by it's publication date which should be stored in MARC field 260$c. Here's an SQL query to select all the publication dates in the database:
SELECT bibid, field_data
FROM biblio_field
WHERE tag="260" AND subfield_cd="c"
Now the average date:
SELECT AVG(field_data)
FROM biblio_field
WHERE tag="260" AND subfield_cd="c"
But we want the dates listed by collection, so we need to pull in the biblio and collection_dm tables:
SELECT c.code, c.description collection, AVG(f.field_data) date
FROM biblio b, biblio_field f, collection_dm c
WHERE f.bibid=b.bibid AND c.code=b.collection_cd
AND f.tag="260" AND f.subfield_cd="c"
GROUP BY c.code, c.description
So, having the basic idea of the report and some SQL to back it up, we can create a basic report for OpenBiblio:
.title "Average Publication Date"
.column code
.column collection
.column date
.sql
SELECT c.code, c.description collection, AVG(f.field_data) date
FROM biblio b, biblio_field f, collection_dm c
WHERE f.bibid=b.bibid AND c.code=b.collection_cd
AND f.tag="260" AND f.subfield_cd="c"
GROUP BY c.code, c.description
.end
If you put this into a file with a .rpt extension and save it in the openbiblio/reports/defs folder, you should see the new report on your report list called "Average Publication Date". Clicking on that report, you'll be asked in what format you want the results. For now, leave it at the default, "HTML (page-by-page)". Hitting "Submit", you should be shown a table with three columns and one row per collection. (If a collection doesn't have any items with a 260$c field, it won't be shown.) So what have we done here? The SQL query itself is exactly the same one we wrote initially, but we've use the RPT language to tell OpenBiblio a little about how to run it and how to show us the results. To do this, we added directives to describe the report to OpenBiblio. Directives in the RPT language are entered on a line that starts with a period. The first directive in this report, Next we tell OpenBiblio which columns to display. To do this, we have a line for each column that begins That's all you need for a very basic report. PrettifyingThere are a few aesthetic improvements we could make on this report. First, it appears in a category on the reports list called "Misc." That probably isn't what we want. We'd like it to appear in the "Cataloging" category, or perhaps a new one called "Statistics". To move the report into a different category, we just have to add the following directive after the title line: .category Statistics That will put our report in the "Statistics" category, which will be created if it doesn't exist already. You'll notice we didn't put "Statistics" in quotes. We could have, and we would have to if the category name had spaces or non-alphanumeric characters in it. But "Statistics" is just one word, so the quotes are optional. Next, we really don't need to see the collection code in the report results. Why do we care what the code number for the collection is? We don't. The simplest way to remove that column is just to delete the '.column code' line. OpenBiblio doesn't care if there are more columns in the SQL results than you specify, it only shows the ones you tell it to. Now the remaining columns need more readable titles. Right now, the columns in the results table are labeled 'collection' and 'date'. We'd like prettier names, so we'll change those column lines like this: .column collection title=Collection .column date title="Avg. Pub. Date" We added options to the column directive. A column can have a number of options, but the one called 'title' changes the column's title shown in the report results. Again, notice that we didn't have to use quotes around "Collection", but they were necessary around "Avg. Pub. Date". So here's the report so far:
.title "Average Publication Date"
.category Statistics
.column collection title=Collection
.column date title="Avg. Pub. Date"
.sql
SELECT c.code, c.description collection, AVG(f.field_data) date
FROM biblio b, biblio_field f, collection_dm c
WHERE f.bibid=b.bibid AND c.code=b.collection_cd
AND f.tag="260" AND f.subfield_cd="c"
GROUP BY c.code, c.description
.end
SortingThe last improvement we'd like to make to this report is to make it sortable. Odds are, there aren't that many rows in the results here, but you'd still like them to be in order. You could simply add ' .parameters . order_by . item collection . end .end Place this code before the
.title "Average Publication Date"
.category Statistics
.column collection title=Collection
.column date title="Avg. Pub. Date"
.parameters
. order_by
. item collection
. end
.end
.sql
SELECT c.code, c.description collection, AVG(f.field_data) date
FROM biblio b, biblio_field f, collection_dm c
WHERE f.bibid=b.bibid AND c.code=b.collection_cd
AND f.tag="260" AND f.subfield_cd="c"
GROUP BY c.code, c.description
. order_by_expr
.end
To make sorting more convenient, we can add sort buttons to a column. To add the buttons for the 'collection' sort type to the 'collection' column, we add a .column collection title=Collection sort=collection We'd also like to sort by the . item date expr="AVG(f.field_data)" There's still a problem, though. By default, OpenBiblio's sort does some processing on the column value to make alphanumeric fields with leading numbers sort correctly. When dealing with straight numeric values, though, it doesn't work properly. So we need to tell OpenBiblio that we're not sorting an alphanumeric value, but a straight numeric one, like this: . item date expr="AVG(f.field_data)" type=numeric Finally, we add sort buttons to the date column. Here's the final report:
.title "Average Publication Date"
.category Statistics
.column collection title=Collection sort=collection
.column date title="Avg. Pub. Date" sort=date
.parameters
. order_by
. item collection
. item date expr="AVG(f.field_data)" type=numeric
. end
.end
.sql
SELECT c.code, c.description collection, AVG(f.field_data) date
FROM biblio b, biblio_field f, collection_dm c
WHERE f.bibid=b.bibid AND c.code=b.collection_cd
AND f.tag="260" AND f.subfield_cd="c"
GROUP BY c.code, c.description
. order_by_expr
.end
More complicated reportsFIXME Need to introduce these topics:
Categories |