| | 197 | |
| | 198 | === Извештај за најпозајмени жанри на временски периоди === |
| | 199 | {{{#!sql |
| | 200 | WITH TimePeriodLoans AS ( |
| | 201 | SELECT |
| | 202 | b.Genre, |
| | 203 | l.LoanDate, |
| | 204 | DATE_TRUNC('month', l.LoanDate) as MonthPeriod, |
| | 205 | DATE_TRUNC('quarter', l.LoanDate) as QuarterPeriod, |
| | 206 | DATE_TRUNC('year', l.LoanDate) as YearPeriod |
| | 207 | FROM |
| | 208 | Book b |
| | 209 | JOIN Book_Copies bc ON b.BookID = bc.BookID |
| | 210 | JOIN Loan l ON bc.CopyID = l.BookCopyID |
| | 211 | ) |
| | 212 | SELECT |
| | 213 | Genre, |
| | 214 | Period, |
| | 215 | PeriodType, |
| | 216 | LoanCount, |
| | 217 | ROUND(AVG(LoanCount) OVER ( |
| | 218 | PARTITION BY Genre, PeriodType |
| | 219 | ORDER BY Period |
| | 220 | ROWS BETWEEN 2 PRECEDING AND CURRENT ROW |
| | 221 | ), 2) as MovingAverage |
| | 222 | FROM ( |
| | 223 | SELECT |
| | 224 | Genre, |
| | 225 | MonthPeriod as Period, |
| | 226 | 'Monthly' as PeriodType, |
| | 227 | COUNT(*) as LoanCount |
| | 228 | FROM TimePeriodLoans |
| | 229 | GROUP BY Genre, MonthPeriod |
| | 230 | UNION ALL |
| | 231 | SELECT |
| | 232 | Genre, |
| | 233 | QuarterPeriod, |
| | 234 | 'Quarterly', |
| | 235 | COUNT(*) |
| | 236 | FROM TimePeriodLoans |
| | 237 | GROUP BY Genre, QuarterPeriod |
| | 238 | UNION ALL |
| | 239 | SELECT |
| | 240 | Genre, |
| | 241 | YearPeriod, |
| | 242 | 'Yearly', |
| | 243 | COUNT(*) |
| | 244 | FROM TimePeriodLoans |
| | 245 | GROUP BY Genre, YearPeriod |
| | 246 | ) as CombinedPeriods |
| | 247 | ORDER BY |
| | 248 | PeriodType, Period DESC, LoanCount DESC; |
| | 249 | }}} |