Koha SQL Reports

GiftED Community Session

Eric Phetteplace ephetteplace@cca.edu, Systems Librarian, California College of the Arts

These slides are available at phette23.github.io/koha-gifted-sql/.

Session specifics

Tips and tricks specific to Koha reports but not SQL in general.

There will be some SQL on slides but hopefully nothing too arcane.

I'm no expert! Please chime in with your own ideas in chat.

What's in a table?

See the Reports Library

And Schema schema.koha-community.org/

"Search engine" links to any given table: https://schema.koha-community.org/23_05/tables/%s.html

Experimental queries

Peek at what's in a particular table:

SELECT * FROM deletedbiblio

Enumerate the values in a field:

SELECT DISTINCT itype FROM items ORDER BY itype
SELECT DISTINCT itype, COUNT(*) FROM items
GROUP BY itype ORDER BY itype

deleted* tables

Deleted biblios, metadata, items, and borrowers (patrons) are not removed from the database permanently but transferred to a table with a "deleted" prefix.

Similarly, past issues (checkouts) and holds (reserves) are moved to tables prefixed with "old".

To be accurate, many reports must factor in these "deleted" or "old" tables.

Use a UNION in a subquery

All biblios:

SELECT * FROM (
	SELECT * FROM biblio
	UNION
	SELECT * FROM deletedbiblio) b
ORDER BY b.biblionumber

Annotate combined tables

This combined items table has a "status" column.

SELECT i.status, i.itemnumber, i.itype FROM
	(SELECT "live" as status, i.* FROM items i
	UNION
	SELECT "deleted" as status, di.* FROM deleteditems di) i
status itemnumber itype
live 1 BOOK
deleted 2 BOOK

Annual bibs added

SELECT YEAR(datecreated) as Year, COUNT(*) as "Records Added"
FROM (
  SELECT biblionumber, datecreated FROM biblio
  UNION
  SELECT biblionumber, datecreated FROM deletedbiblio
) b
WHERE YEAR(datecreated) BETWEEN 2016 AND YEAR(CURDATE())
GROUP BY YEAR(datecreated)

statistics versus issues table

Rather than combine issues & oldissues for circulation figures, use statistics.

The issues tables have no item information & JOINing them to the items tables does not account for changes in item type or location since the checkout occurred.

Checkouts by branch

SQL by Nicole C. Engard, ByWater Solutions

SELECT branch,
MONTH(datetime) AS month,
YEAR(datetime) AS year,
COUNT(datetime) AS checkouts
FROM statistics
WHERE type LIKE 'issue'
GROUP BY branch, year, month
ORDER BY year, month, branch

Translate authorized value codes

Fields that use an authorized value as a controlled vocabularly store the Authorized Value code and not it's human readable label, but we can connect to the authorised_values table.

notforloan example

SELECT i.notforloan as "NFL Code", av.lib as "Label",
COUNT(*) as "Items"
FROM items i
JOIN authorised_values av ON i.notforloan = av.authorised_value
  AND av.category = "NOT_LOAN"
GROUP BY i.notforloan
NFL Code Label Items
-3 Repair 10
-2 In Processing 5
0 Available 65,735

Public JSON data

Make a report's data easily accessible to external programs by setting it to "Public" and copying its JSON URL. The URLs look like:

https://koha.library.org/cgi-bin/koha/svc/report?id=42

Public means public! Don't include confidential information.

Add 948 to OPAC

Report with runtime parameter:

JavaScript in OPACUserJS:

Reports keep getting better!

ID columns connect to bulk operations

"Update and run" button

Autocompletion

Graphs

a pie chart of items per library branch showing 81.3% of items are in SF, 15.8% are in Oak, and 2.9% in MATLIB.

#reports channel in BWS-Partners Slack

For instance, Koha reports won't allow the words UPDATE, DELETE, DROP, INSERT, SHOW, or CREATE anywhere, even in comments or strings. But folks suggested handy tricks like:

SELECT * FROM biblio
WHERE title LIKE CONCAT('%Rocky Horror Picture Sh','ow%')

Questions?

Thanks for coming! These slides are available at phette23.github.io/koha-gifted-sql/.

Eric Phetteplace

Systems Librarian

California College of the Arts

libraries.cca.edu