SQL Reports in Koha

Uploaded on

This was my presentation at KohaCon09

This was my presentation at KohaCon09

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
No Downloads


Total Views
On Slideshare
From Embeds
Number of Embeds



Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

    No notes for slide


  • 1. MySQL Reports in Koha in Koha
    • Nicole C. Engard Koha Documentation Manager
    • KohaCon 2009
  • 2. MySQL Intro
    • MySQL is the language used to query data in the Koha database.
    • Koha’s database structure can be viewed here:
      • http://tinyurl.com/kohadbstructure
    • MySQL’s Documentation Library is a great source for help and information
      • http://dev.mysql.com/doc/index.html
  • 3. Reading DB Structure
    • CREATE TABLE `branches` ( `branchcode` varchar(10) NOT NULL default '', `branchname` mediumtext NOT NULL, `branchaddress1` mediumtext, `branchaddress2` mediumtext, `branchaddress3` mediumtext, `branchphone` mediumtext, `branchfax` mediumtext, `branchemail` mediumtext, `issuing` tinyint(4) default NULL, `branchip` varchar(15) default NULL, `branchprinter` varchar(100) default NULL, UNIQUE KEY `branchcode` (`branchcode`) )
  • 4. Reading DB Structure
    • This table’s name is ‘branches’ and it stores the information about libraries or branches in Koha.
    • Each field is easy to identify because of its name (ex. branchname is the library name).
    • A field with a number in parens after it is a field that is limited in size.
      • So varchar(10) means the field can have no more than 10 characters in it
    • Lastly, we see that ‘branchcode’ is the unique key or unique identifier in the table.
  • 5. Reports in Koha
    • Koha has a very powerful guided reports wizard, but sometimes you want to write your own report - for that you need SQL
    • To start learning how to write SQL queries, you may want to create some guided reports and view the outputted SQL
  • 6. Querying SQL
    • Once you know your database structure, querying SQL is very straightforward.
    • For Example:
      • SELECT branchcode,branchname FROM branches ORDER BY branchname ASC
      • This will list all of the branches and their codes in alphabetical order by branch name
  • 7. Querying a Single Table Single Table
    • To query a single table you will structure your query like this:
      • SELECT column_names from table_name [WHERE ...conditions] [ORDER BY ...conditions];
      • Statements in brackets are optional
    • You can also select everything in a table by using an * in place of column_names
  • 8. Querying Multiple Tables Tables
    • To query multiple tables you will have to join them on a key - what we saw as a UNIQUE KEY in the structure
      • SELECT a.column_names, b.column_names FROM a LEFT JOIN b ON (a.key=b.key);
      • OR
      • SELECT branches.branchname, items.barcode, COUNT(issues.issuedate) as totalissues FROM branches LEFT JOIN issues ON (issues.branchcode = branches.branchcode) LEFT JOIN items ON (issues.itemnumber = items.itemnumber) GROUP BY items.barcode;
  • 9. Common Functions
      • Counts the number of or adds up the total value of results in a column
    • CURDATE()
      • Is the current date (not time, just date)
      • Return the month and year from a field
      • Subtract a period of time from a date
  • 10. Common WHERE clauses WHERE clauses
    • WHERE DATE BETWEEN ‘2009-04-01’ AND ‘2009-05-01’
      • Searches between dates (if no time present it assumes 00:00)
    • WHERE accounttype IN("A","F","FU")
      • The same as saying WHERE accounttype = “A” or accounttype = “F” ... etc
      • Can also do NOT IN( ) for !=
  • 11. Date & Time Help
    • The most common use for reports is for end of the month or end of the year statistics
    • The MySQL manual on Date & Time functions is essential for these queries
      • http://dev.mysql.com/doc/refman/5.1/en/date-and-time- functions.html
  • 12. Let’s Play
    • Think about the reports you would like to see in your Koha system and we’ll try to write at least one report for each of you.
  • 13. Reports by Nicole
    • Patrons with fines:
      • SELECT borrowers.cardnumber,borrowers.surname,borrowers.firstname,FORMAT(SUM(accountlines.amountoutstanding),2) as due FROM borrowers LEFT JOIN accountlines on (borrowers.borrowernumber=accountlines.borrowernumber) WHERE accountlines.amountoutstanding > 0 GROUP BY borrowers.cardnumber ORDER BY borrowers.surname asc
  • 14. Reports by Nicole
    • Long Overdues (12 days):
      • SELECT borrowers.cardnumber,borrowers.surname,borrowers.firstname,items.barcode,issues.date_due FROM borrowers LEFT JOIN issues on (issues.borrowernumber = borrowers.borrowernumber) LEFT JOIN items on (items.itemnumber = issues.itemnumber) WHERE issues.date_due < DATE_SUB(now(), INTERVAL 12 DAY) AND issues.returndate is NULL ORDER BY borrowers.surname asc
  • 15. Reports by Nicole
    • Total number of items circulated from a branch other than the owning branch
      • SELECT count(*) as total from statistics LEFT JOIN items on (statistics.itemnumber = items.itemnumber) where statistics.branch != items.homebranch AND statistics.datetime between '2008-01-01' and '2008-12-31'
  • 16. Reports by Nicole
    • How many new children's (shelving locations) books (Item types) did Library X add to the shared catalog in November 2008?
      • SELECT count(i.biblionumber) as added, i.itype, i.homebranch, i.location from items i WHERE YEAR(i.dateaccessioned) = 2008 AND MONTH(i.dateaccessioned) = 10 AND i.itype = 'BOOK' AND i.location = 'CHILDRENS' GROUP BY i.homebranch ORDER BY i.location asc
  • 17. Other Reports
    • Number of titles cataloged (added) between and / or since x date(s):
      • SELECT COUNT(*) FROM biblio WHERE datecreated BETWEEN '2009-01-01' and '2009-02-01'
      • SELECT COUNT(*) FROM biblio WHERE datecreated > '2009-02-01'
  • 18. More Help
    • NExpress ROCKS!
      • Reports: www.nexpresslibrary.org/training/reports-training/
      • Training Videos: www.nexpresslibrary.org/category/tutorial/
        • Several report videos
  • 19. Thank You
    • Nicole C. Engard Koha Documentation Manager
    • KohaCon 2009
    Slides Available Online on Koha Conference Wiki