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 COUNT(FIELD) or SUM(FIELD) Counts the number of or adds up the total value of results in a column CURDATE() Is the current date (not time, just date) MONTH(FIELD) and YEAR(FIELD) Return the month and year from a field DATE_SUB(DATE, INTERVAL) 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

More Related Content

PPT
Libsys 7 to koha
PPSX
LIS Education (World Wide ‎Overview) ‎
PPTX
Classified catalogue (Tony Vimal)
PPTX
Rfid library management
PPT
Hybrid library
PPTX
POPSI
PDF
History and Development of Special Librarianship
PPTX
Library Metrics and Measurement: Counting What Counts & Making it Matter
Libsys 7 to koha
LIS Education (World Wide ‎Overview) ‎
Classified catalogue (Tony Vimal)
Rfid library management
Hybrid library
POPSI
History and Development of Special Librarianship
Library Metrics and Measurement: Counting What Counts & Making it Matter

What's hot (20)

PDF
Theory of Library Cataloguing
PPT
What Is Cataloging?
PPTX
Viniti
PPT
Academic library and their functions
PDF
Need, steps and challenges of library automation
PPT
Authority
PPTX
What is special library
PDF
Introduction to koha
PPT
Forms of catalogue
PPTX
Finance management in academic library
PPTX
Classified Catalogue Code (ccc)
PPTX
Library and Information Science (LIS) Education and Employment through Blogs:...
PPTX
DDS.pptx
PPTX
Weeding Presentation
PPTX
Classified catalogue
PPTX
Dictionary catalogue vs classified catalogue
PPTX
koha PPT 23822.pptx
PPTX
PPT
Training On Lims
Theory of Library Cataloguing
What Is Cataloging?
Viniti
Academic library and their functions
Need, steps and challenges of library automation
Authority
What is special library
Introduction to koha
Forms of catalogue
Finance management in academic library
Classified Catalogue Code (ccc)
Library and Information Science (LIS) Education and Employment through Blogs:...
DDS.pptx
Weeding Presentation
Classified catalogue
Dictionary catalogue vs classified catalogue
koha PPT 23822.pptx
Training On Lims
Ad

Similar to SQL Reports in Koha (20)

PDF
Coding Potpourri: MySQL
PPT
Unit04 dbms
PPTX
Alasql JavaScript SQL Database Library: User Manual
DOCX
MYSQL 5.5
PPTX
Mdst 3559-02-24-sql2
PDF
Sql wksht-5
PDF
Sql Commands
PPTX
DATA BASE || INTRODUCTION OF DATABASE \\ SQL 2018
PPTX
CS 542 Overview of query processing
PDF
Sql for librarians
PPT
Reportsfest revised
PPTX
MySQL Essential Training
PPTX
Crafting a Koha-Informed Vision: Integrated Library Planning with Gleanings f...
PPT
Sql tables
PPT
Sql tables
PPT
Intro databases (Table, Record, Field)
PDF
SImple SQL
PPTX
Database Concepts and Terminologies
Coding Potpourri: MySQL
Unit04 dbms
Alasql JavaScript SQL Database Library: User Manual
MYSQL 5.5
Mdst 3559-02-24-sql2
Sql wksht-5
Sql Commands
DATA BASE || INTRODUCTION OF DATABASE \\ SQL 2018
CS 542 Overview of query processing
Sql for librarians
Reportsfest revised
MySQL Essential Training
Crafting a Koha-Informed Vision: Integrated Library Planning with Gleanings f...
Sql tables
Sql tables
Intro databases (Table, Record, Field)
SImple SQL
Database Concepts and Terminologies
Ad

More from Nicole C. Engard (20)

ODP
Training Koha Libraries - KohaCon13
PDF
The New Age of Librarianship
PDF
Panning for Gold: Sifting through Emerging Technologies to Find the Real Trea...
PDF
The Accidental Systems Librarian
PDF
Implementing Open Source
PDF
Practical Open Source Software for Libraries
PDF
Intro to Wordpress
PDF
Training Librarians: Tips from the Trenches
ODP
Training on Koha
ODP
Open Source: Freedom and Community
PDF
Open Source in Libraries: Freedom and Community
PDF
Open Source Tools for Libraries
PDF
Web 2.0 Tools & Applications in Libraries
PDF
Open Source for Libraries
PDF
Using Wordpress To Create Your Website
PDF
Open Source for Libraries
PDF
Libraries Developing Openly
PDF
Teaching and Learning Technology
PDF
Why Should I Care? New Technologies for Libraries & Librarians
PDF
Open Source Software for Libraries
Training Koha Libraries - KohaCon13
The New Age of Librarianship
Panning for Gold: Sifting through Emerging Technologies to Find the Real Trea...
The Accidental Systems Librarian
Implementing Open Source
Practical Open Source Software for Libraries
Intro to Wordpress
Training Librarians: Tips from the Trenches
Training on Koha
Open Source: Freedom and Community
Open Source in Libraries: Freedom and Community
Open Source Tools for Libraries
Web 2.0 Tools & Applications in Libraries
Open Source for Libraries
Using Wordpress To Create Your Website
Open Source for Libraries
Libraries Developing Openly
Teaching and Learning Technology
Why Should I Care? New Technologies for Libraries & Librarians
Open Source Software for Libraries

Recently uploaded (20)

PDF
“A New Era of 3D Sensing: Transforming Industries and Creating Opportunities,...
PDF
Improvisation in detection of pomegranate leaf disease using transfer learni...
PPTX
MicrosoftCybserSecurityReferenceArchitecture-April-2025.pptx
PDF
The influence of sentiment analysis in enhancing early warning system model f...
PDF
How ambidextrous entrepreneurial leaders react to the artificial intelligence...
PPTX
GROUP4NURSINGINFORMATICSREPORT-2 PRESENTATION
PDF
Getting started with AI Agents and Multi-Agent Systems
PPT
Geologic Time for studying geology for geologist
PDF
NewMind AI Weekly Chronicles – August ’25 Week III
PPTX
Custom Battery Pack Design Considerations for Performance and Safety
PDF
Developing a website for English-speaking practice to English as a foreign la...
PDF
A contest of sentiment analysis: k-nearest neighbor versus neural network
PDF
A review of recent deep learning applications in wood surface defect identifi...
PDF
STKI Israel Market Study 2025 version august
PPT
Module 1.ppt Iot fundamentals and Architecture
PDF
sustainability-14-14877-v2.pddhzftheheeeee
PPTX
Configure Apache Mutual Authentication
PDF
Architecture types and enterprise applications.pdf
PPT
What is a Computer? Input Devices /output devices
PDF
CloudStack 4.21: First Look Webinar slides
“A New Era of 3D Sensing: Transforming Industries and Creating Opportunities,...
Improvisation in detection of pomegranate leaf disease using transfer learni...
MicrosoftCybserSecurityReferenceArchitecture-April-2025.pptx
The influence of sentiment analysis in enhancing early warning system model f...
How ambidextrous entrepreneurial leaders react to the artificial intelligence...
GROUP4NURSINGINFORMATICSREPORT-2 PRESENTATION
Getting started with AI Agents and Multi-Agent Systems
Geologic Time for studying geology for geologist
NewMind AI Weekly Chronicles – August ’25 Week III
Custom Battery Pack Design Considerations for Performance and Safety
Developing a website for English-speaking practice to English as a foreign la...
A contest of sentiment analysis: k-nearest neighbor versus neural network
A review of recent deep learning applications in wood surface defect identifi...
STKI Israel Market Study 2025 version august
Module 1.ppt Iot fundamentals and Architecture
sustainability-14-14877-v2.pddhzftheheeeee
Configure Apache Mutual Authentication
Architecture types and enterprise applications.pdf
What is a Computer? Input Devices /output devices
CloudStack 4.21: First Look Webinar slides

SQL Reports in Koha

  • 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 COUNT(FIELD) or SUM(FIELD) Counts the number of or adds up the total value of results in a column CURDATE() Is the current date (not time, just date) MONTH(FIELD) and YEAR(FIELD) Return the month and year from a field DATE_SUB(DATE, INTERVAL) 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(&quot;A&quot;,&quot;F&quot;,&quot;FU&quot;) 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