SQL Reports in Koha


Published on

This was my presentation at KohaCon09

Published in: Technology
1 Comment
No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

SQL Reports in Koha

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