Lånestatistik Bibliotek Mellansjö

Från Svenska kohanätverkets wiki
Hoppa till navigering Hoppa till sök

Statistikrapporter som Bibliotek Mellansjö skapat. Se dem som komplement/alternativ till Hyltes rapporter!Statistik Och - precis som för Hylterapporterna krävs det att man byter ut exemplartyper och liknande till det man använder på sitt eget bibliotek

Fråga 10 Fysiskt bestånd

SELECT 
  branchname,
  COUNT(CASE WHEN itype IN ('BARNBOK','BARNKORT','BOK','BOKCD','BOKCDBARN','BOKDAISY','BOKDAISYBA','BOKMP3','BOKMP3BARN','KORTLON','LANGLAN','PAKET','REFERENS','SPRAKKURS','STORSTIL') THEN 1 END) 'Bestånd tryckt bok',
  COUNT(CASE WHEN itype IN ('LAROMDL','LAROMTERM') THEN 1 END) 'Bestånd läromedel',
  COUNT(CASE WHEN itype IN ('BARN LJUD','BARNMP3','LJUDBOK','MP3') THEN 1 END) 'Bestånd ljudböcker',
  COUNT(CASE WHEN itype IN ('BARNTAL','DAISY') THEN 1 END) 'Bestånd talböcker daisy',
  COUNT(CASE WHEN itype IN ('MUSIKCD') THEN 1 END) 'Bestånd musik',
  COUNT(CASE WHEN itype IN ('BLURAY','FILM','VHS') THEN 1 END) 'Bestånd film',
  COUNT(CASE WHEN itype IN ('KARTOR') THEN 1 END) 'Bestånd kartor',
  COUNT(CASE WHEN itype IN ('MUSIK') THEN 1 END) 'Bestånd noter',
  COUNT(CASE WHEN itype IN ('TV-SPEL') THEN 1 END) 'Bestånd TV-spel',
  COUNT(CASE WHEN itype IN ('ARTIKEL','BLANDAT','FOREMAL','SUFRPLATTA','VECKOLAN','X') THEN 1 END) 'Bestånd övrigt'
  
FROM items

LEFT JOIN branches ON (items.homebranch=branches.branchcode)

WHERE items.homebranch = <<Bibliotek|branches>> 

Vill man vara lite petig så stämmer inte ovanstående rapport, eftersom den räknar hur många exemplar som finns just när man köra rapporten - och det som efterfrågas av KB är bestånd på Nyårsafton. Egentligen borde man alltså räkna aktuellt antal exemplar, plus det som gallrats sedan nyår, minus det som förvärvats sedan nyår. Ett försök på en sådan rapport: Om man tar alternativet Alla bibliotek i en stor katalog kommer rapporten antagligen att krascha

SELECT 
CASE
WHEN branches.branchname IS NULL
THEN '=Summa'
ELSE
branches.branchname
END
AS Bibliotek,
SUM(tryckt) AS 'Bestånd tryckt bok',
SUM(laromedel) AS 'Bestånd läromedel',
SUM(ljudbok) AS 'Bestånd ljudböcker',
SUM(talbok) AS 'Bestånd talböcker daisy',
SUM(musik) AS 'Bestånd musik',
SUM(film) AS 'Bestånd film',
SUM(kartor) AS 'Bestånd kartor',
SUM(noter) 'Bestånd noter',
SUM(tvspel) 'Bestånd Tv-spel',
SUM(ovrigt) AS 'Bestånd övrigt'
FROM
(
(SELECT items.homebranch,1 as tryckt,0 as laromedel,0 as ljudbok,0 as talbok,0 as musik,0 as film,0 as kartor,0 as noter,0 as tvspel,0 as ovrigt
FROM items 

LEFT JOIN branches ON (items.homebranch=branches.branchcode)

WHERE 
items.itype IN ('BARNBOK','BARNKORT','BOK','BOKCD','BOKCDBARN','BOKDAISY','BOKDAISYBA','BOKMP3','BOKMP3BARN','KORTLON','LANGLAN','PAKET','POCKET','REFERENS','SPRAKKURS','STORSTIL')
AND items.homebranch LIKE <<Bibliotek|branches:all>>)

UNION ALL

(SELECT items.homebranch,0 as tryckt,1 as laromedel,0 as ljudbok,0 as talbok,0 as musik,0 as film,0 as kartor,0 as noter,0 as tvspel,0 as ovrigt
FROM items 

LEFT JOIN branches ON (items.homebranch=branches.branchcode)

WHERE 
items.itype IN ('LAROMDL','LAROMTERM')
AND items.homebranch  LIKE <<Bibliotek|branches:all>>) 

UNION ALL

(SELECT items.homebranch,0 as tryckt,0 as laromedel,1 as ljudbok,0 as talbok,0 as musik,0 as film,0 as kartor,0 as noter,0 as tvspel,0 as ovrigt
FROM items 

LEFT JOIN branches ON (items.homebranch=branches.branchcode)

WHERE 
items.itype IN ('BARN LJUD','BARNMP3','KASSETT','LJUDBOK','MP3')
AND items.homebranch  LIKE <<Bibliotek|branches:all>>) 

UNION ALL

(SELECT items.homebranch,0 as tryckt,0 as laromedel,0 as ljudbok,1 as talbok,0 as musik,0 as film,0 as kartor,0 as noter,0 as tvspel,0 as ovrigt
FROM items 

LEFT JOIN branches ON (items.homebranch=branches.branchcode)

WHERE 
items.itype IN ('BARNTAL','DAISY','TALBOKKASS')
AND items.homebranch  LIKE <<Bibliotek|branches:all>>) 

UNION ALL

(SELECT items.homebranch,0 as tryckt,0 as laromedel,0 as ljudbok,0 as talbok,1 as musik,0 as film,0 as kartor,0 as noter,0 as tvspel,0 as ovrigt
FROM items 

LEFT JOIN branches ON (items.homebranch=branches.branchcode)

WHERE 
items.itype IN ('MUSIKCD','MUSCDBARN')
AND items.homebranch  LIKE <<Bibliotek|branches:all>>) 

UNION ALL

(SELECT items.homebranch,0 as tryckt,0 as laromedel,0 as ljudbok,0 as talbok,0 as musik,1 as film,0 as kartor,0 as noter,0 as tvspel,0 as ovrigt
FROM items 

LEFT JOIN branches ON (items.homebranch=branches.branchcode)

WHERE 
items.itype IN ('BLURAY','FILM','VHS')
AND items.homebranch  LIKE <<Bibliotek|branches:all>>) 

UNION ALL

(SELECT items.homebranch,0 as tryckt,0 as laromedel,0 as ljudbok,0 as talbok,0 as musik,0 as film,1 as kartor,0 as noter,0 as tvspel,0 as ovrigt
FROM items 

LEFT JOIN branches ON (items.homebranch=branches.branchcode)

WHERE 
items.itype IN ('KARTOR')
AND items.homebranch  LIKE <<Bibliotek|branches:all>>) 

UNION ALL

(SELECT items.homebranch,0 as tryckt,0 as laromedel,0 as ljudbok,0 as talbok,0 as musik,0 as film,0 as kartor,1 as noter,0 as tvspel,0 as ovrigt
FROM items 

LEFT JOIN branches ON (items.homebranch=branches.branchcode)

WHERE 
items.itype IN ('MUSIK','MUSIKBARN') 
AND items.homebranch  LIKE <<Bibliotek|branches:all>>) 

UNION ALL

(SELECT items.homebranch,0 as tryckt,0 as laromedel,0 as ljudbok,0 as talbok,0 as musik,0 as film,0 as kartor,0 as noter,1 as tvspel,0 as ovrigt
FROM items 

LEFT JOIN branches ON (items.homebranch=branches.branchcode)

WHERE 
items.itype IN ('TV-SPEL')
AND items.homebranch  LIKE <<Bibliotek|branches:all>>) 

UNION ALL

(SELECT items.homebranch,0 as tryckt,0 as laromedel,0 as ljudbok,0 as talbok,0 as musik,0 as film,0 as kartor,0 as noter,0 as tvspel,1 as ovrigt
FROM items 

LEFT JOIN branches ON (items.homebranch=branches.branchcode)

WHERE 
items.itype IN ('BLANDAT','BOKPASE','CDROM','FOREMAL','FOREMAL3MD','KONTROLL','SALLSKAPSS','SUFRPLATTA','VECKOLAN','X')
AND items.homebranch  LIKE <<Bibliotek|branches:all>>) 

UNION ALL

(SELECT items.homebranch,-1 as tryckt,0 as laromedel,0 as ljudbok,0 as talbok,0 as musik,0 as film,0 as kartor,0 as noter,0 as tvspel,0 as ovrigt
FROM items 

LEFT JOIN branches ON (items.homebranch=branches.branchcode)

WHERE 
(items.dateaccessioned BETWEEN <<Datum (Nyårsafton i KB-statistiken)|date>> AND CURDATE()) 
AND items.itype IN ('BARNBOK','BARNKORT','BOK','BOKCD','BOKCDBARN','BOKDAISY','BOKDAISYBA','BOKMP3','BOKMP3BARN','KORTLON','LANGLAN','PAKET','POCKET','REFERENS','SPRAKKURS','STORSTIL')
AND items.homebranch  LIKE <<Bibliotek|branches:all>>) 

UNION ALL

(SELECT items.homebranch,0 as tryckt,-1 as laromedel,0 as ljudbok,0 as talbok,0 as musik,0 as film,0 as kartor,0 as noter,0 as tvspel,0 as ovrigt
FROM items 

LEFT JOIN branches ON (items.homebranch=branches.branchcode)

WHERE 
(items.dateaccessioned BETWEEN <<Datum (Nyårsafton i KB-statistiken)|date>> AND CURDATE()) 
AND items.itype IN ('LAROMDL','LAROMTERM')
AND items.homebranch  LIKE <<Bibliotek|branches:all>>) 

UNION ALL

(SELECT items.homebranch,0 as tryckt,0 as laromedel,-1 as ljudbok,0 as talbok,0 as musik,0 as film,0 as kartor,0 as noter,0 as tvspel,0 as ovrigt
FROM items 

LEFT JOIN branches ON (items.homebranch=branches.branchcode)

WHERE 
(items.dateaccessioned BETWEEN <<Datum (Nyårsafton i KB-statistiken)|date>> AND CURDATE()) 
AND items.itype IN ('BARN LJUD','BARNMP3','KASSETT','LJUDBOK','MP3')
AND items.homebranch  LIKE <<Bibliotek|branches:all>>) 

UNION ALL

(SELECT items.homebranch,0 as tryckt,0 as laromedel,0 as ljudbok,-1 as talbok,0 as musik,0 as film,0 as kartor,0 as noter,0 as tvspel,0 as ovrigt
FROM items 

LEFT JOIN branches ON (items.homebranch=branches.branchcode)

WHERE 
(items.dateaccessioned BETWEEN <<Datum (Nyårsafton i KB-statistiken)|date>> AND CURDATE()) 
AND items.itype IN ('BARNTAL','DAISY','TALBOKKASS')
AND items.homebranch  LIKE <<Bibliotek|branches:all>>) 

UNION ALL

(SELECT items.homebranch,0 as tryckt,0 as laromedel,0 as ljudbok,0 as talbok,-1 as musik,0 as film,0 as kartor,0 as noter,0 as tvspel,0 as ovrigt
FROM items 

LEFT JOIN branches ON (items.homebranch=branches.branchcode)

WHERE 
(items.dateaccessioned BETWEEN <<Datum (Nyårsafton i KB-statistiken)|date>> AND CURDATE()) 
AND items.itype IN ('MUSIKCD','MUSCDBARN')
AND items.homebranch  LIKE <<Bibliotek|branches:all>>) 

UNION ALL

(SELECT items.homebranch,0 as tryckt,0 as laromedel,0 as ljudbok,0 as talbok,0 as musik,-1 as film,0 as kartor,0 as noter,0 as tvspel,0 as ovrigt
FROM items 

LEFT JOIN branches ON (items.homebranch=branches.branchcode)

WHERE 
(items.dateaccessioned BETWEEN <<Datum (Nyårsafton i KB-statistiken)|date>> AND CURDATE()) 
AND items.itype IN ('BLURAY','FILM','VHS')
AND items.homebranch  LIKE <<Bibliotek|branches:all>>) 

UNION ALL

(SELECT items.homebranch,0 as tryckt,0 as laromedel,0 as ljudbok,0 as talbok,0 as musik,0 as film,-1 as kartor,0 as noter,0 as tvspel,0 as ovrigt
FROM items 

LEFT JOIN branches ON (items.homebranch=branches.branchcode)

WHERE 
(items.dateaccessioned BETWEEN <<Datum (Nyårsafton i KB-statistiken)|date>> AND CURDATE()) 
AND items.itype IN ('KARTOR')
AND items.homebranch  LIKE <<Bibliotek|branches:all>>) 

UNION ALL

(SELECT items.homebranch,0 as tryckt,0 as laromedel,0 as ljudbok,0 as talbok,0 as musik,0 as film,0 as kartor,-1 as noter,0 as tvspel,0 as ovrigt
FROM items 

LEFT JOIN branches ON (items.homebranch=branches.branchcode)

WHERE 
(items.dateaccessioned BETWEEN <<Datum (Nyårsafton i KB-statistiken)|date>> AND CURDATE()) 
AND items.itype IN ('MUSIK','MUSIKBARN') 
AND items.homebranch  LIKE <<Bibliotek|branches:all>>) 

UNION ALL

(SELECT items.homebranch,0 as tryckt,0 as laromedel,0 as ljudbok,0 as talbok,0 as musik,0 as film,0 as kartor,0 as noter,-1 as tvspel,0 as ovrigt
FROM items 

LEFT JOIN branches ON (items.homebranch=branches.branchcode)

WHERE 
(items.dateaccessioned BETWEEN <<Datum (Nyårsafton i KB-statistiken)|date>> AND CURDATE()) 
AND items.itype IN ('TV-SPEL')
AND items.homebranch  LIKE <<Bibliotek|branches:all>>) 

UNION ALL

(SELECT items.homebranch,0 as tryckt,0 as laromedel,0 as ljudbok,0 as talbok,0 as musik,0 as film,0 as kartor,0 as noter,0 as tvspel,-1 as ovrigt
FROM items 

LEFT JOIN branches ON (items.homebranch=branches.branchcode)

WHERE 
(items.dateaccessioned BETWEEN <<Datum (Nyårsafton i KB-statistiken)|date>> AND CURDATE()) 
AND items.itype IN ('ARTIKEL','BLANDAT','BOKPASE','CDROM','FOREMAL','FOREMAL3MD','KONTROLL','SALLSKAPSS','SUFRPLATTA','VECKOLAN','X')
AND items.homebranch  LIKE <<Bibliotek|branches:all>>) 

UNION ALL

(SELECT deleteditems.homebranch,1 as tryckt,0 as laromedel,0 as ljudbok,0 as talbok,0 as musik,0 as film,0 as kartor,0 as noter,0 as tvspel,0 as ovrigt
FROM deleteditems 

LEFT JOIN branches ON (deleteditems.homebranch=branches.branchcode)

WHERE 
(deleteditems.timestamp BETWEEN <<Datum (Nyårsafton i KB-statistiken)|date>> AND CURDATE())
AND deleteditems.itype IN ('BARNBOK','BARNKORT','BOK','BOKCD','BOKCDBARN','BOKDAISY','BOKDAISYBA','BOKMP3','BOKMP3BARN','KORTLON','LANGLAN','PAKET','POCKET','REFERENS','SPRAKKURS','STORSTIL')
AND deleteditems.homebranch  LIKE <<Bibliotek|branches:all>>) 

UNION ALL

(SELECT deleteditems.homebranch,0 as tryckt,1 as laromedel,0 as ljudbok,0 as talbok,0 as musik,0 as film,0 as kartor,0 as noter,0 as tvspel,0 as ovrigt
FROM deleteditems 

LEFT JOIN branches ON (deleteditems.homebranch=branches.branchcode)

WHERE 
(deleteditems.timestamp BETWEEN <<Datum (Nyårsafton i KB-statistiken)|date>> AND CURDATE())
AND deleteditems.itype IN ('LAROMDL','LAROMTERM')
AND deleteditems.homebranch  LIKE <<Bibliotek|branches:all>>) 

UNION ALL

(SELECT deleteditems.homebranch,0 as tryckt,0 as laromedel,1 as ljudbok,0 as talbok,0 as musik,0 as film,0 as kartor,0 as noter,0 as tvspel,0 as ovrigt
FROM deleteditems 

LEFT JOIN branches ON (deleteditems.homebranch=branches.branchcode)

WHERE (deleteditems.timestamp BETWEEN <<Datum (Nyårsafton i KB-statistiken)|date>> AND CURDATE())
AND deleteditems.itype IN ('BARN LJUD','BARNMP3','KASSETT','LJUDBOK','MP3')
AND deleteditems.homebranch  LIKE <<Bibliotek|branches:all>>) 

UNION ALL

(SELECT deleteditems.homebranch,0 as tryckt,0 as laromedel,0 as ljudbok,1 as talbok,0 as musik,0 as film,0 as kartor,0 as noter,0 as tvspel,0 as ovrigt
FROM deleteditems 

LEFT JOIN branches ON (deleteditems.homebranch=branches.branchcode)

WHERE 
(deleteditems.timestamp BETWEEN <<Datum (Nyårsafton i KB-statistiken)|date>> AND CURDATE())
AND deleteditems.itype IN ('BARNTAL','DAISY','TALBOKKASS')
AND deleteditems.homebranch  LIKE <<Bibliotek|branches:all>>) 

UNION ALL

(SELECT deleteditems.homebranch,0 as tryckt,0 as laromedel,0 as ljudbok,0 as talbok,1 as musik,0 as film,0 as kartor,0 as noter,0 as tvspel,0 as ovrigt
FROM deleteditems 

LEFT JOIN branches ON (deleteditems.homebranch=branches.branchcode)

WHERE 
(deleteditems.timestamp BETWEEN <<Datum (Nyårsafton i KB-statistiken)|date>> AND CURDATE())
AND deleteditems.itype IN ('MUSIKCD','MUSCDBARN')
AND deleteditems.homebranch  LIKE <<Bibliotek|branches:all>>) 

UNION ALL

(SELECT deleteditems.homebranch,0 as tryckt,0 as laromedel,0 as ljudbok,0 as talbok,0 as musik,1 as film,0 as kartor,0 as noter,0 as tvspel,0 as ovrigt
FROM deleteditems 

LEFT JOIN branches ON (deleteditems.homebranch=branches.branchcode)

WHERE 
(deleteditems.timestamp BETWEEN <<Datum (Nyårsafton i KB-statistiken)|date>> AND CURDATE())
AND deleteditems.itype IN ('BLURAY','FILM','VHS')
AND deleteditems.homebranch  LIKE <<Bibliotek|branches:all>>) 

UNION ALL

(SELECT deleteditems.homebranch,0 as tryckt,0 as laromedel,0 as ljudbok,0 as talbok,0 as musik,0 as film,1 as kartor,0 as noter,0 as tvspel,0 as ovrigt
FROM deleteditems 

LEFT JOIN branches ON (deleteditems.homebranch=branches.branchcode)

WHERE 
(deleteditems.timestamp BETWEEN <<Datum (Nyårsafton i KB-statistiken)|date>> AND CURDATE())
AND deleteditems.itype IN ('KARTOR')
AND deleteditems.homebranch  LIKE <<Bibliotek|branches:all>>) 

UNION ALL

(SELECT deleteditems.homebranch,0 as tryckt,0 as laromedel,0 as ljudbok,0 as talbok,0 as musik,0 as film,0 as kartor,1 as noter,0 as tvspel,0 as ovrigt
FROM deleteditems 

LEFT JOIN branches ON (deleteditems.homebranch=branches.branchcode)

WHERE 
(deleteditems.timestamp BETWEEN <<Datum (Nyårsafton i KB-statistiken)|date>> AND CURDATE())
AND deleteditems.itype IN ('MUSIK','MUSIKBARN') 
AND deleteditems.homebranch  LIKE <<Bibliotek|branches:all>>) 

UNION ALL

(SELECT deleteditems.homebranch,0 as tryckt,0 as laromedel,0 as ljudbok,0 as talbok,0 as musik,0 as film,0 as kartor,0 as noter,1 as tvspel,0 as ovrigt
FROM deleteditems 

LEFT JOIN branches ON (deleteditems.homebranch=branches.branchcode)

WHERE 
(deleteditems.timestamp BETWEEN <<Datum (Nyårsafton i KB-statistiken)|date>> AND CURDATE())
AND deleteditems.itype IN ('TV-SPEL')
AND deleteditems.homebranch  LIKE <<Bibliotek|branches:all>>) 

UNION ALL

(SELECT deleteditems.homebranch,0 as tryckt,0 as laromedel,0 as ljudbok,0 as talbok,0 as musik,0 as film,0 as kartor,0 as noter,0 as tvspel,1 as ovrigt
FROM deleteditems 

LEFT JOIN branches ON (deleteditems.homebranch=branches.branchcode)

WHERE 
(deleteditems.timestamp BETWEEN <<Datum (Nyårsafton i KB-statistiken)|date>> AND CURDATE())
AND deleteditems.itype IN ('ARTIKEL','BLANDAT','BOKPASE','CDROM','FOREMAL','FOREMAL3MD','KONTROLL','SALLSKAPSS','SUFRPLATTA','VECKOLAN','X')
AND deleteditems.homebranch  LIKE <<Bibliotek|branches:all>>) 
) ds
LEFT JOIN branches ON (branches.branchcode=ds.homebranch)
GROUP BY branches.branchname WITH ROLLUP


Fråga 10 Fysiskt nyförvärv

SELECT 
  branchname,
  COUNT(CASE WHEN itype IN ('BARNBOK','BARNKORT','BOK','BOKCD','BOKCDBARN','BOKDAISY','BOKDAISYBA','BOKMP3','BOKMP3BARN','KORTLON','LANGLAN','PAKET','REFERENS','SPRAKKURS','STORSTIL') THEN 1 END) 'Nyförvärv tryckt bok',
  COUNT(CASE WHEN itype IN ('LAROMDL','LAROMTERM') THEN 1 END) 'Nyförvärv läromedel',
  COUNT(CASE WHEN itype IN ('BARN LJUD','BARNMP3','LJUDBOK','MP3') THEN 1 END) 'Nyförvärv ljudböcker',
  COUNT(CASE WHEN itype IN ('BARNTAL','DAISY') THEN 1 END) 'Nyförvärv talböcker daisy',
  COUNT(CASE WHEN itype IN ('MUSIKCD') THEN 1 END) 'Nyförvärv musik',
  COUNT(CASE WHEN itype IN ('BLURAY','FILM','VHS') THEN 1 END) 'Nyförvärv film',
  COUNT(CASE WHEN itype IN ('KARTOR') THEN 1 END) 'Nyförvärv kartor',
  COUNT(CASE WHEN itype IN ('MUSIK') THEN 1 END) 'Nyförvärv noter',
  COUNT(CASE WHEN itype IN ('TV-SPEL') THEN 1 END) 'Nyförvärv TV-spel',
  COUNT(CASE WHEN itype IN ('ARTIKEL','BLANDAT','FOREMAL','SUFRPLATTA','VECKOLAN','X') THEN 1 END) 'Nyförvärv övrigt'
  
FROM items

LEFT JOIN branches ON (items.homebranch=branches.branchcode)

WHERE items.homebranch = <<Bibliotek|branches>>  
AND items.dateaccessioned BETWEEN <<Datum från|date>>-INTERVAL 1 DAY AND <<Datum till |date>>+INTERVAL 1 DAY 


GROUP BY homebranch

Fråga 10, tidskrifter

Fråga 10 är knepig - har man bara ett fåtal titlar är det helt klart enklare att räkna för hand än att köra en rapport!

Det som efterfrågas är inte antalet prenumerationer utan unika titlar per kommun, dessutom ska man inte räkna veckotidningar som exempelvis Land och ICA-kuriren. Och nyförvärv ska man bara rapportera om titeln är ny för kommunen - om en filial startar en nyprenumeration av en titel som redan finns på HB räknas det inte som nyförvärv.

Det här är ett första försök att lösa frågan med en rapport. Villkoret är att man ska ha ankomstregistrerat (serial.status=2) mer än ett nummer under året - jag har helt enkelt antagit att enstaka nummer är eftersläntare från föregående år. För oss är subscription_frequencies.id=4 för veckotidningar. Enklaste sättet att få fram den siffran för sitt eget system är att gå under Periodika > Hantera frekvenser > klicka på Redigera - så ser man i URLen vilket nummer man ska välja.

I den här versionen så visas texten Ny i högraste kolumnen så fort det finns en nystartad prenumeration - oavsett om man har haft prenumerationer på titeln tidigare eller inte. Fritt fram att fixa för den som kommer på hur det ska göras

SELECT biblio.title AS Titel ,GROUP_CONCAT(DISTINCT branches.branchname SEPARATOR '<br>') AS 'Bibliotek',COUNT(serialid) AS 'Antal mottagna nummer',GROUP_CONCAT(DISTINCT 
subscriptionhistory.histstartdate SEPARATOR '<br>') AS Startdatum,
CASE 
WHEN subscriptionhistory.histstartdate > <<Datum från|date>> 
THEN
'Ny'
ELSE
''
END
AS 'Ny?'

FROM subscription
LEFT JOIN biblio ON (biblio.biblionumber=subscription.biblionumber)
LEFT JOIN serial on (serial.subscriptionid=subscription.subscriptionid)
LEFT JOIN branches ON (branches.branchcode=subscription.branchcode)
LEFT JOIN subscription_frequencies ON (subscription_frequencies.id=subscription.periodicity)
LEFT JOIN subscriptionhistory ON (subscriptionhistory.subscriptionid=subscription.subscriptionid)

 

WHERE serial.status='2' 
AND subscription_frequencies.id NOT IN ('4')
AND serial.publisheddate BETWEEN <<Datum från|date>>-INTERVAL 1 DAY AND <<Datum till |date>>+INTERVAL 1 DAY
AND subscription.branchcode IN ('Sigel1','Sigel2','Sigel3') 

GROUP BY subscription.biblionumber
HAVING COUNT(serialid)>1 

ORDER BY biblio.title

Fråga 11 olika bestånd - om man har en placeringskod för skönlitteratur

Det enklaste är naturligtvis om man har placeringskoder på alla exemplar - då kan man göra en så här enkel rapport

SELECT 
  COUNT(CASE WHEN itype IN ('BARNBOK','BARNKORT','BOKCDBARN','BOKDAISYBA','BOKMP3BARN','BARN LJUD','BARNMP3','BARNTAL','MUSIKBARN','MUSIKBARN') THEN 1 END) 'Bestånd för barn och unga',
  COUNT(CASE WHEN location IN ('Skonlitteratur','Deckare','Deckare & Thriller','Fantasy','Fantasy/SF','Noveller') THEN 1 END) 'Bestånd av skönlitteratur',
  COUNT(CASE WHEN location IN ('REFERENS') THEN 1 END) 'Antal referensexemplar',
  COUNT(CASE WHEN itype IN ('BARNTAL','BOKCD','BOKCDBARN','BOKDAISYBA','BOKMP3','BOKMP3BARN','DAISY','STORSTIL') THEN 1 WHEN location IN ('Lattlast','Appelhyllan') THEN 1 END) 'Bestånd läsnedsättning'
  
FROM items

LEFT JOIN branches ON (items.homebranch=branches.branchcode)

 WHERE items.homebranch = <<Bibliotek|branches>> 

Fråga 11 olika bestånd - om man måste utgå från hyllsignum (Mellansjös överkrångliga version)

Mellansjös rapport för Specialfrågor om bestånd ser ut som följer. Den går inte att använda rakt av i andra kataloger, eftersom vi har skapat ett auktoriserat värde för att avgränsa på kommun. Dessutom räknar den med gallringar och nyförvärv som gjorts sedan årsskiftet - något som de flesta nog tycker är onödigt. En förenklad variant kommer att komma längre ned.

KB-statistiken definierar skönlitteratur som det som har hyllsignum H med underavdelningar. Eftersom ungdomslitteratur har hyllsignum uHc, uHce et cetera så har jag lagt både höger- och vänstertrunkering för H. Tyvärr gör det att man får med även signum som Lz Hansson, Qz Hennes & Mauritz - så den som vill vara noggrann får gärna fixa det

SELECT 
CASE WHEN 
branches.branchname IS NULL
THEN '=Summa'
ELSE
branches.branchname 
END 
AS Bibliotek,

SUM(barn) AS 'Bestånd för barn och unga',
SUM(skon) AS 'Bestånd av skönlitteratur',
SUM(referensexemplar) AS'Antal referensexemplar',
SUM(lasnedsattning) AS 'Bestånd läsnedsättning'


FROM
(
(SELECT items.homebranch, 1 as barn, 0 as skon, 0 as referensexemplar, 0 as lasnedsattning
FROM items 

LEFT JOIN library_groups ON (items.homebranch=library_groups.branchcode)

WHERE items.homebranch IS NOT NULL AND library_groups.parent_id=<<Kommun|librarygroupsparentid>> 
AND items.itype IN ('BARN TIDSK','BARNBOK','BOKCDBARN','BOKDAISYBA','BOKMP3BARN','BARN LJUD','BARNMP3','BARNTAL'))

UNION ALL 

(SELECT items.homebranch, 0 as barn, 1 as skon, 0 as referensexemplar, 0 as lasnedsattning
FROM items 

LEFT JOIN library_groups ON (items.homebranch=library_groups.branchcode)

WHERE items.homebranch IS NOT NULL AND library_groups.parent_id=<<Kommun|librarygroupsparentid>> 
AND itemcallnumber LIKE BINARY '%H%' 
AND items.itype IN ('BARN TIDSK','BARNBOK','BOK','LANGLAN','POCKET','TIDSKRIFT'))

UNION ALL 

(SELECT items.homebranch, 0 as barn, 0 as skon, 1 as referensexemplar, 0 as lasnedsattning
FROM items 

LEFT JOIN library_groups ON (items.homebranch=library_groups.branchcode)

WHERE items.homebranch IS NOT NULL AND library_groups.parent_id=<<Kommun|librarygroupsparentid>> 
AND items.location IN ('REFERENS'))

UNION ALL

(SELECT items.homebranch, 0 as barn, 0 as skon, 0 as referensexemplar, 1 as lasnedsattning
FROM items 

LEFT JOIN library_groups ON (items.homebranch=library_groups.branchcode)

WHERE items.homebranch IS NOT NULL AND library_groups.parent_id=<<Kommun|librarygroupsparentid>> AND items.itype IN ('BARNTAL','BOKCD','BOKCDBARN','BOKDAISYBA','BOKMP3','BOKMP3BARN','DAISY','STORSTIL','TALBOKKASS'))

UNION ALL 

(SELECT items.homebranch, -1 as barn, 0 as skon, 0 as referensexemplar, 0 as lasnedsattning
FROM items 

LEFT JOIN library_groups ON (items.homebranch=library_groups.branchcode)

WHERE (items.dateaccessioned BETWEEN <<Datum (Nyårsafton i KB-statistiken)|date>> AND CURDATE()) 
AND items.homebranch IS NOT NULL AND library_groups.parent_id=<<Kommun|librarygroupsparentid>> 
AND items.itype IN ('BARN TIDSK','BARNBOK','BARNKORT','BOKCDBARN','BOKDAISYBA','BOKMP3BARN','BARN LJUD','BARNMP3','BARNTAL'))

UNION ALL 

(SELECT items.homebranch, 0 as barn, -1 as skon, 0 as referensexemplar, 0 as lasnedsattning
FROM items 

LEFT JOIN library_groups ON (items.homebranch=library_groups.branchcode)

WHERE (items.dateaccessioned BETWEEN <<Datum (Nyårsafton i KB-statistiken)|date>> AND CURDATE()) 
AND items.homebranch IS NOT NULL AND library_groups.parent_id=<<Kommun|librarygroupsparentid>> 
AND itemcallnumber LIKE BINARY '%H%' 
AND items.itype IN ('BARN TIDSK','BARNBOK','BARNKORT','BOK','KORTLON','LANGLAN','POCKET','TIDSKRIFT'))

UNION ALL 

(SELECT items.homebranch, 0 as barn, 0 as skon, -1 as referensexemplar, 0 as lasnedsattning
FROM items 

LEFT JOIN library_groups ON (items.homebranch=library_groups.branchcode)

WHERE (items.dateaccessioned BETWEEN <<Datum (Nyårsafton i KB-statistiken)|date>> AND CURDATE()) 
AND items.homebranch IS NOT NULL AND library_groups.parent_id=<<Kommun|librarygroupsparentid>> 
AND items.location IN ('REFERENS'))

UNION ALL

(SELECT items.homebranch, 0 as barn, 0 as skon, 0 as referensexemplar, -1 as lasnedsattning
FROM items 

LEFT JOIN library_groups ON (items.homebranch=library_groups.branchcode)

WHERE (items.dateaccessioned BETWEEN <<Datum (Nyårsafton i KB-statistiken)|date>> AND CURDATE()) 
AND items.homebranch IS NOT NULL AND library_groups.parent_id=<<Kommun|librarygroupsparentid>> AND items.itype IN ('BARNTAL','BOKCD','BOKCDBARN','BOKDAISYBA','BOKMP3','BOKMP3BARN','DAISY','STORSTIL','TALBOKKASS'))

UNION ALL 

(SELECT deleteditems.homebranch, 1 as barn, 0 as skon, 0 as referensexemplar, 0 as lasnedsattning
FROM deleteditems 

LEFT JOIN library_groups ON (deleteditems.homebranch=library_groups.branchcode)

WHERE (deleteditems.timestamp BETWEEN <<Datum (Nyårsafton i KB-statistiken)|date>> AND CURDATE())
AND deleteditems.homebranch IS NOT NULL AND library_groups.parent_id=<<Kommun|librarygroupsparentid>> 
AND deleteditems.itype IN ('BARN TIDSK','BARNBOK','BARNKORT','BOKCDBARN','BOKDAISYBA','BOKMP3BARN','BARN LJUD','BARNMP3','BARNTAL'))

UNION ALL 

(SELECT deleteditems.homebranch, 0 as barn, 1 as skon, 0 as referensexemplar, 0 as lasnedsattning
FROM deleteditems 

LEFT JOIN library_groups ON (deleteditems.homebranch=library_groups.branchcode)

WHERE (deleteditems.timestamp BETWEEN <<Datum (Nyårsafton i KB-statistiken)|date>> AND CURDATE())
AND deleteditems.homebranch IS NOT NULL AND library_groups.parent_id=<<Kommun|librarygroupsparentid>> 
AND deleteditems.itemcallnumber LIKE BINARY '%H%' 
AND deleteditems.itype IN ('BARN TIDSK','BARNBOK','BARNKORT','BOK','KORTLON','LANGLAN','POCKET','TIDSKRIFT'))

UNION ALL 

(SELECT deleteditems.homebranch, 0 as barn, 0 as skon, 1 as referensexemplar, 0 as lasnedsattning
FROM deleteditems 

LEFT JOIN library_groups ON (deleteditems.homebranch=library_groups.branchcode)

WHERE (deleteditems.timestamp BETWEEN <<Datum (Nyårsafton i KB-statistiken)|date>> AND CURDATE())
AND deleteditems.homebranch IS NOT NULL AND library_groups.parent_id=<<Kommun|librarygroupsparentid>> 
AND deleteditems.location IN ('REFERENS'))

UNION ALL

(SELECT deleteditems.homebranch, 0 as barn, 0 as skon, 0 as referensexemplar, 1 as lasnedsattning
FROM deleteditems 

LEFT JOIN library_groups ON (deleteditems.homebranch=library_groups.branchcode)

WHERE (deleteditems.timestamp BETWEEN <<Datum (Nyårsafton i KB-statistiken)|date>> AND CURDATE())
AND deleteditems.homebranch IS NOT NULL AND library_groups.parent_id=<<Kommun|librarygroupsparentid>> AND deleteditems.itype IN ('BARNTAL','BOKCD','BOKCDBARN','BOKDAISYBA','BOKMP3','BOKMP3BARN','DAISY','STORSTIL','TALBOKKASS'))

) ds

LEFT JOIN branches ON (branches.branchcode=ds.homebranch)

GROUP BY branches.branchname WITH ROLLUP

Fråga 11 olika bestånd - om man måste utgå från hyllsignum (Förenklad version)

Om man tar alternativet Alla bibliotek i en stor katalog kommer rapporten antagligen att krascha

SELECT 
CASE WHEN 
branches.branchname IS NULL
THEN '=Summa'
ELSE
branches.branchname 
END 
AS Bibliotek,

SUM(barn) AS 'Bestånd för barn och unga',
SUM(skon) AS 'Bestånd av skönlitteratur',
SUM(referensexemplar) AS'Antal referensexemplar',
SUM(lasnedsattning) AS 'Bestånd läsnedsättning'


FROM
(
(SELECT items.homebranch, 1 as barn, 0 as skon, 0 as referensexemplar, 0 as lasnedsattning
FROM items 

WHERE items.homebranch LIKE <<Bibliotek|branches:all>>
AND items.itype IN ('BARN TIDSK','BARNBOK','BOKCDBARN','BOKDAISYBA','BOKMP3BARN','BARN LJUD','BARNMP3','BARNTAL'))

UNION ALL 

(SELECT items.homebranch, 0 as barn, 1 as skon, 0 as referensexemplar, 0 as lasnedsattning
FROM items 

WHERE items.homebranch LIKE <<Bibliotek|branches:all>>
AND itemcallnumber LIKE BINARY '%H%' 
AND items.itype IN ('BARN TIDSK','BARNBOK','BOK','LANGLAN','POCKET','TIDSKRIFT'))

UNION ALL 

(SELECT items.homebranch, 0 as barn, 0 as skon, 1 as referensexemplar, 0 as lasnedsattning
FROM items 

WHERE items.homebranch LIKE <<Bibliotek|branches:all>>
AND items.location IN ('REFERENS'))

UNION ALL

(SELECT items.homebranch, 0 as barn, 0 as skon, 0 as referensexemplar, 1 as lasnedsattning
FROM items 

WHERE items.homebranch LIKE <<Bibliotek|branches:all>> 
AND items.itype IN ('BARNTAL','BOKCD','BOKCDBARN','BOKDAISYBA','BOKMP3','BOKMP3BARN','DAISY','STORSTIL','TALBOKKASS'))

) ds

LEFT JOIN branches ON (branches.branchcode=ds.homebranch)

GROUP BY branches.branchname WITH ROLLUP

Fråga 11 Olika nyförvärv

SELECT 
  branchname,
  COUNT(CASE WHEN itype IN ('BARN LJUD','BARN TIDSK','BARNBOK','BARNKORT','BOKCDBARN','BOKDAISYBA','BOKMP3BARN') THEN 1 END) 'Nyförvärv tryckt för barn'
  
FROM items

LEFT JOIN branches ON (items.homebranch=branches.branchcode)

WHERE items.homebranch IS NOT NULL 
AND items.dateaccessioned BETWEEN <<Datum från|date>>-INTERVAL 1 DAY AND <<Datum till |date>>+INTERVAL 1 DAY 
AND items.homebranch = <<Bibliotek|branches>>


GROUP BY homebranch

Fråga 11 Olika utlån

Förutsätter att man har en separat uppsättning exemplartyper för olika sorters barnmedia

SELECT 
  branchname AS Bibliotek,
  COUNT(CASE WHEN type IN ('issue','renew') AND itemtype IN ('BARNBOK','BARNKORT','BOKCDBARN','BOKDAISYBA','BOKMP3BARN','BARN LJUD','BARNMP3','BARNTAL') THEN 1 END) 'Utlån för barn',
  COUNT(CASE WHEN type IN ('issue','renew') AND itemtype IN ('BARNTAL','BOKCD','BOKCDBARN','BOKDAISYBA','BOKMP3','BOKMP3BARN','DAISY','STORSTIL') THEN 1 WHEN type IN ('issue','renew') AND location IN ('Lattlast','Appelhyllan') THEN 1 END) 'Utlån läsnedsättning'  
FROM statistics

LEFT JOIN branches ON (statistics.branch=branches.branchcode)

WHERE statistics.branch = <<Bibliotek|branches>> 
AND statistics.itemtype IS NOT NULL 
AND statistics.datetime BETWEEN <<Datum från|date>>-INTERVAL 1 DAY AND <<Datum till |date>>+INTERVAL 1 DAY 
 


GROUP BY branch

Fråga 12 Frågor om språk

Bibliotek Mellansjö har ett stort antal äldre katalogposter från ett av de tidigare systemen som saknar språkkod. De flesta av dem är på svenska, så vi låter helt enkelt tom språkkod räknas som svenska

SELECT 
CASE 
WHEN substring( ExtractValue(metadata,'//controlfield[@tag="008"]'), 36,3 )='swe' THEN '1 Svenska'
WHEN substring( ExtractValue(metadata,'//controlfield[@tag="008"]'), 36,3 )='' THEN '1 Svenska'
WHEN substring( ExtractValue(metadata,'//controlfield[@tag="008"]'), 36,3 )='fin' THEN '2 Minoritets'
WHEN substring( ExtractValue(metadata,'//controlfield[@tag="008"]'), 36,3 )='9mk' THEN '2 Minoritets'
WHEN substring( ExtractValue(metadata,'//controlfield[@tag="008"]'), 36,3 )='smi' THEN '2 Minoritets'
WHEN substring( ExtractValue(metadata,'//controlfield[@tag="008"]'), 36,3 )='smj' THEN '2 Minoritets'
WHEN substring( ExtractValue(metadata,'//controlfield[@tag="008"]'), 36,3 )='sme' THEN '2 Minoritets'
WHEN substring( ExtractValue(metadata,'//controlfield[@tag="008"]'), 36,3 )='sms' THEN '2 Minoritets'
WHEN substring( ExtractValue(metadata,'//controlfield[@tag="008"]'), 36,3 )='sma' THEN '2 Minoritets'
WHEN substring( ExtractValue(metadata,'//controlfield[@tag="008"]'), 36,3 )='rom' THEN '2 Minoritets'
WHEN substring( ExtractValue(metadata,'//controlfield[@tag="008"]'), 36,3 )='yid' THEN '2 Minoritets'
ELSE '3 Utländska'
END
AS 'Språk',
COUNT(*) AS 'Antal titlar'
    
FROM biblio_metadata 

GROUP BY 1

Vill man räkna bara en del av katalogen lägger man till en WHERE-sats där man säger att biblionumber ska förekomma i och så gör man sitt urval i en subquery. Rapporten går inte att använda rakt av, eftersom den innehåller vårt lokala auktoriserade värde för kommuner

SELECT 
CASE 
WHEN substring( ExtractValue(metadata,'//controlfield[@tag="008"]'), 36,3 )='swe' THEN '1 Svenska'
WHEN substring( ExtractValue(metadata,'//controlfield[@tag="008"]'), 36,3 )='' THEN '1 Svenska'
WHEN substring( ExtractValue(metadata,'//controlfield[@tag="008"]'), 36,3 )='fin' THEN '2 Minoritets'
WHEN substring( ExtractValue(metadata,'//controlfield[@tag="008"]'), 36,3 )='9mk' THEN '2 Minoritets'
WHEN substring( ExtractValue(metadata,'//controlfield[@tag="008"]'), 36,3 )='smi' THEN '2 Minoritets'
WHEN substring( ExtractValue(metadata,'//controlfield[@tag="008"]'), 36,3 )='smj' THEN '2 Minoritets'
WHEN substring( ExtractValue(metadata,'//controlfield[@tag="008"]'), 36,3 )='sme' THEN '2 Minoritets'
WHEN substring( ExtractValue(metadata,'//controlfield[@tag="008"]'), 36,3 )='sms' THEN '2 Minoritets'
WHEN substring( ExtractValue(metadata,'//controlfield[@tag="008"]'), 36,3 )='sma' THEN '2 Minoritets'
WHEN substring( ExtractValue(metadata,'//controlfield[@tag="008"]'), 36,3 )='rom' THEN '2 Minoritets'
WHEN substring( ExtractValue(metadata,'//controlfield[@tag="008"]'), 36,3 )='yid' THEN '2 Minoritets'
ELSE '3 Utländska'
END
AS 'Språk',
COUNT(*) AS 'Antal titlar'
    
FROM biblio_metadata 

WHERE
biblio_metadata.biblionumber
IN
(SELECT items.biblionumber

FROM items
LEFT JOIN library_groups ON (items.homebranch=library_groups.branchcode)

WHERE items.homebranch IS NOT NULL 
AND library_groups.parent_id=<<Kommun|librarygroupsparentid>>) 

GROUP BY 1

Fråga 14 Lånestatistik

SELECT 
  branchname AS Bibliotek,
  COUNT(CASE WHEN type = 'issue' AND itemtype IN ('BARNBOK','BARNKORT','BOK','BOKCD','BOKCDBARN','BOKDAISY','BOKDAISYBA','BOKMP3','BOKMP3BARN','KORTLON','LANGLAN','PAKET','REFERENS','SPRAKKURS','STORSTIL') THEN 1 END) 'Utlån tryckt bok',
  COUNT(CASE WHEN type = 'renew' AND itemtype IN ('BARNBOK','BARNKORT','BOK','BOKCD','BOKCDBARN','BOKDAICY','BOKDAISYBA','BOKMP3','BOKMP3BARN','KORTLON','LANGLAN','PAKET','REFERENS','SPRAKKURS','STORSTIL') THEN 1 END) 'Omlån tryckt bok',
  COUNT(CASE WHEN type = 'issue' AND itemtype IN ('LAROMDL','LAROMTERM') THEN 1 END) 'Utlån läromedel',
  COUNT(CASE WHEN type = 'renew' AND itemtype IN ('LAROMDL','LAROMTERM') THEN 1 END) 'Omlån läromedel',
  COUNT(CASE WHEN type = 'issue' AND itemtype IN ('BARN LJUD','BARNMP3','LJUDBOK','MP3') THEN 1 END) 'Utlån ljudböcker',
  COUNT(CASE WHEN type = 'renew' AND itemtype IN ('BARN LJUD','BARNMP3','LJUDBOK','MP3') THEN 1 END) 'Omlån ljudböcker',
  COUNT(CASE WHEN type = 'issue' AND itemtype IN ('BARNTAL','DAISY') THEN 1 END) 'Utlån talböcker daisy',
  COUNT(CASE WHEN type = 'renew' AND itemtype IN ('BARNTAL','DAISY') THEN 1 END) 'Omlån talböcker daisy',
  COUNT(CASE WHEN type = 'issue' AND itemtype IN ('BARN TIDSK','TIDSKRIFT') THEN 1 END) 'Utlån tidskrifter',
  COUNT(CASE WHEN type = 'renew' AND itemtype IN ('BARN TIDSK','TIDSKRIFT') THEN 1 END) 'Omlån tidskrifter',
  COUNT(CASE WHEN type = 'issue' AND itemtype IN ('MUSIKCD') THEN 1 END) 'Utlån musik',
  COUNT(CASE WHEN type = 'renew' AND itemtype IN ('MUSIKCD') THEN 1 END) 'Omlån musik',
  COUNT(CASE WHEN type = 'issue' AND itemtype IN ('BLURAY','FILM','VHS') THEN 1 END) 'Utlån film',
  COUNT(CASE WHEN type = 'renew' AND itemtype IN ('BLURAY','FILM','VHS') THEN 1 END) 'Omlån film',
  COUNT(CASE WHEN type = 'issue' AND itemtype IN ('KARTOR') THEN 1 END) 'Utlån kartor',
  COUNT(CASE WHEN type = 'renew' AND itemtype IN ('KARTOR') THEN 1 END) 'Omlån kartor',
  COUNT(CASE WHEN type = 'issue' AND itemtype IN ('MUSIK','MUSIKBARN') THEN 1 END) 'Utlån noter',
  COUNT(CASE WHEN type = 'renew' AND itemtype IN ('MUSIK','MUSIKBARN') THEN 1 END) 'Omlån noter',
  COUNT(CASE WHEN type = 'issue' AND itemtype IN ('TV-SPEL') THEN 1 END) 'Utlån TV-spel',
  COUNT(CASE WHEN type = 'renew' AND itemtype IN ('TV-SPEL') THEN 1 END) 'Omlån TV-spel',
  COUNT(CASE WHEN type = 'issue' AND itemtype IN ('ARTIKEL','BLANDAT','FOREMAL','SUFRPLATTA','VECKOLAN','X') THEN 1 END) 'Utlån övrigt',
  COUNT(CASE WHEN type = 'renew' AND itemtype IN ('ARTIKEL','BLANDAT','FOREMAL','SUFRPLATTA','VECKOLAN','X') THEN 1 END) 'Omlån övrigt'

FROM statistics

LEFT JOIN branches ON (statistics.branch=branches.branchcode)

WHERE statistics.branch=<<Bibliotek|branches>> 
AND statistics.itemtype IS NOT NULL 
AND statistics.datetime BETWEEN <<Datum från|date>>-INTERVAL 1 DAY AND <<Datum till |date>>+INTERVAL 1 DAY 


GROUP BY branch

ORDER BY branchname

Fråga 16 Fjärrlån

Fråga 16 blandar äpplen och päron. Fjärrlån-in räknar antal lån som gjorts av exemplartyp Fjärrlån. Fjärrlån-ut räknar antal lån som gjorts av låntagare i kategorin bibliotek. Listan med borrowers.cardnumber är bibliotek inom vår egen organisation, eftersom de biblioteken har låntagarkategori bibliotek men inte ska räknas som fjärrlån

SELECT 
CASE
WHEN branchname IS NULL
THEN '=Summa'
ELSE
 branchname
 END AS Bibliotek,
 SUM(utlan) AS 'Utlån',
 SUM(inlan) AS 'Inlån'
 
 FROM
 (
 (SELECT branch,1 as utlan, 0 as inlan
 FROM statistics

LEFT JOIN borrowers ON (statistics.borrowernumber=borrowers.borrowernumber)
LEFT JOIN branches ON (statistics.branch=branches.branchcode)

WHERE type = 'issue' 
AND borrowers.categorycode IN ('BIBLIOTEK') 
AND borrowers.cardnumber NOT IN ('8bxq','8bxz','8bya','8byb','8byc','8byi','8byl','8bym','8byo','8byq','8byr','8bys','Gull','Hjo','Hova','Kabo','Kbro','Mari','Skgy','Sksb','Tida','Tikf','Tore','Vagy')
AND statistics.branch LIKE <<Bibliotek|branches:all>>
AND statistics.itemtype IS NOT NULL 
AND statistics.datetime BETWEEN <<Datum från|date>>-INTERVAL 1 DAY AND <<Datum till |date>>+INTERVAL 1 DAY 
 )
 
 UNION ALL
 
 (SELECT branch,0 as utlan, 1 as inlan
 FROM statistics

LEFT JOIN borrowers ON (statistics.borrowernumber=borrowers.borrowernumber)
LEFT JOIN branches ON (statistics.branch=branches.branchcode)

WHERE type = 'issue' 
AND itemtype IN ('FJARRLAN')
AND statistics.branch LIKE<<Bibliotek|branches:all>>
AND statistics.itemtype IS NOT NULL 
AND statistics.datetime BETWEEN <<Datum från|date>>-INTERVAL 1 DAY AND <<Datum till |date>>+INTERVAL 1 DAY  )
) ds
LEFT JOIn branches On (branches.branchcode=ds.branch)

GROUP BY branchname WITH ROLLUP

Fråga 19 Aktiva låntagare

SELECT 
CASE WHEN
branchname IS NULL
THEN
'=Summa'
ELSE
branchname 
END 
AS 'Låntagarens hembibliotek',
COUNT(CASE WHEN code = 'PERSNUMMER' AND LEFT( RIGHT(attribute, 2), 1) % 2 = 0 THEN 1 END) 'Aktiva kvinnor',
COUNT(CASE WHEN code = 'PERSNUMMER' AND LEFT( RIGHT(attribute, 2), 1) % 2 = 1 THEN 1 END) 'Aktiva män',
COUNT (CASE WHEN borrowers.categorycode IN ('BIBLIOTEK','ELEVTIDA','FORSKOLA','INSTT','KVALON','PERSONAL','PTID','SKOLA','SKOLBIBTID') THEN 1 END) 'Institutionslåntagare',
COUNT(CASE WHEN dateofbirth > DATE_SUB(CURDATE(), INTERVAL 18 YEAR) OR borrowers.categorycode IN ('ELEVTIDA','SKOLBIBTID','BARN') THEN 1 END) 'Under 18 år'

FROM borrowers 

LEFT JOIN borrower_attributes ON (borrowers.borrowernumber=borrower_attributes.borrowernumber) 
LEFT JOIN branches ON (borrowers.branchcode=branches.branchcode)

WHERE 
borrowers.borrowernumber IN

(SELECT borrowernumber 

FROM
statistics 

WHERE 
statistics.datetime BETWEEN <<Datum från|date>>-INTERVAL 1 DAY AND <<Datum till |date>>+INTERVAL 1 DAY AND statistics.branch LIKE <<Utlånande bibliotek|branches:all>> AND statistics.type='issue')

GROUP BY branchname WITH ROLLUP

Fråga 19 Aktiva låntagare (experiment för 2019 och framåt)

Ett försök att använda den nya systempreferensen TrackLastPatronActivity


SELECT 
  branches.branchname AS Bibliotek,
  COUNT(CASE WHEN borrower_attributes.code='PERSNUMMER'  AND LEFT( RIGHT(attribute, 2), 1) % 2 = 0 THEN 1 END) 'Kvinnor',
  COUNT(CASE WHEN borrower_attributes.code='PERSNUMMER'  AND LEFT( RIGHT(attribute, 2), 1) % 2 = 1 THEN 1 END) 'Män',
  COUNT (CASE WHEN borrowers.categorycode IN ('BIBLIOTEK','ELEVTIDA','FORSKOLA','INSTT','KVALON','PERSONAL','PTID','SKOLA','SKOLBIBTID') THEN 1 END) 'Institutionslåntagare',
  COUNT(CASE WHEN dateofbirth > DATE_SUB(CURDATE(), INTERVAL 18 YEAR) OR borrowers.categorycode IN ('ELEVTIDA','SKOLBIBTID','BARN') THEN 1 END) 'Under 18 år'

FROM borrowers
LEFT JOIN borrower_attributes ON (borrowers.borrowernumber=borrower_attributes.borrowernumber)
LEFT JOIN branches ON (borrowers.branchcode=branches.branchcode)

WHERE borrowers.lastseen BETWEEN <<Datum från|date>>-INTERVAL 1 DAY 
AND <<Datum till |date>>+INTERVAL 1 DAY 
AND borrowers.branchcode=<<Bibliotek|branches>>

GROUP BY borrowers.branchcode