SQL Reports in Koha
Upcoming SlideShare
Loading in...5

SQL Reports in Koha



This was my presentation at KohaCon09

This was my presentation at KohaCon09



Total Views
Views on SlideShare
Embed Views



3 Embeds 205

http://www.nexpresslibrary.org 191
http://www.slideshare.net 13
file:// 1



Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

CC Attribution-NonCommercial-ShareAlike LicenseCC Attribution-NonCommercial-ShareAlike LicenseCC Attribution-NonCommercial-ShareAlike License

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
Post Comment
Edit your comment

SQL Reports in Koha SQL Reports in Koha Presentation Transcript

  • MySQL Reports in Koha in Koha
    • Nicole C. Engard Koha Documentation Manager
    • KohaCon 2009
  • 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
  • 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`) )
  • 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.
  • 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
  • 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
  • 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
  • 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;
  • 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
  • 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 !=
  • 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
  • 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.
  • 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
  • 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
  • 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'
  • 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
  • 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'
  • More Help
    • NExpress ROCKS!
      • Reports: www.nexpresslibrary.org/training/reports-training/
      • Training Videos: www.nexpresslibrary.org/category/tutorial/
        • Several report videos
  • Thank You
    • Nicole C. Engard Koha Documentation Manager
    • KohaCon 2009
    Slides Available Online on Koha Conference Wiki