| 67 | === Предвидување дали одреден артикл во одреден магацин треба да се нарача во однос на неговота достапност и потрошувачка во последниот месец дена === |
| 68 | |
| 69 | {{{#!sql |
| 70 | select a.articlename, l.locationname, |
| 71 | (case |
| 72 | when q1.dostapnost+coalesce(q2.incoming, 0)<coalesce(q3.sold, 0) then 'Yes' |
| 73 | else 'No' |
| 74 | end |
| 75 | ) as defict from articles a |
| 76 | left join storedarticles s on s.articleid=a.articleid |
| 77 | left join locations l on l.locationid=s.locationid |
| 78 | left join ( |
| 79 | select articleid, locationid, coalesce(sum(quantity), 0) as dostapnost from storedarticles s |
| 80 | group by s.sarticleid, s.locationid |
| 81 | ) q1 on q1.articleid=a.articleid and q1.locationid=l.locationid |
| 82 | left join ( |
| 83 | select articleid, locationid, coalesce(sum(quantity), 0) as incoming from orderedarticles o |
| 84 | where o.articlestatus='In progress' or o.articlestatus='Delivered' |
| 85 | group by o.articleid, o.locationid |
| 86 | ) q2 on q2.articleid=a.articleid and q2.locationid=l.locationid |
| 87 | left join ( |
| 88 | select i.articleid, l.locationid, coalesce(sum(quantity), 0) as sold from invoicedarticles i |
| 89 | left join invoices inv on inv.invoiceid=i.invoiceid and inv.datecreate between now()-interval '1 month' and now() |
| 90 | left join workers w on w.userid=inv.workeruserid |
| 91 | left join locations l on l.locationid=w.locationid |
| 92 | group by i.articleid, l.locationid |
| 93 | )q3 on q3.articleid=a.articleid and q3.locationid=l.locationid |
| 94 | order by a.articlename |
| 95 | |
| 96 | }}} |
| 97 | |