| 1 | = Напредни извештаи од базата (SQL и складирани процедури) = |
| 2 | |
| 3 | == Извештај за најдобрите магационери во однос на нивната највредна креирана фактура. Се прикажуваат информации за вкупната сума во фактурата која ја наплатиле и дополнително се прикажува фактурираниот артиклот кој имал најголема вкупна цена во истата таа фактура== |
| 4 | {{{#!sql |
| 5 | select u.firstname, u.lastname, u.username, u.email, max(ti.totalPrice) as topInvoiceSum, |
| 6 | (select a.articlename from articles a |
| 7 | left join invoicedarticles i2 on i2.articleid=a.articleid |
| 8 | where i2.price*i2.quantity=max(ba.totalPrice)) as topArticleName, |
| 9 | (select i2.price from articles a |
| 10 | left join invoicedarticles i2 on i2.articleid=a.articleid |
| 11 | where i2.price*i2.quantity=max(ba.totalPrice)) as topArticlePrice, |
| 12 | (select i2.quantity from articles a |
| 13 | left join invoicedarticles i2 on i2.articleid=a.articleid |
| 14 | where i2.price*i2.quantity=max(ba.totalPrice)) as topArticleQuantity, |
| 15 | max(ba.totalPrice) as topArticleTotalPrice from workers w |
| 16 | left join users u on u.userid=w.userid |
| 17 | left join |
| 18 | ( |
| 19 | select i.invoiceid, i.workeruserid , sum(ia.price*ia.quantity) as totalPrice from invoices i |
| 20 | left join invoicedarticles ia on ia.invoiceid=i.invoiceid |
| 21 | group by i.invoiceid, i.workeruserid |
| 22 | ) ti on ti.workeruserid=u.userid |
| 23 | left join |
| 24 | ( |
| 25 | select ia2.invoiceid, a.articlename, ia2.price as price, ia2.quantity as quantity, ia2.price*ia2.quantity as totalPrice from invoicedarticles ia2 |
| 26 | left join articles a on a.articleid=ia2.articleid |
| 27 | ) ba on ba.invoiceid=ti.invoiceid |
| 28 | group by u.userid |
| 29 | order by topInvoiceSum desc |
| 30 | limit 5 |
| 31 | }}} |
| 32 | |
| 33 | == Извештај за најнепрофитабилната категорија на артикли. Се прикажуваат информации за името на категоријата, нејзиниот профит и информации за вкупниот и просечниот прилив и одлив по артикл== |
| 34 | |
| 35 | {{{#!sql |
| 36 | select c.categoryname, prihod.pri-odliv.odl as profit, prihod.pri as priliv, odliv.odl as odliv, |
| 37 | (select avg(i2.quantity*i2.price) from invoicedarticles i2) as avgPrihod, |
| 38 | (select avg(o2.quantity*o2.price) from orderedarticles o2) as avgOdliv |
| 39 | from categories c |
| 40 | left join ( |
| 41 | select abtc.categoryid, coalesce(sum(i.quantity*i.price), 0) as pri from articles a |
| 42 | left join article_belongs_to_category abtc on abtc.articleid=a.articleid |
| 43 | left join invoicedarticles i on i.articleid=a.articleid |
| 44 | group by abtc.categoryid |
| 45 | ) prihod on prihod.categoryid=c.categoryid |
| 46 | left join ( |
| 47 | select abtc.categoryid, coalesce(sum(o.quantity*o.price), 0) as odl from articles a |
| 48 | left join article_belongs_to_category abtc on abtc.articleid=a.articleid |
| 49 | left join orderedarticles o on o.articleid=a.articleid |
| 50 | group by abtc.categoryid |
| 51 | )odliv on odliv.categoryid=c.categoryid |
| 52 | where prihod.pri-odliv.odl=(select min(prihod.pri-odliv.odl) from categories c |
| 53 | left join ( |
| 54 | select abtc.categoryid, coalesce(sum(i.quantity*i.price), 0) as pri from articles a |
| 55 | left join article_belongs_to_category abtc on abtc.articleid=a.articleid |
| 56 | left join invoicedarticles i on i.articleid=a.articleid |
| 57 | group by abtc.categoryid |
| 58 | ) prihod on prihod.categoryid=c.categoryid |
| 59 | left join ( |
| 60 | select abtc.categoryid, coalesce(sum(o.quantity*o.price), 0) as odl from articles a |
| 61 | left join article_belongs_to_category abtc on abtc.articleid=a.articleid |
| 62 | left join orderedarticles o on o.articleid=a.articleid |
| 63 | group by abtc.categoryid |
| 64 | )odliv on odliv.categoryid=c.categoryid) |
| 65 | }}} |